Can't set ephemeral port number?

S

Steve Schaeffer

Hi all,

I'm trying to specify a port number for the client side of a TCP connection
and I'm not having success. According to what I've read in the sdk 1.4.2_02
docs this should be the way to do it (and this parallels what works in C):

socket = new Socket();
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(InetAddress.getLocalHost(), localPort));
socket.connect(new InetSocketAddress(serverAddress, serverPort));

I don't get any errors, but I also don't get the desired result. In Windows
I get the correct port number assigned the first time but the reuseaddr is
ignored and I can't re-connect until after the TIME_WAIT period expires. In
Linux, I don't even get the specified local port asigned and instead I get
the next ephemeral port assigned, as though I did not specify a port or
specified port 0.

I also tried doing it this way, even though I didn't expect it to work from
reading the docs:

socket = new Socket(serverAddress, serverPort, InetAddress.getLocalHost(),
localPort);

I've searched the web and not found any contrary information, nor did I find
any examples that claim to work. Is this broken in Java?

Any information would be appreciated.

Thanks.
 
G

Gordon Beaton

I'm trying to specify a port number for the client side of a TCP
connection and I'm not having success. According to what I've read
in the sdk 1.4.2_02 docs this should be the way to do it (and this
parallels what works in C):

socket = new Socket();
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(InetAddress.getLocalHost(), localPort));
socket.connect(new InetSocketAddress(serverAddress, serverPort));

I don't get any errors, but I also don't get the desired result.

Your code works just fine for me. I get the requested local port
(according to netstat) and I can reconnect several times without
waiting for TIME_WAIT.

What address does InetAddress.getLocalHost() return on your system,
and what happens if you *only* specify the port number when you create
the bind address? i.e.:

socket.bind(new InetSocketAddress(localPort));

I'm using this JDK on Linux (Sparc):

java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build Blackdown-1.4.1-beta)
Java HotSpot(TM) Client VM (build Blackdown-1.4.1-beta, mixed mode)

/gordon
 
S

Steve Schaeffer

Gordon Beaton said:
Your code works just fine for me. I get the requested local port
(according to netstat) and I can reconnect several times without
waiting for TIME_WAIT.

What address does InetAddress.getLocalHost() return on your system,
and what happens if you *only* specify the port number when you create
the bind address? i.e.:

socket.bind(new InetSocketAddress(localPort));

I'm using this JDK on Linux (Sparc):

java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build Blackdown-1.4.1-beta)
Java HotSpot(TM) Client VM (build Blackdown-1.4.1-beta, mixed mode)

/gordon

Gordon,

Thanks very much for your reply. While it doesn't solve my problem, it did
give me a couple of ideas which led to a solution. It got me thinking about
what might be different between your system and mine. That you are on a
Sparc system seems quite significant. I suspect that there is a bug in the
Windows and Linux (x86) JVMs from Sun, but I didn't dig deep enough to prove
it because I found a document
(http://www.ncftpd.com/ncftpd/doc/misc/ephemeral_ports.html) that describes
how to set the ephemeral port range for a variety of systems. Being able to
do this obviates the need to go through these gymnastics.

To answer your questions:
InetAddress.getLocalHost() gives me <host name>/<IP address>
and
socket.bind(new InetSocketAddress(localPort)); gives the same result as what
I had.

Thanks, again.
 

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

Forum statistics

Threads
473,815
Messages
2,569,705
Members
45,494
Latest member
KandyFrank

Latest Threads

Top