Jared
2011-03-14 16:52:19 UTC
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.