views:

56

answers:

1

This boost udp server doesn't function as expected.

It is identical to the blocking UDP echo server EXCEPT for a minor change. I'm using a different socket to return the response, i.e. sock2. Now this code works perfectly if the client is on the same machine as the server. But the moment I run this on a different machine, the response is not received. However, if I change the sending socket to be sock rather than sock2 it does work across machines.

Any idea why that would be? wireshark is not showing any errors at all. Client uses random source port, but then calls recv_from on the same random port. Server sends response back to that same port number that the client then listens on.

#include <cstdlib>
#include <iostream>
#include <boost/asio.hpp>

using boost::asio::ip::udp;

enum { max_length = 1024 };

void server(boost::asio::io_service& io_service, short port)
{
  udp::socket sock(io_service, udp::endpoint(udp::v4(), port));
  udp::socket sock2(io_service, udp::endpoint(udp::v4(), 0));
  for (;;)
  {
    char data[max_length];
    udp::endpoint sender_endpoint;
    size_t length = sock.receive_from(
        boost::asio::buffer(data, max_length), sender_endpoint);
    printf("Got data, sending response to %s:%d\n", sender_endpoint.address().to_string().c_str(), sender_endpoint.port());
    sock2.send_to(boost::asio::buffer(data, length), sender_endpoint);
  }
}

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: blocking_udp_echo_server <port>\n";
      return 1;
    }

    boost::asio::io_service io_service;

    using namespace std; // For atoi.
    server(io_service, atoi(argv[1]));
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}
A: 

Answering my own question again!

Check there is no firewall software running on the other machine!. It turns out that the windows machine running in the VM was running a windows firewall.

I didn't immediately suspect this because when I send the UDP response on the original socket using the port number passed into the server, it worked.

I guess the windows firewall is maintaining some state.

Matt H