Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1092549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:41:58+00:00 2026-05-16T23:41:58+00:00

This boost udp server doesn’t function as expected. It is identical to the blocking

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:41:58+00:00Added an answer on May 16, 2026 at 11:41 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using boost::asio to create a server application, specifically the UDP APIs. Right
I'm reading from a boost::asio::ip::udp::socket like this: using boost::asio::ip::udp; // ... char recv_buf[128]; udp::endpoint
this is my boost::asio server class Server: public boost::enable_shared_from_this<Server>, private boost::noncopyable{ private: boost::asio::ip::tcp::acceptor _acceptor;
Unlike this question: Linker Error while building application using Boost Asio in Visual Studio
I just tested something like this: boost::thread workerThread1(boost::bind(&Class::Function, this, ...); boost::thread workerThread2(boost::bind(&Class::Function, this, ...);
Assume I have a function like this: static const boost::int32_t SOME_CONST_VALUE = 1073741823; template<typename
Suppose I have code something like this: #include boost/thread/mutex.hpp using boost::mutex; typedef mutex::scoped_lock lock;
i'm trying to make an asynchronous UDP Client with boost::asio, the Server is from
I am using boost::asio::ip::udp::socket to communicate. I use socket.receive_from(...) to receive messages from clients.
I would like to pass a lambda to a funciton. This boost::function<void()> fncPtr(boost::bind<void>([](){/* something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.