C
carl_bevil
I would like to use a single port to connect to a server. I would like
to be able to disconnect a socket using this port and then be able to
connect again (on the same port) immediately. I know there is a
TIME_WAIT value, but I thought I could get around it by using the
ResuseAddress option.
Here is what the code looks like (C#):
Socket connectSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
connectSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, (int)1); // I've tried using the
Boolean version also
connectSocket.Bind(localEndPoint);
connectSocket.Connect(remoteEndPoint); // I've tried doing this with
BeginConnect/EndConnect also
// use the socket
connectSocket.Shutdown(SocketShutdown.Both);
connectSocket.Disconnect(true); // true is to allow reuse, right?
connectSocket.Close();
// now create a new socket using the same port:
Socket connectSocket2 = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
connectSocket2.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, (int)1); // I've tried using the
Boolean version also
connectSocket2.Bind(localEndPoint);
connectSocket2.Connect(remoteEndPoint); // Again, I've tried doing this
with BeginConnect/EndConnect also.
At this point I get the error, "Only one usage of each socket address
(protocol/network address/port) is normally permitted"
So, am I misunderstanding the ResuseAddress option, or am I just using
it wrong?
Appreciate any help; thanks.
Carl
to be able to disconnect a socket using this port and then be able to
connect again (on the same port) immediately. I know there is a
TIME_WAIT value, but I thought I could get around it by using the
ResuseAddress option.
Here is what the code looks like (C#):
Socket connectSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
connectSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, (int)1); // I've tried using the
Boolean version also
connectSocket.Bind(localEndPoint);
connectSocket.Connect(remoteEndPoint); // I've tried doing this with
BeginConnect/EndConnect also
// use the socket
connectSocket.Shutdown(SocketShutdown.Both);
connectSocket.Disconnect(true); // true is to allow reuse, right?
connectSocket.Close();
// now create a new socket using the same port:
Socket connectSocket2 = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
connectSocket2.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, (int)1); // I've tried using the
Boolean version also
connectSocket2.Bind(localEndPoint);
connectSocket2.Connect(remoteEndPoint); // Again, I've tried doing this
with BeginConnect/EndConnect also.
At this point I get the error, "Only one usage of each socket address
(protocol/network address/port) is normally permitted"
So, am I misunderstanding the ResuseAddress option, or am I just using
it wrong?
Appreciate any help; thanks.
Carl