Socket error on service stop
-
Well I'm new to programming using sockets. What i am trying to do is create a client/server model. The client will report into the server every so often, and the server will send back data telling the agent what to do. (Monitoring software). The problem I am having is closing the socket on the server. I get errors like: A blocking operation was interrupted by a call to WSACancelBlockingCall For my code:
while (running)
{
try
{
Socket socket = this.listener.Accept();if (socket != null) { Thread handleThread = new Thread(new ParameterizedThreadStart(HandleTheClient)); handleThread.Start(socket); } } catch (Exception ex) { Logging.Log(ex.ToString()); } }
Closing:
protected override void OnStop()
{
running = false;if (listener != null) { listener.Shutdown(SocketShutdown.Both); listener.Close(); } }
Now I know the problem is with;
Socket socket = this.listener.Accept();
But how do I fix this? IS the only way to use AcceptAsync? IF you are wondering why I'm not using that then its because I haven't learned it yet. I need to read up on it first. So can I break this block somehow when the service is trying to stop so the service will stop without error or even stop?
-
Well I'm new to programming using sockets. What i am trying to do is create a client/server model. The client will report into the server every so often, and the server will send back data telling the agent what to do. (Monitoring software). The problem I am having is closing the socket on the server. I get errors like: A blocking operation was interrupted by a call to WSACancelBlockingCall For my code:
while (running)
{
try
{
Socket socket = this.listener.Accept();if (socket != null) { Thread handleThread = new Thread(new ParameterizedThreadStart(HandleTheClient)); handleThread.Start(socket); } } catch (Exception ex) { Logging.Log(ex.ToString()); } }
Closing:
protected override void OnStop()
{
running = false;if (listener != null) { listener.Shutdown(SocketShutdown.Both); listener.Close(); } }
Now I know the problem is with;
Socket socket = this.listener.Accept();
But how do I fix this? IS the only way to use AcceptAsync? IF you are wondering why I'm not using that then its because I haven't learned it yet. I need to read up on it first. So can I break this block somehow when the service is trying to stop so the service will stop without error or even stop?
Before calling the Accept method, you must first call the Listen method to listen for and queue incoming connection requests. Have you tried it Whatever,still now if you receive a SocketException,you can use the SocketException.ErrorCode property to obtain the specific error code. After getting code you can refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error. I Hope it will work for you. :)
-
Before calling the Accept method, you must first call the Listen method to listen for and queue incoming connection requests. Have you tried it Whatever,still now if you receive a SocketException,you can use the SocketException.ErrorCode property to obtain the specific error code. After getting code you can refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error. I Hope it will work for you. :)
I didn't show all of my code.. sorry about that. I called the Listen for the WHILE statement. The problem is when doing it this way it blocks at the Accept() method. The way I have it works. I can accept connections and do what I want with the data (I'm sending / receiving serialized data). The problem is when someone goes and tries to stop the service. That is when I try to call the Shutdown and Close method and thats when it hangs I decided to use async sockets
modified on Saturday, November 6, 2010 12:48 PM