0

I have 2 python programs: 1st one is a tcp server that in while True cycle accepts new clients and handles each of them in a separate thread. On the client side I have basic functionality and use it for sending and receiving simple messages. The thing that I don't understand - it is how I can close client socket properly.

For now I'm using this code to close my client socket:

 def __on_s_close(self):
 try:
 self.__socket.shutdown(socket.SHUT_RDWR)
 except socket.error as e:
 logger.error(f"error while shutting down: {e}")
 finally:
 self.__socket.close()
 self.__socket = None
 logger.debug("client terminated from client module!")

As I understood, in the Server module I accept a client socket object, so I can shutdown and close it from the server side. My questions are:

  1. Where do I need to close the client socket - in the server module or in client module? What's the difference?
  2. If I close it in server module, what happens to the socket object in the client module? It becomes None Type? Or stays the same?
  3. If I want reconnect client to the server, do I need to create a new instance of a socket class, or I can use previous one?
  4. What happens after socket.close()? Also, as I understood, even after I executed this method on the client side, due to while True cycle my program still can reach socket.recv() function and run into errors. But when I execute socket.close() on the server side - I don't get such error on the client side. Is there any way to close socket gracefully on the client side?(excluding try/except)

Thank you for your help.

asked Jun 23, 2025 at 5:51
7
  • Where's the problem? If you have an open socket that you no longer need, just close it. Maybe your problem is more about telling the server that there are no longer any clients. Commented Jun 23, 2025 at 6:23
  • The problem is that I don't know on which side I need to do it, and what happens to the socket object on the other side. And about telling the server: so if I want to disconnect, I need to send message to a server and close client socket on the server side? Do I need to do this on the client side afterwards? Commented Jun 23, 2025 at 6:33
  • And if I want to reconnect, do I need to create a new socket object every time? Commented Jun 23, 2025 at 6:34
  • If you've closed the client socket you can't reuse it so, yes, you'll need to create another one Commented Jun 23, 2025 at 7:09
  • okay, thanks. But if I close user socket on the server side, do I need to close it on the user side as well? or I just can equal it to None: my_socket = None? Commented Jun 23, 2025 at 11:53

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.