10 Replies - 11954 Views - Last Post: 13 March 2011 - 06:13 PM Rate Topic: -----

#1 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Getting a client port and ip address from server code

Posted 13 March 2011 - 03:18 PM

Hello,

I am writing two programs a client and a server. The client will send a message to the server and reply. However I am having trouble trying to get the clients ip adress and port number so I can print it to the screen on the server side. I have looked at the InetAddress library and I think that just confused me more. The only code I tried was:

try {
    		InetAddress addr = InetAddress.getLocalHost();

    		// Get IP Address
   			 String clientip = addr.getAddress();

			} catch (UnknownHostException e) {
			}



Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Getting a client port and ip address from server code

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting a client port and ip address from server code

Posted 13 March 2011 - 03:26 PM

The Client does not have a port number ... actually kind of but this is hidden by the protocol

The client connects to the server on a predefined server port number then communication continues on a channels (not port) that are created on both server and client sides. Something that you do not want to know about, anyhow they are too much low level for Java

Even on server side, this channel re-route client messages so another Client can connect on the same port offered for connection by the server
Was This Post Helpful? 1
  • +
  • -

#3 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Re: Getting a client port and ip address from server code

Posted 13 March 2011 - 03:31 PM

so if a initiate a connection to a server code from the client there's no way to tell the port or ip?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting a client port and ip address from server code

Posted 13 March 2011 - 03:37 PM

View Postgordonheimer, on 13 March 2011 - 05:31 PM, said:

so if a initiate a connection to a server code from the client there's no way to tell the port or ip?

The port no, there is no port involved.
The ip yes ... but for which reasons ? You want to stop connections from certain IPs ?
Actually, while you are at it which IP do you want ?
Most PC IP address are 192.168.1.xxx which are kind of subadresses of the router that connects you to your ISP. And within your ISP your router address may be another subaddress of your ISP connection to the world.

There must be a few thousands 192.168.1.1 connected to Google every time.
Was This Post Helpful? 0
  • +
  • -

#5 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Re: Getting a client port and ip address from server code

Posted 13 March 2011 - 03:39 PM

View Postpbl, on 13 March 2011 - 03:37 PM, said:

View Postgordonheimer, on 13 March 2011 - 05:31 PM, said:

so if a initiate a connection to a server code from the client there's no way to tell the port or ip?

The port no, there is no port involved.
The ip yes ... but for which reasons ? You want to stop connections from certain IPs ?
Actually, while you are at it which IP do you want ?
Most PC IP address are 192.168.1.xxx which are kind of subadresses of the router that connects you to your ISP. And within your ISP your router address may be another subaddress of your ISP connection to the world.

There must be a few thousands 192.168.1.1 connected to Google every time.



I just want to retrieve the IP address of the machine that is connected to the server.
Was This Post Helpful? 0
  • +
  • -

#6 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Re: Getting a client port and ip address from server code

Posted 13 March 2011 - 03:48 PM

so something like

InetAddress clientHost = request.getAddress();
         int clientPort = request.getPort();


wouldnt work?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting a client port and ip address from server code

Posted 13 March 2011 - 03:51 PM

View Postgordonheimer, on 13 March 2011 - 05:39 PM, said:

I just want to retrieve the IP address of the machine that is connected to the server.

Which IP ?
The one inside it's lan
The one of it's router within the ISP ?
The one of the router of the ISP ?

Without offense, seems you miss some concepts here :)

Java won't let you know the 2 first ones, the address will be for display/log purpose only, you won't be able to do anything practical out of it
Was This Post Helpful? 0
  • +
  • -

#8 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Re: Getting a client port and ip address from server code

Posted 13 March 2011 - 03:54 PM

I think just in the LAN, I am just using it for display purposes.

Thank you for being patient and helping :D
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting a client port and ip address from server code

Posted 13 March 2011 - 04:16 PM

View Postgordonheimer, on 13 March 2011 - 05:48 PM, said:

so something like

InetAddress clientHost = request.getAddress();
         int clientPort = request.getPort();


wouldnt work?

request is an object of which class ?
Was This Post Helpful? 1
  • +
  • -

#10 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Re: Getting a client port and ip address from server code

Posted 13 March 2011 - 04:22 PM

I think I got it working, however my server does not stay open and immediately closes

This post has been edited by gordonheimer: 13 March 2011 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting a client port and ip address from server code

Posted 13 March 2011 - 06:13 PM

long seed=0;  
Random random;  
if (seed == 0) {           // how can seed be different than 0 here
  random = new Random();  
}  
else {                     // so the if and the else are useless
   random = new Random(seed);  
}  


:)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1