• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get the real ip adress

 
Ramon Haayema
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
K. Tsang
Bartender
Posts: 3404
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic