• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ibrcommon/ibrcommon/net/BroadcastSocket.cpp

Go to the documentation of this file.
00001 /*
00002  * BroadcastSocket.cpp
00003  *
00004  *  Created on: 28.06.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrcommon/net/BroadcastSocket.h"
00009 #include "ibrcommon/net/NetAddress.h"
00010 
00011 namespace ibrcommon
00012 {
00013         BroadcastSocket::BroadcastSocket()
00014          : udpsocket(IPPROTO_UDP)
00015         {
00016                 int b = 1;
00017                 if ( ::setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, (char*)&b, sizeof(b)) == -1 )
00018                 {
00019                         throw SocketException("BroadcastSocket: cannot send broadcasts");
00020                 }
00021 
00022                 // broadcast addresses should be usable more than once.
00023                 setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&b, sizeof(b));
00024         }
00025 
00026         BroadcastSocket::~BroadcastSocket()
00027         {
00028         }
00029 
00030         void BroadcastSocket::bind(int port, const NetInterface &iface)
00031         {
00032                 bind(port, iface.getBroadcast(NetAddress::NETADDR_IP).toString());
00033         }
00034 
00035         void BroadcastSocket::bind(int port, std::string address)
00036         {
00037                 // set listen port
00038                 _sockaddr.sin_port = htons(port);
00039 
00040                 // convert given address
00041                 if (address.length() > 0)
00042                 {
00043                         if (inet_pton(AF_INET, address.c_str(), &_sockaddr.sin_addr) <= 0)
00044                         {
00045                                 throw SocketException("BroadcastSocket: can not parse address " + address);
00046                         }
00047                 }
00048 
00049                 if ( ::bind(_socket, (struct sockaddr *) &_sockaddr, sizeof(_sockaddr)) < 0 )
00050                 {
00051                         throw SocketException("BroadcastSocket: cannot bind socket");
00052                 }
00053         }
00054 }

Generated on Thu Nov 11 2010 09:49:47 for IBR-DTNSuite by  doxygen 1.7.1