Reusing a port number in a UDP

前端 未结 3 746
臣服心动
臣服心动 2021-01-24 01:39

In ASIO, s it possible to create another socket that has the same source port as another socket?

My UDP server application is calling receive_from using port 3000. It p

相关标签:
3条回答
  • 2021-01-24 02:06

    The exception tells you as it is: you can't create two live sockets with the same source port. I don't know ASIO, but you should be able to create the socket before spinning off the thread, keeping reference to the socket and the thread for later use, and once the data sending thread is idle, joining back to it and sending any other stuff.

    EDIT: with a little bit of effort, you can also make a socket for which you don't have to wait until the entire data from one thread has been sent: have a worker thread owning the socket listen on a queue for chunks of data (ideally exactly the size of the payload you intend to send) and send arbitrary chunks of payload to this queue, from multiple threads.

    0 讨论(0)
  • 2021-01-24 02:10

    send reply to the same socket (that you received client's request on) instead of creating new one but make sure you don't send to the same socket from both threads simultaneously

    0 讨论(0)
  • 2021-01-24 02:26

    You should be able to use the SO_REUSEADDR socket option to bind multiple sockets to the same address. But having said that, you don't want to do this because it's not specified which socket will receive incoming data on that port (you would have to check all sockets for incoming data)

    The better option is just to use the same socket to send replies - this can safely be done from multiple threads without any additional synchronisation (as you are using UDP).

    0 讨论(0)
提交回复
热议问题