Use the same udp socket to receive / send async

I use the same socket on my udp server to receive data from clients on some port, and later, after processing requests, it responds to clients using ip :: ud :: socket :: async_send_to

Receive is performed by async using async_receive_from. The socket uses the same ioService (this is the same socket after all) The documentation does not clearly indicate whether at the same time the same udp socket can receive datagrams from client A (asynchronously) and possibly send another datagram to the client B (asynchronous sending) at the same time I suspect this may cause problems. I ended up using the same socket for the answer, because I could not bind another socket to the same server port, responding to another client.

How to associate another socket with the same server?

EDIT . I am trying to associate a second udp socket with the same UDP port:

socket(ioService, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), port))

When I do this for the first time (binding to the server "receive"), it works fine, but tries to create another socket a second time, as if it reports an error while binding (asio throws exception)

+5
source share
1 answer

It is possible to simultaneously receive a UDP socket from one remote endpoint and send it to another remote endpoint. However, in the Boost.Asio Threads and Boost.Asio documentation , it is generally unsafe to make concurrent calls for a single object.

Thus, it is safe:

thread_1 | thread_2
--------------------------------------+---------------------------------------
socket.async_receive_from( ... );     |
socket.async_send_to( ... );          |

:

 thread_1                             | thread_2
--------------------------------------+---------------------------------------
socket.async_receive_from( ... );     |
                                      | socket.async_send_to( ... );

:

 thread_1                             | thread_2
--------------------------------------+---------------------------------------
socket.async_receive_from( ... );     | socket.async_send_to( ... );
                                      |

, , boost::asio::async_read, .


, , :

  • , io_service::run() .
  • async_receive_from async_send_to . , ReadHandler, async_receive_from, async_send_to, WriteHandler, async_send_to, async_receive_from.

    void read()
    {
      socket.async_receive_from( ..., handle_read );  --.
    }                                                   |
        .-----------------------------------------------'
        |      .----------------------------------------.
        V      V                                        |
    void handle_read( ... )                             |
    {                                                   |
      socket.async_send_to( ..., handle_write );  --.   |
    }                                               |   |
        .-------------------------------------------'   |
        |                                               |
        V                                               |
    void handle_write( ... )                            |
    {                                                   |
      socket.async_receive_from( ..., handle_read );  --'
    }
    

, , , . boost:: asio:: io_service:: strand , Boost.Thread mutex.


. , buffer endpoint request- > process- > . async_receive_from . , boost:: shared_ptr. , , , , .


, socket_base::reuse_address , . , , , :

  • TCP , TIME_WAIT.
  • UDP , .
+14

All Articles