socket.setReuseAddress(true);

C

cmk128

Hi

socket.setReuseAddress(true); doesn't release the socket after
socket.close(). Why?
Here is my testing code:

for (int i = 0; i < 10000; i++) {
try {
Socket socket = new Socket("peter-computer",
21);
socket.setReuseAddress(true);
socket.close();

} catch (Exception ex) {
ex.printStackTrace();
return;
}
}

thanks
from Peter ([email protected])
 
E

Esmond Pitt

Hi

socket.setReuseAddress(true); doesn't release the socket after
socket.close(). Why?


er, because it's not supposed to? Socket.close() always ties up the port
for 2*MSS or 2 minutes. Socket.setReuseAddress() helps you with this
problem when opening the *next* socket, not when closing the current one.
 
C

Chris Uppal

socket.setReuseAddress(true); doesn't release the socket after
socket.close(). Why?

As I understand it, setReuseAddress() affects the behaviour of the socket as
the corresponding connection is set up (as it is bound()), not the behaviour of
the /next/ socket that attempts to bind to the same port. The problem is that
the Socket constructor that takes the name of a host opens the connection for
you, so the socket has already been bound() by the time that the constructor
returns (arguably this is bad design, although it is convenient), and so the
setReuseAddress() has no effect.

I believe (but see below) that the code to use for this kind of purpose should
be:

Socket socket = new Socket();
socket.setReuseAddress(true);
socket.connect(new InetSocketAddress(m_host, m_port));

However, either my understanding is wrong, or that code is incorrect, or
there's a bug somewhere in the Java implementation or the Windows (XP and 2K)
network stacks that I've tried it on. It seems that the bind() call (inside
Socket.connect()) still fails intermittently. For my purposes (which was just
a quick stress test of a custom server implementation), it was sufficient to
catch the error and ignore it. Running in a tight loop (at around 600
connection / second) that happened to about 5% of the connection attempts.
Which, I imagine is too low to be ascribed to genuine port exhaustion. More
importantly, it was low enough for me to ignore. My real code:

....loop...
Socket socket = new Socket();
socket.setReuseAddress(true);
socket.setTcpNoDelay(true);
try
{
socket.connect(new InetSocketAddress(m_host, m_port));
}
catch (BindException e)
{
bindErrors++;
continue;
}
....more code...

It would be nice to know what was really going wrong -- /especially/ if it's a
mistake on my part -- but at least I was able to run my tests...

-- chris
 
C

cmk128

Thanks for reply
But your code doesn't work, the local port is still changing.
thanks
from Peter
 
C

cmk128

Gordon said:
That's normal and expected. Why do you want to specify the local port?

/gordon

In Java . Socket.connect() doesn't ensure it will connect to the
server, if i connect() many time, all the sockets will run out, then i
have no way to connect to the server again.
thanks
 

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,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top