Boost logo

Boost-Build :

Subject: [Boost-build] asio send broadcast
From: Vvovchenko (vvovchenko_at_[hidden])
Date: 2012-11-06 14:15:35


Hello!!!

I wrote simple application which send messages over the network using
boost::asio.

When I execute this application on ubunt-desktop everythink fine. But when I
execut it on ubuntu-server broadcast messages are not send (It receive it
and send answers to it, but don't send broadcast messages)

Code of my application:
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/array.hpp>

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

class udp_server {
public:
        // Server constructor, start listening port 5000 and sends broadcast
        // message
        udp_server(boost::asio::io_service& io_service)
                : socket_(io_service, udp::endpoint(udp::v4(), 5000))
        {
                socket_.set_option(udp::socket::broadcast(true));

                start_receive();

                broadcast("broadcast_message");
        }
private:
        // starts receive messages, when message is received function
        // udp_server::handle_receive is called
        void start_receive()
        {
                try {
                        socket_.async_receive_from(
                                boost::asio::buffer(recv_buffer_),
                                remote_endpoint_,
                                boost::bind(&udp_server::handle_receive, this,
                                        boost::asio::placeholders::error,
                                        boost::asio::placeholders::bytes_transferred));
                } catch (std::exception& e) {
                        std::cerr << "udp_server::start_receive: " << e.what() << std::endl;
                }
        }

        // sends broadcast message
        void broadcast(std::string msg)
        {
                try {
                        boost::shared_ptr<std::string> message(new std::string(msg));
        
                        socket_.async_send_to(boost::asio::buffer(*message),
                                udp::endpoint(boost::asio::ip::address_v4::broadcast(),
                                5000), boost::bind(&udp_server::handle_send,
                                this, message, boost::asio::placeholders::error,
                                boost::asio::placeholders::bytes_transferred));
                } catch (std::exception& e) {
                        std::cerr << "udp_server::broadcast: " << e.what() <<
                                std::endl;
                }
        }

        // This function is called when message is received.
        // Sends reply message to any incomming message (except "Server reply")
        void handle_receive(const boost::system::error_code& error,
                                std::size_t bytes_transferred)
        {
                if (!error || error == boost::asio::error::message_size) {

                        std::string recv_str(recv_buffer_.data(),
                                                        bytes_transferred);
                        std::cout << "handle_receive: from " <<
                                remote_endpoint_.address().to_string() <<
                                " recv_buffer_ = \"" << recv_str << "\"";

                        if (recv_str != std::string("Server reply")) {
                                std::cout << ", sending \"Server reply\"";

                                try {
                                        boost::shared_ptr<std::string> message(
                                             new std::string("Server reply"));

                                        socket_.async_send_to(
                                                boost::asio::buffer(*message),
                                                remote_endpoint_,
                                                boost::bind(
                                                    &udp_server::handle_send,
                                                    this, message,
                                                    boost::asio::placeholders::error,
                                                    boost::asio::placeholders::bytes_transferred));

                                } catch (std::exception& e) {
                                        std::cerr << "udp_server::handle_receive: " << e.what() << std::endl;
                                }
                        }
                        std::cout << std::endl;

                        start_receive();
                }
                else
                        std::cerr << "udp_server::handle_receive: " << error.message() <<
std::endl;
        }

        // This function called when message is send
        void handle_send(boost::shared_ptr<std::string> message,
                const boost::system::error_code& error,
                std::size_t bytes_transferred)
        {
                std::cout << "handle_send: error: \"" << error.message() <<
                        "\" message = \"" << *message <<
                        "\", bytes_transferred = " << bytes_transferred <<
                        std::endl;
        }

        udp::socket socket_;
        udp::endpoint remote_endpoint_;
        boost::array<char, 128> recv_buffer_;
};

int main()
{
        try {
                boost::asio::io_service io_service;
                udp_server server(io_service);
                io_service.run();
        }
        catch (std::exception& e) {
                std::cerr << e.what() << std::endl;
        }

        return 0;
}

Output on ubuntu-desktop(192.168.1.2):
handle_send: error: "Success" message = "broadcast_message",
bytes_transferred = 17
handle_receive: from 192.168.1.2 recv_buffer_ = "broadcast_message", sending
"Server reply"
handle_send: error: "Success" message = "Server reply", bytes_transferred =
12
handle_receive: from 192.168.1.2 recv_buffer_ = "Server reply"
handle_receive: from 192.168.1.3 recv_buffer_ = "broadcast_message", sending
"Server reply"
handle_send: error: "Success" message = "Server reply", bytes_transferred =
12

Output on ubuntu-server(192.168.1.1):
handle_send: error: "Network is unreachable" message = "broadcast_message",
bytes_transferred = 0
handle_receive: from 192.168.1.3 recv_buffer_ = "broadcast_message", sending
"Server reply"
handle_send: error: "Success" message = "Server reply", bytes_transferred =
12

Output on ubuntu-desktop(192.168.1.3):
handle_send: error: "Success" message = "broadcast_message",
bytes_transferred = 17
handle_receive: from 192.168.1.3 recv_buffer_ = "broadcast_message", sending
"Server reply"
handle_send: error: "Success" message = "Server reply", bytes_transferred =
12
handle_receive: from 192.168.1.3 recv_buffer_ = "Server reply"
handle_receive: from 192.168.1.1 recv_buffer_ = "Server reply"
handle_receive: from 192.168.1.2 recv_buffer_ = "Server reply"

I'm using ubuntu 10.04.
Server configuration:
# uname -a
Linux astorex 2.6.32-44-server #98-Ubuntu SMP Mon Sep 24 17:41:33 UTC 2012
x86_64 GNU/Linux

# ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:42:a6:6f
          inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe42:a66f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
          RX packets:10624 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1619 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:6505617 (6.5 MB) TX bytes:134671 (134.6 KB)

# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Is anybody know what's wrong?

--
View this message in context: http://boost.2283326.n4.nabble.com/asio-send-broadcast-tp4638287.html
Sent from the Boost - Build mailing list archive at Nabble.com.

Boost-Build list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk