Reusing a port number in a UDP

前端 未结 3 751
臣服心动
臣服心动 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.

提交回复
热议问题