Home arrow static arrow Java Programming [Archive] - set client ip on run time
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - set client ip on run time
This topic has 7 replies on 1 page.

Posts:397
Registered: 10/15/03
set client ip on run time  
Jul 21, 2004 1:46 AM



 
My client should connect to a server that sites on the same lan.
My current version has a configuration file that keep the client ip.
I would like to get the client ip at run time.
The problem is that there might be more then one communication card ->ips
And i am not sure how can i check which one of them is in the same subnet as the server (I got the server ip)
Please tell me if you got an idea.
regards,
Liat
 

Posts:319
Registered: 11/8/00
Re: set client ip on run time  
Jul 21, 2004 1:58 AM (reply 1 of 7)



 
If you connect to the server you are running using an unqualified name then it should use the nic that is on the same subnet i would assume. I not certian what the specs say regarding two nics

If you then need to find you client IP you can get it from the socket with getLocalAddress(). This will return a InetAddress which you can then use to get the ip address. see the APIs for Socket and InetAddress

http://java.sun.com/j2se/1.4.2/docs/api/java/net/InetAddress.html
http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html
 

Posts:397
Registered: 10/15/03
Re: set client ip on run time  
Jul 21, 2004 2:03 AM (reply 2 of 7)



 
the problem is not to get the ips of the client but to decide which one to use (in case of more thenone ip)
 

Posts:319
Registered: 11/8/00
Re: set client ip on run time  
Jul 21, 2004 2:17 AM (reply 3 of 7)



 
If they are on different subnets then connecting to server using an unqualified name should be use the nic on the same subnet in preference to the other. Why don't you write a sample app to test the connection eg.

public class TestClient{     public static void main(String[] args)     {          Socket s;         for (int i = 0; i < 50; i++)         {              s = new Socket("unqualified name of server", 80);              String shost = s.getLocalAddress().getHostAddress();              System.out.println("<unqualified> IP = " + shost);              s = null;         }          for (int i = 0; i < 50; i++)         {              s = new Socket("ipaddress", 80);              String shost = s.getLocalAddress().getHostAddress();              System.out.println("<qualified> IP = " + shost);              s = null;         }     }}


Although this would only give you a rough idea it may be that it will prove me wrong stright away.

 

Posts:319
Registered: 11/8/00
Re: set client ip on run time  
Jul 21, 2004 2:30 AM (reply 4 of 7)



 
If you know what your IP addresses are then you could use the Socket.bind() method to bind a socket to a local address. then call the connect method.

http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html#bind(java.net.SocketAddress)
 

Posts:397
Registered: 10/15/03
Re: set client ip on run time  
Jul 21, 2004 2:45 AM (reply 5 of 7)



 
I got all ips of the client and the ip of the server.
It wouldnt help me to bind the socket to the server?!
Maybe i should try to connect to the server with each one of the ip's and the ip that will work is the right ip?
 

Posts:319
Registered: 11/8/00
Re: set client ip on run time  
Jul 21, 2004 2:49 AM (reply 6 of 7)



 
It wouldnt help me to bind the socket to the server?!

You don't bind the socket to the server, you bind it to a local address which you then use to connect to the server! This gives you the control over which ip address is used to connect. If my understanding of the problem is right this is what you want.
 

Posts:397
Registered: 10/15/03
Re: set client ip on run time  
Jul 21, 2004 4:15 AM (reply 7 of 7)



 
I dont connect directly using a soket....
I have a framework that i use....
what i do is pass the ip and port parameters and try to connect...
SO what you say i should do is try to connect with all possible ip's?
Untill that's work or until i have passed all ip posibilities?
 
This topic has 7 replies on 1 page.