Handle larger frames than is the size of buffer

Viewed 312

I created a UDP communicator. If I receive a message of size larger than recv() accepts, then I want to handle the exception:

OSError: [WinError 10040] A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself.

How can I do that? Is it safe to handle WinError like:

try:
    message, comm_point = self.clientSocket.recvfrom(MSG_SIZE)
except WinError:
    ...do some job

I know, that WinError.h is a quite large library and it seems to me, that at the time of the try block I may handle some other WinErrors, if the system invokes them...

1 Answers

It seems to me that you are probably using a socket. In that case you don’t have to handle the specific OSError if you handle the socket error. You could do something like:

try:

        message, comm_point = self.clientSocket.recvfrom(MSG_SIZE)

except socket.error, e:
        #Do some job ex.
        print "Error receiving data: %s" % e
        sys.exit(1)

This way the socket will raise an error and you’ll catch it whilst still getting other errors that have nothing to do with your socket, but are OSErrors

Reference, for more help: https://www.networkcomputing.com/data-centers/python-network-programming-handling-socket-errors

Related