get local port

J

jiefeng Xu

I want to know if there is a way in Java to retrieve the local port. A
lot of the methods I find need to provide with the IP or port
information. I can find the local hostname, but not the port. Can
anyone give a hand on this. I appreciate the help. Thanks.
 
R

Roedy Green

I want to know if there is a way in Java to retrieve the local port. A
lot of the methods I find need to provide with the IP or port
information. I can find the local hostname, but not the port. Can
anyone give a hand on this. I appreciate the help. Thanks.

That question has many answers. The port number depends on the
protocol. See http://mindprod.com/jgloss/port.html
 
G

Gordon Beaton

I want to know if there is a way in Java to retrieve the local port.
A lot of the methods I find need to provide with the IP or port
information. I can find the local hostname, but not the port. Can
anyone give a hand on this. I appreciate the help. Thanks.

From your description it sounds like you're asking for something that
might be a static attribute of the host computer itself, but that's
not what port numbers are.

Every open Socket and ServerSocket has a (possibly unique) local port
number, and every connected Socket has a remote port number as well.
Consult the API documentation for these two classes to see how to get
the information.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top