Hey all,

I've been experimenting with a basic chat server/client from my Python textbook, in efforts to keep the code clean and simple I think some optional code was omitted and it's made the books example a little buggy.

I've been having troubles with the line:

transmission = self.request.recv(1024)

This is server side, awaiting information from a client.

The problem I'm having is if a client connects, sends chat for a while and then disconnects in a non expected way (terminate the process, stop the execution of code, internet cuts out, etc) the server crashes.

So I believe this should be wrapped in a try

try:
    transmission = self.request.recv(1024)
except: #should this be a specific exception only, or a general one
    #What should I put here?

Any help on that last code snippet would be appreciated. Thanks!

Since the server crashed, it displayed the exception, probably a socket.error, so use except socket.error . In except block, put the code that you want to execute when the client disconnects in an unexpected way (it could be a simple pass statement). Also, if these lines are in a loop which reads from the client, you must exit that loop once you know that the error happened.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.