Search code examples
socketsbittorrentdhtkademlia

Is it possible to receive from one port and send through another in mainline dht?


I'm trying to implement mainline dht. While implementing I found it easier to use multithreading to handle requests and send requests at the same time. But it's impossible for a singular port to both send and receive at the same time. There's two solutions I thought of, one of those would be using different ports for receiving and sending, however in mainline dht it seems like whenever you send a request, nodes will remember you based on the port you send the request on. Is it possible to still implement a different port for receiving and sending?


Solution

  • The DHT requires that the same port is used for sending and receiving.

    But it's impossible for a singular port to both send and receive at the same time.

    Sockets are thread-safe, you can issue send and receive syscalls to the same socket at the same time.

    If you want to load-balance reading across multiple threads you can open multiple sockets bound to the same port via SO_REUSEPORT but that shouldn't be necessary because any regular DHT implementation will only see a dozen packets per second, perhaps with short burts into the thousands, something that a single core can comfortably handle.