how to do nonblocking recvfrom in windows?

how to do nonblocking recvfrom in windows?

Postby jonturner53 » Mon May 13, 2013 9:32 pm

The following code snippet attempts to read a datagram from a nonblocking socket.
If no packet is available, we generally get an EAGAIN exception and return None.
If there is a packet, we skip the exception and proceed to the code that
handles the datagram content (not shown).
Code: Select all
try :
    buf, senderAdr = self.sock.recvfrom(2000)
except error as e :
    if e.errno == errno.EAGAIN : return None
    # treat anything else as fatal error

This code was written for Unix/Linux and works fine on my Mac or a Linux machine.
I would like to know how to do the same thing on a windows machine. I've tried
substituting errno.EWOULDBLOCK, since it appears that Windows uses this
error code. But it also appears that there may be more hoops to jump through
to get this to work on Windows.

Any suggestions?

Jon Turner
Posts: 1
Joined: Mon May 13, 2013 9:19 pm

Re: how to do nonblocking recvfrom in windows?

Postby setrofim » Tue May 14, 2013 7:22 am

Have you tried sock.setblocking(False)?
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: how to do nonblocking recvfrom in windows?

Postby gratuitous_arp » Fri May 17, 2013 1:38 pm

Another option is to use select to see when the socket has data to read. The Python socket programming howto has a useful section on non-blocking sockets: ... ng-sockets
Posts: 6
Joined: Tue Apr 16, 2013 11:10 pm

Return to Networking

Who is online

Users browsing this forum: No registered users and 1 guest