Exit from blocking thread on TCP, read in C #

My server / client will start a new "readerThread ()" to read incoming tcp traffic. This thread is blocking while reading (). How can I get out of this readerThread ().

One way is to start another thread that closes the socket when the thread needs to finish, so the read will complete. is there a cleaner / better way to do this.

+2


source to share


5 answers


I misunderstood the question. This is what I think you should do.



  • If you've created a socket on the parent thread and are only using the new stream to read incoming data, then I would suggest calling Socket.Shutdown (). So the Receive methods will return 0 (no bytes) and you can exit the thread method. Shutdown will disable send / receive, but if there is any data waiting to be sent / received in the buffer, it will ensure that it is sent / received before the socket is closed. The Receive method will return 0 if you call shutdown when the socket is blocked in Receive, but it throws a socket exception with Socket = Shutdown (or 10058) error code. So be prepared to catch and process it.

  • If you create a socket on a new thread and accept new connections (Socket.Listen () and Socket.Accept) , you can connect to that socket from your parent thread and Send 0 bytes. You can exit the new stream when the receive methods return 0 bytes.

  • If you are creating a socket on a new thread and it can only be used by the client (connecting to a different socket) then this is not appropriate at all. You may need to abandon the stream (not recommended) unless you configure your server to send 0 bytes when you want to close the client socket, but this way your client application will depend on the server to close the socket.

+3


source


If you use the blocking read () command, you should almost always have another thread of control in charge of closing it and clearing the socket.

Typically, I would use a select () call that expires after 1 second or so to check if there is data to read, and each timeout checks to see if the shutdown status flag was on a different thread.



But if you go with pure locking, use the control flow as you suggest.

+1


source


I would use an Asnyncronous Socket communication. I wrote an article that demonstrates this on my blog. ou can read here:

http://www.andrewrea.co.uk/blog/2009/06/09/Part1SocketProgrammingWithCJAVACAndActionScript30EstablishingABaseConnectionAndCommunicationWithCServerAndAS3.aspx

Andrew

+1


source


I'm somewhat puzzled as to what exactly you are doing: there is no read () method for the Socket class in .NET.

My recommendation is to create a second socket that listens on a specific port and has a Socket.Select flow block instead.Connecting to this second socket should be done as a shutdown request (possibly after proper authentication, for example by sending application password through this socket).

0


source


Another way to do this is to send a 0 byte packet to your auditory socket from somewhere else in your own application if you want to close it.

I found that a slightly cleaner approach than closing the coket from another thread, as an exception will be thrown by the listening thread if you close it on it.

-1


source







All Articles