简体   繁体   中英

Python UDP socket send and receive data at the same time

I'm creating a simple socket server using UDP in Python and I want to add some notion of reliability by having the receiver send ACKs. My sender will have to send data and receive these ACKs at the same time. What is the preferred way of doing this? Should I make both a sending and receiving thread, use some kind of nonblocking calls, or something else?

Any help would be greatly appreciated!

There are a couple solutions for this:

  • The simplest solution is in stdlib, the SocketServer module which has a UDPServer - you can set handlers for different message types and enable your state machine that way. There's also a bunch of nifty mixins for threads and other behaviors.

  • Third-party module gevent which uses libevent for async network programming is another solution.

  • You could write something at the raw socket level with scapy but that puts all the heavy lifting on you, although there are undoubtedly examples of how to do what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM