开发者

Python UDP socket options for multiple & concurrent clients

Let me explain a bit the app i'm doing.

I'm creating a central UDP (needs to be UDP) server for multiple and concurrent clients that also "talk" between them. I do a check into a dict of known clients addresses and create a client handler thread if "i dont know" the client. Else, the thread receives the data ad does its job. The new thread receives a reference to the socket and the caller address, so they use socket.sendTo with that address and the data.

The problem appears when a client closes it's client (Alt+F4) and someone talks to em, cause the socket throws a 10054 error, "Socket connection reset". Not expected in UDP, i think. The "talk" method on the client threads is also between try & except tags but still, is the UDP server "recvfrom" the one that triggers the exception.

I am using Python 2.5 (need 开发者_开发百科it for the code) and this socket options:

host = "0.0.0.0"
port = 10000

UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
UDPSock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
UDPSock.bind((host,port))

There are some options that let me do what I wanted or a library that solves this things?.

Or If i am doing it wrong... there is a way to do/emulate the same behavior as threaded TCP socket (create a new client handler thread on accept) but with UDP?

Thanks for reading ;)


The 'socket reset' happens because the client sent an ICMP port-unreachable in response to the datagram sent to a socket that wasn't there any more. Perfectly reasonable way to express that in the API, so you're just going to have to handle it. But if you just ignore the exception, the socket should still be working.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜