02-15-2011 11:27 AM
I am using a TCP/IP connection to communcate between executables. The communication is not constant, but it may occur every 10 seconds or in some situations it may occur every 1 second. The communication between the executables can be summed up like this:
1. The server is listening for connections.
2. The client opens a connection sends a command and possible some data to the server.
3. The server responds to the client to complete the communcation, possibly with some data but not nececarilly.
So I am unsure about whether I should close this connection after step 3, or should I keep it alive and have the server constatly reading until the next command and data is sent from client. Or should I close the connection and then just listen for a new client initiated connection again. Thanks for the help
Anthony
02-15-2011 01:13 PM
I recommend that you keep the connection open. You may want to move the TCP handling into a separate loop (if it's not there already) and then whenever data is received, put it into a queue where it can be handled by the rest of your code. Creating a new connection every time you want to send some data in either direction is not very efficient.
02-15-2011 02:46 PM
Okay, that makes sense. Thanks
02-16-2011 05:51 PM
Yes, keep it open but you may want to add some code to re-connect if the connection is lost.