   Socket Server/Client Question
  I have a question regarding sockets with VC++.  I have a client application which also needs to send commands/messages to the server application.  Should I create two sockets in my client app, one for getting data and one for sending data?  I've seen examples where the same socket is used for both tasks. Or should I just protect the socket with a critical section?  Thanks.   
  If your messages are synchronous, a single socket will do.  If they're asynchronous (that is, the server could send at any time and the client could send at any time so that, for example, a new server message could arrive while the client is waiting for a reply to the one it just sent), then two sockets are an answer.If you're communicating over a network, there's no kind of synchronization primitive you can use.Your best bet is to structure the protocol so that only one end ever initiates message exchanges (see, for example, the HTTP protocol).--smw   ;Quote:Original post by BregmaIf you're communicating over a network, there's no kind of synchronization primitive you can use.I believe he's thinking about either a token or a handshake.The server and client would pass a token message, where when one had the token, it would be allowed to pass messages.Or else the server / client would do a 3-way handshake (SYN/ACK/ACK) before passing a message.   ;  A single socket is fine; the network layer will take care of sorting everything out for you (assuming you're using TCP, which is highly advisable). A socket is already basically two streams (one going each direction) so there's no need to create a second.A common way to make sure you don't lose track of things is to divide all of your data into request/response pairs. When the client or server sends a request, it generates an ID number for the request, and then sends the ID and the request data over the socket. When the other side handles the request, it sends a reply back with the same ID number and the reply data. This way, both sides always know what is going on, and you don't have to worry about forgetting what your sockets are doing.   ;  Thanks for the replies.   