This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Get remote port for connection 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 "Get remote port for connection" Watch "Get remote port for connection" New topic

Get remote port for connection

venkatesh Nunna

Joined: Jul 19, 2011
Posts: 3
whenever client sent the request to server, then the server accept the request and assign a new port number. How to get that port number on client side? please give answer that question friends
Rob Spoor

Joined: Oct 27, 2005
Posts: 19552

Welcome to the Ranch!

Socket has method getLocalSocketAddress() to return the socket address (host + port) for the local side (in your case the server) and getRemoteSocketAddress() to return the remote socket address for the other side (in your case the client). You'll need to cast the result to InetSocketAddress (after using instanceof to make sure it's safe) to be able to get the host and port.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Get remote port for connection
Similar Threads
Question on server Ports
why is localport different? Socket port number question
Question about ServerSocket port number and Socket port number
difference between Ports ?