Boost logo

Boost :

Subject: [boost] Fw: [asio] multicast.
From: John Maddock (boost.regex_at_[hidden])
Date: 2011-11-21 07:18:02

Message forwarded from original sender:



I am trying to join multicast group via _VPN_, this means that I cannot see
listen-address in ipconfig actually
Could I please me explain how should I connect in this case? I am using
multicast-reciever sample.
with port 9023, listen, multicast

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include "boost/bind.hpp"

const short multicast_port = 9023;

class receiver
  receiver(boost::asio::io_service& io_service,
      const boost::asio::ip::address& listen_address,
      const boost::asio::ip::address& multicast_address)
    : socket_(io_service)
    // Create the socket so that multiple may be bound to the same address.
    boost::asio::ip::udp::endpoint listen_endpoint(
        boost::asio::ip::address::from_string(""), multicast_port);

 //boost::asio::ip::udp::endpoint listen_endpoint(
      // listen_address, multicast_port);//if I use listen_address instead
of, there is an error in socket_.bind(listen_endpoint);;

    // Join the multicast group.

        boost::asio::buffer(data_, max_length), sender_endpoint_,
        boost::bind(&receiver::handle_receive_from, this,

  catch (std::exception& e)
    std::cerr << "Exception: " << e.what() << "\n";

  void handle_receive_from(const boost::system::error_code& error,
      size_t bytes_recvd)
    if (!error)
      std::cout.write(data_, bytes_recvd);
      std::cout << std::endl;

          boost::asio::buffer(data_, max_length), sender_endpoint_,
          boost::bind(&receiver::handle_receive_from, this,

  boost::asio::ip::udp::socket socket_;
  boost::asio::ip::udp::endpoint sender_endpoint_;
  enum { max_length = 1024 };
  char data_[max_length];

int main(int argc, char* argv[])

   std::string list = "";
   std::string mt = "";
   boost::asio::io_service io_service;
    receiver r(io_service,

  catch (std::exception& e)
    std::cerr << "Exception: " << e.what() << "\n";

  return 0;

But with native c++ I can obtain data:
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <mswsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")

int main(){
    WSAData data;
    WSAStartup( MAKEWORD( 2, 2 ), &data );

    int sd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);

    sockaddr_in Sender;
    int SenderAddrSize = sizeof( Sender );

    struct sockaddr_in binda;

char *arg1 = new char[100];
char *arg2 = new char[100];
char *arg3 = new char[100];

arg1 = "";
arg2 = "";
arg3 = "9023";

    binda.sin_family = AF_INET;
    binda.sin_port = htons( atoi(arg3));
    binda.sin_addr.s_addr = htonl(INADDR_ANY);
    bind(sd,(struct sockaddr*)&binda, sizeof(binda));

struct ip_mreq_source imr;
   imr.imr_multiaddr.s_addr = inet_addr(arg1);
   imr.imr_sourceaddr.s_addr = inet_addr(arg2);
   imr.imr_interface.s_addr = INADDR_ANY;
   setsockopt(sd, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, (char *) &imr,
char buf[65536];

 while(1) {
        int res=recvfrom(sd,(char*)buf,sizeof(buf),0, (SOCKADDR *)& Sender,
         } //WORKS


I cannot realize what is the difference in these two samples,

Could you please tell me in which way should I use boost library to obtain
data in this case?

Thanks in advance,


Boost list run by bdawes at, gregod at, cpdaniel at, john at