Reputation: 63
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
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:
Upvotes: 1