aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Display Clients port number? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Display Clients port number?" Watch "Display Clients port number?" New topic
Author

Display Clients port number?

Ray Zeigler
Greenhorn

Joined: Jul 06, 2012
Posts: 18
Hi Im trying to figure out how to display the IP and port number of a client who has sent data to a server program that I wrote.

In my server program I've been able to display my IP and my servers port number with the following code:


But how would I display the information on the clients side?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

Why don't you put all these questions in one thread!?

WP
Ray Zeigler
Greenhorn

Joined: Jul 06, 2012
Posts: 18
Cause there all separate questions :P Do you know how I can display the clients IP and not my own?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18159
    
    8

You have that information on the server? Then you don't display it on the client. At least, the server doesn't. It's up to the client to display it.

So your server code should just send that information to the client. It's likely you'll have to modify your communications protocol to include something which transmits that information from the server to the client.
Ray Zeigler
Greenhorn

Joined: Jul 06, 2012
Posts: 18
Can you show me an example of how to do it?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18159
    
    8

An example of a server sending data to a client? Sure, I guess you haven't even started to design your communications protocol so let me suggest that when the client first connects, the server will respond by sending the IP address and port from which the client is communicating.
Ray Zeigler
Greenhorn

Joined: Jul 06, 2012
Posts: 18
Im looking for the correct java code to do this

This is an example of code that displays the hosts IP

InetAddress myIp =InetAddress.getLocalHost();
System.out.println("Listening Port: " + serverSocket.getLocalPort() );
System.out.println("IP: " + myIp.getHostAddress() );

Now I'm looking for the java code that will display the client IP. By what your saying it seems it isnt as easy to display the clients IP and port number as opposed to what I have written above which shows the Host IP and port? Is there a way of displaying it as shown above?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18159
    
    8

I'm sorry, I don't seem to understand what you're asking. First you said you wanted to display the host's information at the client, so we had a rather unproductive conversation about how to do that.

But now it seems that you want to display the client's information at the host. Do I have that right? Or is your question actually more like how to find out what it is? I seem to be having a hard time understanding you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display Clients port number?
 
Similar Threads
Network assignment HELP NEEDED
Responses from UDP Server
How to prevent others from connecting to my server?
how to get client ip from socket
java.net Socket port number question