开发者

can more than one thread use the same port

i'm trying to write a multithreaded server in python which can recieve multiple client requests then send the answer in a UDP packet (port 53). I was wondering if all these threads can use 53 at the same time. t开发者_JS百科hanks a lot (this question might seem silly, i'm a beginner)


If you specify the 'reuse port' option, then yes you can bind multiple listening sockets to the same port. But in my experience even when doing that, only one of the sockets will receive any given packet (unless you're receiving multicast packets).

mySocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)

Normally though, when you talk about a 'multithreaded server', you're talking about a server using a 'connection oriented' protocol (over TCP, not UDP) where there is a server socket listening for connections, and then when a connection is formed, it creates a thread to deal with it.

For a multithreaded UDP server, you would probably have a single socket which queues incomming requests, and have multiple threads pulling from the queue and doing the work. The threads themselves wouldn't need to deal with the socket.

(Note: Depending on your platform, you may have to use SO_REUSEADDR instead. Some info here.)


Please don't. Use Twisted instead. It's already got all that sort of stuff done.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜