| Author |
Get the real ip adress
|
Ramon Haayema
Greenhorn
Joined: May 17, 2008
Posts: 14
|
|
Hi all,
I need for a program to send packets the real ip adres and not the local adres.
now i get the ip number from the local machine but this is not the internet adres.
exapmle my computer has the following adress from the router 192.168.1.102 but my internet adress is 81.58.155.112
what i need is last adres to send data to the client/server.
or is there a other trick to send?
try {
java.net.InetAddress i = java.net.InetAddress.getLocalHost();
System.out.println(i); // name and IP address
System.out.println(i.getHostName()); // name
System.out.println(i.getHostAddress()); // IP address only
}
catch(Exception e){e.printStackTrace();
}
this give me the local adres but not the internet adress.
this all is for :
public DaemonIn()
{
this.payload = new TcpSendClass();
initServerSocket();
try
{
while (true)
{
// listen for and accept a client connection to serverSocket
Socket sock = this.serverSocket.accept();
InputStream iStream = sock.getInputStream();
ObjectInputStream oiStream = new ObjectInputStream(iStream);
this.payload = (TcpSendClass)oiStream.readObject();
//this.payload =(oiStream.getClass()) oiStream.readObject();
Thread.sleep(1000);
this.payload.hashCode();
System.out.println("recived tcp class");
DaemonLog DL = new DaemonLog();
DL.WriteToLog(this.payload.getClass().getName(),"In");
}
}
catch (SecurityException se)
{
System.err.println("Unable to get client address due to security.");
System.err.println(se.toString());
System.exit(1);
}
catch (IOException ioe)
{
System.err.println("Unable to read data from an open socket.");
System.err.println(ioe.toString());
System.exit(1);
}
catch (InterruptedException ie) { } // Thread sleep interrupted
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
this.serverSocket.close();
}
catch (IOException ioe)
{
System.err.println("Unable to close an open socket.");
System.err.println(ioe.toString());
System.exit(1);
}
}
System.out.println("Received payload:");
System.out.println(this.payload.toString());
}
and a reciver but the reciver needs to send back for that i need the adress of the client computer.
please help...
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16680
|
|
now i get the ip number from the local machine but this is not the internet adres.
exapmle my computer has the following adress from the router 192.168.1.102 but my internet adress is 81.58.155.112
Addresses that begin with 192.168.x.x are part of private networks -- they can not be reached from outside of the network. The address of 192.168.1.102 *is* your IP address. All computers that can reach you, has to use that address. The address of 81.58.155.112 is *not* your IP address. It is the address of the router, any machine in the private network going through the router will get that address.
There is no magical technique that will get you the router address, all you have is your machine's default gateway, which may goes through multiple routers before it will get to the router that gets to the internet... so it is highly dependent on how your network is configured.
Furthermore, even if you have your router's address -- what are you going to do with it? You can't just give that address to your client and expect it to magically be able to connect to a machine in the private network. The router needs to be configured to forward packets back to you.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
Henry Wong wrote:There is no magical technique that will get you the router address, all you have is your machine's default gateway, which may goes through multiple routers before it will get to the router that gets to the internet... so it is highly dependent on how your network is configured.
There are pages on the with the sole purpose of reporting back your WAN IP address. It is possible to do a request to such a page (using URL and URLConnection, for instance), then parse the resulting HTML page to get back the WAN IP. But yeah, it's a workaround. You really can't get your WAN IP address without asking a party in the WAN (aka the Internet) to return it for you.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
K. Tsang
Ranch Hand
Joined: Sep 13, 2007
Posts: 1219
|
|
|
So what happens if you dial up directly with your modem not going through router? That IP address should be the WAN address. Then when you do InetAddress.getLocalHost() you should get that WAN address.
|
K. Tsang JavaRanch SCJP5 SCJD/OCM-JD
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
|
Sure, but how often does that occur these days? I don't know many people without some router or firewall installed in between the modem and PC(s).
|
 |
 |
|
|
subject: Get the real ip adress
|
|
|