I have a server and a client application, here’s how it’s currently implemented: The server has a loop, in its own thread, that listens for new connections through a TcpListener. If someone connects, it passes the TcpClient to a loop that listens to that TcpClient specifically, and handles all interaction with that user through NetworkStream.
On the client, I have a TcpClient that connects to the server. It’s my understanding that I need to branch it to a different thread, since any IO will put it in to blocking mode, and I can’t let it stop the main thread.
So far, am I understanding it correctly? Is there an easier way to asure asynchronous communication without stopping the main thread for either program?
Either way, for my main question: How do I make sure all of these threads and sockets are properly disposed of? If a program exits while it’s still in blocking mode, it seems to freeze. So how do I abort all IO on the socket and tell the infinite loop in the child threads that we’re done?
This is also a Unity3d project by the way, in case that changes anything.
Handing off each new connection to its own thread could limit your scalability down the line. Also there’s no reason to require thread handoff for I/O in the client.
You can resolve both issues by using async I/O.
BeginConnect/EndConnecton the client,BeginRead/EndRead, BeginWrite/EndWriteon the server side for reads and writes.Then everything can be event-driven without any need for new threads of your own. Connect completion triggers first async read, then each read completion posts a new async read call on that socket – writes are always deterministic from your POV anyway.
Closing your sockets will results in pending reads/writes exiting out with an error, and then you should be able to exit the process cleanly. Be careful that you implement appropriate locking (likely
lock()will be all you need) to ensure sockets are not used anywhere after they have been closed.