tags:

views:

188

answers:

1

I have some code which needs to know the local port it uses to connect to a server.

The Socket.localPort() call always seems to return -1. Also, do a Socket.toString() returns something like "port=33031,localport=-1" - again, with a -1.

Does anyone know why this, and how to get the local side port number? I am GNU Classpath 0.97.2 - is this just an API bug or do I not understand this call?

A: 

If you're looking at the Socket class from java.net package, it returns a -1 if the socket is not bound yet. The method is not a static method and is goes by the name, getLocalPort not just localPort. In GNU Classpath, -1 most probably means the same thing.

Joset