Opened 5 years ago

Last modified 5 years ago

#13463 new Bugs

Boost UDP multicast sender not using correct port

Reported by: anonymous Owned by: chris_kohlhoff
Milestone: To Be Determined Component: asio
Version: Boost 1.66.0 Severity: Showstopper
Keywords: asio udp multicast sender Cc:

Description

I follow this code to create a UDP multicast receiver: http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/example/cpp11/multicast/sender.cpp

I modify the code with these for predefined values:

class receiver:

short multicast_port = 13000;

in main():

if (argc != 2) and the code for argv that follows are commented out

sender s(io_context, boost::asio::ip::make_address("192.168.0.255"));

The first 3 octets match that of the local network. I check with Wireshark. It shows me that the packages are sent with source port of 53916, and destination port 0. Running the code again gives a source port of 59483, and destination port 0.

What is causing the source port to be different from specified value? How can I force it to use the specified port number? Thanks.

Attachments (1)

UdpMulticastSender_1_66.cpp (3.8 KB ) - added by anonymous 5 years ago.

Download all attachments as: .zip

Change History (4)

by anonymous, 5 years ago

Attachment: UdpMulticastSender_1_66.cpp added

comment:1 by anonymous, 5 years ago

I have previously tested the unicast UDP time server (http://www.boost.org/doc/libs/1_65_0/doc/html/boost_asio/tutorial/tutdaytime6/src.html) and client (http://www.boost.org/doc/libs/1_65_0/doc/html/boost_asio/tutorial/tutdaytime4/src.html) provided sample codes. They work, and the port specified is correctly observed with Wireshark. Could someone tell me what went wrong with this UDP multicast sender?

comment:2 by nguyen.tnhoang@…, 5 years ago

OK, I manage to narrow down the error, but not sure how it happens. In the sender() class constructor:

sender(boost::asio::io_context& io_context, const boost::asio::ip::address& multicast_address)

: endpoint_(multicast_address, multicast_port), socket_(io_context, endpoint_.protocol()), timer_(io_context), message_count_(0)

I change the line:

socket_(io_context, endpoint_.protocol()),

into

socket_(io_context, udp::endpoint(udp::v4(), 13000)),

It works properly. Wireshark shows the packages sent with that specified source port of 13000. However, if I change it to:

socket_(io_context, udp::endpoint(udp::v4(), multicast_port)),

It still won't work, despite multicast_port being specified as 13000. Please help. I need the source port to be dynamically set. Thank you.

comment:3 by anonymous, 5 years ago

I change the order of the private members in the code:

private:

unsigned short multicast_port = 13000; 30001 int max_message_count = 10; boost::asio::ip::udp::endpoint endpoint_; boost::asio::ip::udp::socket socket_; boost::asio::steady_timer timer_; int message_count_; std::string message_;

now everything works as intended. Please close the ticket.

Note: See TracTickets for help on using tickets.