Danze
Danze

Reputation: 63

Sending and receiveing UDP on the same port

I use X-Plane and it send its output UDP data to 127.0.0.1:49000. The problem rises when my application is supposed to listen to the same port to receive data via boost::asio.

I used the boost sample code at here and here.

// at main function:
udp_server server(49000);

// class implementation:
class udp_server
{
public:
    udp_server(int port) :
        io_service(),
        socket_(io_service, udp::endpoint(udp::v4(), port)) // Exception occurs here
    {
        start_receive();
        udp_thread = std::thread([this](){io_service.run();});
    }

    void start_receive()
    {
        socket_.async_receive_from(
            boost::asio::buffer(recv_buffer_), remote_endpoint_,
            boost::bind(&udp_server::handle_receive, this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));
    }

    // ...

    std::thread udp_thread;

    boost::asio::io_service io_service;
    udp::socket socket_;
    udp::endpoint remote_endpoint_;
    boost::array<char, 5000> recv_buffer_;
};

When I run the code, I receive an exception at the initialization of the socket:

bind: An attempt was made to access a socket in a way forbidden by its access permissions

It make sense, because X-Plane is writing at this port and I read from the same port. Both of us are occupying the same port. Is there any way to overcome this problem? If not, does it mean that two application at the computer cannot transfer message via UDP?

Upvotes: 1

Views: 658

Answers (1)

David Schwartz
David Schwartz

Reputation: 182893

It make sense, because X-Plane is writing at this port and I read from the same port.

X-Plane is sending from that port and receiving to that port. It will write datagrams to (and receive datagrams from) whatever port the programs it has to talk to are receiving on and sending from.

Both of us are occupying the same port. Is there any way to overcome this problem?

Yes, pick a different port to send from and receive on.

If not, does it mean that two application at the computer cannot transfer message via UDP?

They certainly can. Typically, one side picks a well-known port that it sends from and receives on. The other side uses a random port that it sends from and receives on, sending to and receiving from the well-known port.

So the flow is:

  1. X-Plane always binds to a well-known port, 49,000.
  2. You pick a random port, say 31,020. You bind to that port.
  3. You send a datagram from port 31,020 to port 49,000, since that's X-Plane's well-known port.
  4. X-Plane receives a datagram from port 31,020 to port 49,000, the port it is bound to. X-Plane notes that the datagram came from port 31,020.
  5. X-Plane sends a response to port 31,020, the port your program is listening on from port 49,000, the port it is bound to and sends from.
  6. Your program receives a datagram from port 49,000 to port 31,020, the port it is listening on.
  7. Your program knows this is a response from X-Plane because it came from port 49,000.

Upvotes: 1

Related Questions