Cam Jackson
Cam Jackson

Reputation: 12294

What is the correct procedure for multiple, sequential communications over a socket?

I've been struggling along with sockets, making OK progress, but I keep running into problems, and feeling like I must be doing something wrong for things to be this hard.

There are plenty of tutorials out there that implement a TCP client and server, usually where:

That I can handle. However, no one seems to go into the details of what you should and shouldn't be doing with sequential communication between the same two machines/processes.

I'm after the general sequence of function calls for doing multiple messages, but for the sake of asking a real question, here are some constraints:

and some specific questions:

  1. Should the server be closing the connection after its response, or trying to hang on to the connection until the next communication?
  2. Likewise, should the client close the connection after it receives the response, or try to reuse the connection?
  3. Does a closed connection (either through close() or through some error) mean the end of the communication, or the end of the life of the entire object?
    1. Can I reuse the object by connecting again?
    2. Can I do so on the same port of the server?
    3. Or do I have reinstantiate another socket object with a fresh call to socket.socket()?
  4. What should I be doing to avoid getting 'address in use' errors?
  5. If a recv() times out, is the socket reusable, or should I throw it away? Again, can I start a new connection with the same socket object, or do I need a whole new socket?

Upvotes: 3

Views: 931

Answers (1)

xubuntix
xubuntix

Reputation: 2333

  1. If you know that you will communicate between the two processes soon again, there is no need for closing the connection. If your server has to deal with other connections as well, you want to make it multithreaded, though.
  2. The same. You know that both have to do the same thing, right?
  3. You have to create a new socket on the client and you can also not reuse the socket on the server side: you have to use the new socket returned by the next (clientsocket, address) = serversocket.accept() call. You can use the same port. (Think of webservers, they always accept connections to the same port, from thousands of clients)

In both cases (closing or not closing), you should however have a message termination sign, for example a \n. Then you have to read from the socket until you have reached the sign. This usage is so common, that python has a construct for that: socket.makefile and file.readline

UPDATE:

  1. Post the code. Probably you have not closed the connection correctly.
  2. You can call recv() again.

UPDATE 2: You should never assume that the connection is reliable, but include mechanisms to reconnect in case of errors. Therefore it is ok to try to use the same connection even if there are longer gaps. As for errors you get: if you need specific help for your code, you should post small (but complete) examples.

Upvotes: 2

Related Questions