Problems with socket ReuseAddress option

B

Bob

I am trying to reuse a socket address but I am having problems. What I am
doing is creating a socket, connecting, then getting the LocalEnd point. I
then Disconnect and try to connect again using the same address but I get an
error 10048 [WSAEADDRINUSE]. Can someone tell me what I am doing wrong.
Here is the code...

ws_socket = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//Set the reuse address flag.
ws_socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,(int)1);

//Call connect which works.
ws_socket.Connect(...)
//Here i perform my socket work.
....
//Get the local endpoint & close the socket.
EndPoint endpoint = ws_socket.LocalEndPoint;
ws_socket.ShutDown(...)
ws_socket.Close();

//Now create a new socket and try to reuse the address.
ws_socket = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
ws_socket.Bind(endpoint );
//Here is where it throws the exception.
// System.SystemException {"Only one usage of each socket address
(protocol/network address/port) is normally permitted"}
System.SystemException
ws_socket.Connect(...)
 
V

Vadym Stetsyak

It is necessary to call
ws_socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,(int)1);
for the second instance of Socket class.
 

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

Top