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 9017817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:22:06+00:00 2026-06-16T04:22:06+00:00

Combining the information from boost docs and manual for connect function I take it

  • 0

Combining the information from boost docs and manual for connect function I take it that the asio::udp::socket::connect function serves to set the address to which datagrams are sent by default, and the only address from which datagrams are received.

This works nicely but the manual also states that I should be able to dissolve the association (disconnect), which is what I’m having problem doing. I.e. I would like the socket to start receiving packets from any address again.

Further in the manual is written:

Connectionless sockets may dissolve the association by connecting to an
address with the sa_family member of sockaddr set to AF_UNSPEC.

I have tried this code:

asio::udp::endpoint unspecified_endpoint;
assert(unspecified_endpoint.address().is_unspecified()); // OK
socket.connect(unspecified_endpoint);

But that did not help.


EDIT: Created a test case by taking the client and server examples from boost::asio page. In client code, I’ve changed the outgoing port number from random to 5192:

udp::socket s(io_service, udp::endpoint(udp::v4(), 5192));

And I changed the function server in the server code:

void server(boost::asio::io_service& io_service, short port)
{
  udp::socket sock(io_service, udp::endpoint(udp::v4(), port));

#define CASE 3

#if CASE == 1
  sock.connect(udp::endpoint());
#elif CASE == 2
  sock.connect(udp::endpoint(ip::address::from_string("127.0.0.1"), 5193));
  sock.connect(udp::endpoint());
#elif CASE == 3
  sock.connect(udp::endpoint(ip::address::from_string("192.168.1.3"), 5192));
  sock.connect(udp::endpoint());
#endif

  for (;;)
  {
    char data[max_length];
    udp::endpoint sender_endpoint;
    size_t length = sock.receive_from(
    boost::asio::buffer(data, max_length), sender_endpoint);
    sock.send_to(boost::asio::buffer(data, length), sender_endpoint);
  }
}

Cases 1 and 2 seem to work, but in case 3 the client wont receive a reply. The full source can be found here.

  • 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-06-16T04:22:08+00:00Added an answer on June 16, 2026 at 4:22 am

    As mentioned in the comments, the call to sock.connect(udp::endpoint()); translates to connect(6, {sa_family=AF_INET, sin_port=htons(0), sin_addr=inet_addr("0.0.0.0")}, 16) = 0 which isn’t the same as a connect with AF_UNSPEC and doesn’t completely disconnect the UDP socket (it appears to only reset the peer port number).

    I don’t think there is a way to achieve what you want purely with boost asio, but you could just use a native POSIX call to disconnect the socket, something like

    struct sockaddr unspec_addr = { AF_UNSPEC };
    ::connect(sock.native_handle(), &unspec_addr, sizeof(unspec_addr));
    

    Debugging notes: run the program with strace and then use netstat -nu to list the UDP sockets.

    • 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 just combining css files from other templates but I can figure out why
I' ve tried combining the module pattern with a constructor function but it seems
Delphi documentation says that Debug information [...] it does not affect the size or
I'm writing a socket program to download images. The problem is that when I
I got the following string to extract some information from: String: String: String Number;
I tried using the answer involving git-filterbranch from this question Combining multiple git repositories
I'm writing a sinatra app in ruby that gathers information about my network into
I have an Access 2010 database with information from Geonames. The table name is
Judging from the information I have read about type providers so far, I wonder

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.