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

  • SEARCH
  • Home
  • 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 3598340
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:13:47+00:00 2026-05-18T20:13:47+00:00

I need to get the source IP for a datagram in a UDP server

  • 0

I need to get the source IP for a datagram in a UDP server I’m writing using boost ASIO.
In the example udp datagram server the line:

Note: my current code is identical to the existing udp async server example in the boost asio documentation.

 socket_.async_receive_from(
        boost::asio::buffer(data_, max_length), sender_endpoint_,
        boost::bind(&server::handle_receive_from, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));

Is passing error and bytes_transferred. I also need the address of the message.

Now I know I can use sender_endpoint_.address().to_string() directly, but I
I would like to pass this in as a parameter.

So I tried placing sender_endpoint_.address() in as a parameter.
i.e.

socket_.async_receive_from(
        boost::asio::buffer(data_, MAX_LENGTH), sender_endpoint_,
        boost::bind(&server::handle_receive_from, this,
          boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred,
                    sender_endpoint_.address()));

But the result is 0.0.0.0 when read from handle_receive_from.
However, if I simply access the sender_endpoint_.address() inside handle_receive_from
I get 127.0.0.1 which is correct.

How do pass this correctly? I need this because there are going to be multiple threads calling io_service.run() so a packet from another source may overwrite sender_endpoint and I need to be able to reply to that source packet.

  • 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-18T20:13:48+00:00Added an answer on May 18, 2026 at 8:13 pm

    I think that if you access sender_endpoint_.address() in your handler before you set up another async handler, it will be fine and not overridden. You do call async_receive_from again in your handler, right, to be ready to receive another packet?

    In your second code block, where you try sender_endpoint_.address() as a parameter, sender_endpoint_.address() is getting called when you set up the handler, not being executed by the handler when a packet is received. That’s why it’ll always be 0.0.0.0.

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

Sidebar

Related Questions

I need to get the source IP for a datagram in a UDP server
I am writing java code where i need to get some information from a
I need to get Class Model name from #django.db.models.fields.related.ManyToManyField object.For example: class Source(models.Model): groups
In php I need to get the contents of a url (source) search for
I need to get the default printer name. I'll be using C# but I
I need to get a log of user access to our SQL Server so
I need to get into FluentNhibernate and NHibernate code so i rebuild the solutions
I really need to get started with source control because I'm going absolutely crazy
i have been trying to get the source of a page in php using
I am trying to apply this bug fix http://code.google.com/p/selenium/source/detail?r=11856 to the selenium-server-standalone-2.0b3.jar. Does anyone

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.