Question:
Java: How to get public IP Address on a network?
Jared
2011-03-14 16:52:19 UTC
So I'm very new to socket programming. I have written a chat client-server in Java using Sockets (fairly low-level).

Basically, I run the server and then it listens on a given port. I can connect to the server by running the client program but you have to supply the ip address (and port) for the server. The port is no big deal, since I have a default value that both the client and server know about.

So here's the problem: To do this over the internet then I have to give the client my public IP address (or whoever is running the server). How can I figure this out?

If I use InetAddress.getHostByName("localhost") then it will just give me the IP address assigned by the router (or worse 127.0.0.1).

So it would be nice if I could display the public ip address in the server so that that person could then give their ip address to the client (manually...I would have to have some kind of connection server to do it automatically).

How can I get the public IP from the router rather than just giving me the local ip address of the server?

I'm not asking how to go about figuring out the public ip (I can do that through ifconfig/ipconfig or my router setup), I'm asking how to figure it out through Java so that I can display it to the person running the server.

Also for this to work over the internet I have to forward the port from the router to the correct computer (where the server is running). Is there a way to do that without having to go through the router setup (to forward the port)? For instance, in Java can I tell the router to forward the port to this computer (when the server runs)?

FYI, this is a toy project, I'm basically using it to learn about networking and communicating via the internet...so I'm sure there are better ways to go about implementing a chat program.
Three answers:
peteams
2011-03-14 17:58:05 UTC
I'm afraid using IPv4 you cannot find your public IP address without asking a server that's sitting out on the public internet itself.



The way these things normally work is a rendezvous servers is contacted by the two clients. The server knows the IP addresses of both clients and can inform each of them its peers address. Actually more than that, in many circumstances it can arrange for the two to poke a hole through their respective firewalls so they can communicate. I forget the details, there's and RFC covering it.



So I've not mentioned Java, but unfortunately if all you have are two peers on different networks they're not going to be able to communicate.
rolandini
2016-10-28 12:52:30 UTC
the following is a conditional if statement. in the journey that your does no longer specify the third argument alongside the command line (i.e port form) then it takes 7 because the default port as 7. If consumer provides third argument, which will be taken as servPort. As you comprehend all the command line arguments are saved as strings, the third argument should be switched over to integer by calling Integer.parseInt approach int servPort = (args.length == 3) ? Integer.parseInt(args[2]) : 7;
?
2011-03-15 13:45:22 UTC
yeah you can find the ip deails her by use this link http://www.zippozap.com/domain-name-to-ip-address/ and also if you need to locate the ip address means just try this www.ipdetails.com ,hope its helpful...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...