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:

~~~~~

Hi

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.

http://www.boost.org/doc/libs/1_41_0/doc/html/boost_asio/example/multicast/receiver.cpp
with port 9023, listen 172.20.200.42, multicast 239.192.112.3

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

const short multicast_port = 9023;

class receiver
{
public:
  receiver(boost::asio::io_service& io_service,
      const boost::asio::ip::address& listen_address,
      const boost::asio::ip::address& multicast_address)
    : socket_(io_service)
  {
  try{
    // 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("0.0.0.0"), multicast_port);

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

    socket_.open(listen_endpoint.protocol());
    socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
    socket_.bind(listen_endpoint);

    // Join the multicast group.
    socket_.set_option(
        boost::asio::ip::multicast::join_group(multicast_address));

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

 }
  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;

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

private:
  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[])
{
  try
  {

   std::string list = "172.20.200.42";
   std::string mt = "239.192.112.3";
   boost::asio::io_service io_service;
    receiver r(io_service,
        boost::asio::ip::address::from_string(list),
        boost::asio::ip::address::from_string(mt));
    io_service.run();

  }
  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 = "239.192.112.3";
arg2 = "172.20.200.42";
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,
 sizeof(imr));
char buf[65536];

 while(1) {
        int res=recvfrom(sd,(char*)buf,sizeof(buf),0, (SOCKADDR *)& Sender,
&SenderAddrSize);
printf(buf);
         } //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 acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk