Click here to Skip to main content
15,895,538 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hey

i wrote a client/server program, when client program try close the server program will recognize this action of client side, and try to dispose all assigned resources to that socket connection, an begin to re-listen on the same port

i did this to dispose network(dont know it is right to do or not)
<code>con.CurrentConnectionSocket.Close();
((System.IDisposable)con.CurrentConnectionSocket).Dispose();
</code>

BUT when i try to re-listen, catch a 'SocketException' with this exception message

Only one usage of each socket address (protocol/network address/port) is normally permitted

how do i handle this?

i need help ASAP

tnx in advance
Posted
Updated 26-Jan-11 8:06am
v2

You cannot reuse a disposed or closed socket. If you do that, instantiate a new instance of the Socket class. Or if you want to reuse it, do not call Close (which btw calls Dispose internally - so you don't need to call Dispose once you call Close anyway). Instead call Disconnect(true) on the socket instance.

[Update]
----------
In answer to your comment on reconnecting: Just do everything that you do to make the initial connection (other than instantiating the socket object).
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 26-Jan-11 15:03pm    
Good answer, of course -- my 5.
The update is something OP could have guessed at once :-)
--SA
Sergey Alexandrovich Kryukov 26-Jan-11 15:03pm    
OP commented:

tnx, but how do i re-connect then? i still catch that exception :(
Sergey Alexandrovich Kryukov 26-Jan-11 15:04pm    
OP commented:

getting crazy for a simple reconnection code~X(

i do this but still have that problem[after i disconnect the socket]
Collapse

CurrentConnectionSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
CurrentConnectionSocket.Bind(ConnetionIPEP);
CurrentConnectionSocket.Listen(1);
ConnectionStatusString = "Waitting for client connection ....";
CurrentConnectionSocket.NoDelay = true;
StaticConnectionInfo.ConnectionRootMode = ConnectionMode.Listenning;
CurrentConnectionSocket.BeginAccept(new AsyncCallback(AcceptCon_SYNC), CurrentConnectionSocket);
StaticConnectionInfo.NetworkSideOption = Setting.SettingSideOption.Server;
StaticConnectionInfo.ServerConnectionRoot = this;
tnx, but how do i re-connect then? i still catch that exception :(
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 26-Jan-11 15:03pm    
OP commented:

getting crazy for a simple reconnection code~X(

i do this but still have that problem[after i disconnect the socket]
Collapse

CurrentConnectionSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
CurrentConnectionSocket.Bind(ConnetionIPEP);
CurrentConnectionSocket.Listen(1);
ConnectionStatusString = "Waitting for client connection ....";
CurrentConnectionSocket.NoDelay = true;
StaticConnectionInfo.ConnectionRootMode = ConnectionMode.Listenning;
CurrentConnectionSocket.BeginAccept(new AsyncCallback(AcceptCon_SYNC), CurrentConnectionSocket);
StaticConnectionInfo.NetworkSideOption = Setting.SettingSideOption.Server;
StaticConnectionInfo.ServerConnectionRoot = this;
getting crazy for a simple reconnection code~X(

i do this but still have that problem[after i disconnect the socket]
CurrentConnectionSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
CurrentConnectionSocket.Bind(ConnetionIPEP);
CurrentConnectionSocket.Listen(1);
ConnectionStatusString = "Waitting for client connection  ....";
CurrentConnectionSocket.NoDelay = true;
StaticConnectionInfo.ConnectionRootMode = ConnectionMode.Listenning;
CurrentConnectionSocket.BeginAccept(new AsyncCallback(AcceptCon_SYNC), CurrentConnectionSocket);
StaticConnectionInfo.NetworkSideOption = Setting.SettingSideOption.Server;
StaticConnectionInfo.ServerConnectionRoot = this;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900