00001 /* 00002 * UnicastSocket.cpp 00003 * 00004 * Created on: 28.06.2010 00005 * Author: morgenro 00006 */ 00007 00008 #include "ibrcommon/net/UnicastSocket.h" 00009 00010 namespace ibrcommon 00011 { 00012 UnicastSocket::UnicastSocket() 00013 : udpsocket(IPPROTO_UDP) 00014 { 00015 00016 } 00017 00018 UnicastSocket::~UnicastSocket() 00019 { 00020 } 00021 00022 void UnicastSocket::bind(int port, const NetInterface &iface) 00023 { 00024 // set listen port 00025 _sockaddr.sin_port = htons(port); 00026 00027 // convert given address 00028 iface.getAddress(&_sockaddr.sin_addr); 00029 00030 if ( ::bind(_socket, (struct sockaddr *) &_sockaddr, sizeof(_sockaddr)) < 0 ) 00031 { 00032 throw SocketException("UnicastSocket: cannot bind socket"); 00033 } 00034 } 00035 00036 void UnicastSocket::bind(int port, std::string address) 00037 { 00038 // set listen port 00039 _sockaddr.sin_port = htons(port); 00040 00041 // convert given address 00042 if (address.length() > 0) 00043 { 00044 if (inet_pton(AF_INET, address.c_str(), &_sockaddr.sin_addr) <= 0) 00045 { 00046 throw SocketException("UnicastSocket: can not parse address " + address); 00047 } 00048 } 00049 00050 if ( ::bind(_socket, (struct sockaddr *) &_sockaddr, sizeof(_sockaddr)) < 0 ) 00051 { 00052 throw SocketException("UnicastSocket: cannot bind socket"); 00053 } 00054 } 00055 }
1.7.1