View Full Version : how to get IP address

04-17-2004, 06:42 AM
i wana my computer to be server and another computer in remote place to be a client. after making my computer the server, how can i get its IP address to make the remote computer talk with it?
i tried this but it worked only in the LAN:

import java.net.*;
public class LocalHostDemo {
public static void main(String args[]) {
System.out.println("Looking up local host");
try {
InetAddress localAddress = InetAddress.getLocalHost();
System.out.println("IP address: " + localAddress.getHostAddress());
catch(UnknownHostException uhe) {
System.out.println("Error - unable to resolve localhost");

i mean the client was in the same LAN, but now i wana make the client in remote place.

04-17-2004, 07:25 AM
You could get your IP adress simply by going to to www.whatismyip.com . If thats what your asking.. Not 2 sure what ur asking tho.

04-17-2004, 07:28 AM
how to get it using java?

04-17-2004, 07:32 AM
O, I am unfamiliar with java. However, you may find you answer here: http://mazsola.iit.uni-miskolc.hu/tempus/discom/doc/net/netcom.pdf

I beleive you may find your answer around page 29.

04-17-2004, 08:08 AM
i tried www.whatismyip.com the ip address that i got it from isn't the same when i go to network properties in widows, and it was the same as another compuer in the same LAN. when i wana make a connection between two computers in a different LAN which ip should i use?

04-17-2004, 02:34 PM
or in other words i wana make connection between two computers, one of them is the client and the other one is the server, how can i do it with java or even c++?

04-17-2004, 03:20 PM
Well you would use winsock in C++ .. assuming this is on windows.

You would use gethostname() and then probably use gethostbyname() to resolve the ip... There is most likely a better way but I haven't really done much winsock stuff so that's how I'd do it.

04-20-2004, 06:02 AM
actually the programming language is not the problem, what i wana know is how to make the connection, for example, in the network of our university there are many routers, if i wana connect to a computer outside this network, which IP should i give, and so on.

04-20-2004, 08:28 AM
it sounds like your question is sort of a DNS question, Correct me if I'm wrong, you have 2 computers that connect on the same network, they can resolve one another, no problem all is well, but if you put client on a remote host, it can no longer resolve name of server. This is a DNS issue of sorts. Is the IP of your computer in network neighborhood something like 192.168.x.x ....if so this means it's a private IP address.

you said
"i tried www.whatismyip.com the ip address that i got it from isn't the same when i go to network properties in windows."

This means you are probably using a either a proxy server or firewall or Network address translation of some kind....which complicates things...a lot.
The IP shown on whatismyIP is probably that of your router/firewall, proxy server, or your NAT'd IP. One book I would recommend is Java Network Protocols Blackbook....it's niiiice for java network stuff, but the internet is the best resource......

search and read, search and read, rinse and repeat.

Sorry i don't have a quick answer for you, but a lot depends on how your network is setup....if there's a campus firewall like most universities have, then a remote client wouldn't be able to make a connection to a computer on the internal campus network at all most likely.....but what you could try is doing it the other way around. Run the server remotely, and the client locally for testing purposes. Your client could make the outbound connection. Check out gotdns.org for free dynamic dns service if needed....to give your remote server a 'NAME' that resolves from anywhere on the internet.

hope this helped, but have a feeling it just gave you more questions.....there's a lot to it.......


04-20-2004, 08:51 AM
thanx alot black3842, i really appretiate your information, but till now i don't know how to make the connection through routers and firewalls :( , do u know any website for more information?

04-20-2004, 10:04 PM
Okay, when you go to your windows command prompt, and you type up ipconfig (or something similar), you will get your network address. This may be something similar to Usually it is only the last number that changes, depending on the router settings. Then, when you go to whatismyip.com, you will get your internet connection IP address. What happens when you get a connection in a lan, is kind of like a branch type system. First, you have your internet connection ---
Then, your internet connection connects to a routere ---0
Your router will then split that connection to multiple computers ---0<
Your router will give all of the new computers a different IP adress, or a LAN address, different from the Internet Connection IP adress. If your computer is behind a router/LAN, and you would like for it to be directly accessed by the internet, you must edit your forwarding/DHCP settings. For example, if you have a Linksys router, you will have to go to the configuration page (usually, and on the top menu, click the advanced tab, then click the Forwarding Tab. On this page, you would enter the port and name that you would like your computer to be accessible by (Internet Port:80), then you also have to enter in your computers lan address. (The address you get from the microsoft command prompt: ipconfig)

Does this help?

05-20-2004, 05:25 PM

if u aint got the link already then u are gonna love me for givin it to you.

this will give u all the standard classes and the construstors and methods in java. i know for a fact that there is a getIp() (not sure if that is what it is called in java) but it WILL be there somewhere!!

Another way is to think Cookies!!! lots n lots n lots of ways of gettin IP address.

..Come find me in the Java Jungle...