38 m_sMsg.append(strerror(errno));
80 unsigned int addr_len =
sizeof(addr);
82 if (getsockname(
m_sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
83 throw CSocketException(
"Fetch of local address failed (getsockname())",
true);
85 return inet_ntoa(addr.sin_addr);
91 unsigned int addr_len =
sizeof(addr);
93 if (getsockname(
m_sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
96 return ntohs(addr.sin_port);
102 sockaddr_in localAddr;
103 memset(&localAddr, 0,
sizeof(localAddr));
104 localAddr.sin_family = AF_INET;
105 localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
106 localAddr.sin_port = htons(localPort);
108 if (bind(
m_sockDesc, (sockaddr *) &localAddr,
sizeof(sockaddr_in)) < 0) {
116 sockaddr_in localAddr;
117 FillAddr(localAddress, localPort, localAddr);
119 if (bind(
m_sockDesc, (sockaddr *) &localAddr,
sizeof(sockaddr_in)) < 0) {
120 throw CSocketException(
"Set of local address and port failed (bind())",
true);
124 void CSocket::FillAddr(
const string & localAddress,
unsigned short localPort, sockaddr_in& localAddr )
126 memset(&localAddr, 0,
sizeof(localAddr));
127 localAddr.sin_family = AF_INET;
130 if ((host = gethostbyname(localAddress.c_str())) == NULL) {
135 localAddr.sin_addr.s_addr = *((
unsigned long *) host->h_addr_list[0]);
137 localAddr.sin_port = htons(localPort);
142 unsigned long int nSize;
143 socklen_t n =
sizeof(nSize);
144 getsockopt(
m_sockDesc,SOL_SOCKET,SO_RCVBUF,(
void *)&nSize, (&n));
151 if (setsockopt(
m_sockDesc, SOL_SOCKET, SO_RCVBUF, &nSize,
sizeof(nSize)) == -1)
169 opts = ( opts | O_NONBLOCK );
171 opts = ( opts & ~O_NONBLOCK );
179 sockaddr_in destAddr;
180 FillAddr(foreignAddress, foreignPort, destAddr);
183 if (::connect(
m_sockDesc, (sockaddr *) &destAddr,
sizeof(destAddr)) < 0) {
190 if (::send(
m_sockDesc, (
void *) buffer, bufferLen, 0) < 0) {
198 if ((nBytes = ::recv(
m_sockDesc, (
void *) buffer, bufferLen, 0)) < 0) {
201 char* sData =
static_cast<char *
>(buffer);
202 sData[nBytes] =
'\0';
209 unsigned int addr_len =
sizeof(addr);
211 if (getpeername(
m_sockDesc, (sockaddr *) &addr,(socklen_t *) &addr_len) < 0) {
212 throw CSocketException(
"Fetch of foreign address failed (getpeername())",
true);
214 return inet_ntoa(addr.sin_addr);
220 unsigned int addr_len =
sizeof(addr);
222 if (getpeername(
m_sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
223 throw CSocketException(
"Fetch of foreign port failed (getpeername())",
true);
225 return ntohs(addr.sin_port);
230 Send(sStr.c_str(), sStr.length());
267 if (timeToWait == ULONG_MAX)
269 nRet = select(fdmax+1, &read_fds, NULL, NULL, NULL);
277 struct timeval timeout;
278 timeout.tv_sec = timeToWait;
280 nRet = select(fdmax+1, &read_fds, NULL, NULL, &timeout);
295 memset(&ifr, 0,
sizeof(ifr));
296 snprintf(ifr.ifr_name,
sizeof(ifr.ifr_name),
"%s", sInterface.c_str());
320 sockaddr_in nullAddr;
321 memset(&nullAddr, 0,
sizeof(nullAddr));
322 nullAddr.sin_family = AF_UNSPEC;
324 if (::connect(
m_sockDesc, (sockaddr *) &nullAddr,
sizeof(nullAddr)) < 0)
326 if (errno != EAFNOSUPPORT)
334 unsigned short foreignPort )
336 sockaddr_in destAddr;
337 FillAddr(foreignAddress, foreignPort, destAddr);
339 if (sendto(
m_sockDesc, (
void *) buffer, bufferLen, 0,(sockaddr *) &destAddr,
sizeof(destAddr)) != bufferLen)
348 sockaddr_in clntAddr;
349 socklen_t addrLen =
sizeof(clntAddr);
351 if ((nBytes = recvfrom(
m_sockDesc, (
void *) buffer, bufferLen, 0, (sockaddr *) &clntAddr,
352 (socklen_t *) &addrLen)) < 0)
356 sourceAddress = inet_ntoa(clntAddr.sin_addr);
357 sourcePort = ntohs(clntAddr.sin_port);
358 char* sData =
static_cast<char *
>(buffer);
359 sData[nBytes] =
'\0';
365 if (setsockopt(
m_sockDesc, IPPROTO_IP, IP_MULTICAST_TTL, (
void *) &multicastTTL,
sizeof(multicastTTL)) < 0)
373 struct ip_mreq multicastRequest;
375 multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str());
376 multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
377 if (setsockopt(
m_sockDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP,
378 (
void *) &multicastRequest,
379 sizeof(multicastRequest)) < 0)
388 struct ip_mreq multicastRequest;
390 multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str());
391 multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
392 if (setsockopt(
m_sockDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP,
393 (
void *) &multicastRequest,
394 sizeof(multicastRequest)) < 0)
396 throw CSocketException(
"Multicast group leave failed (setsockopt())",
true);
405 int broadcastPermission = 1;
406 setsockopt(
m_sockDesc, SOL_SOCKET, SO_BROADCAST,
407 (
void *) &broadcastPermission,
sizeof(broadcastPermission));
void SetReadBufferSize(unsigned int nSize)
CSocket(SocketType type, NetworkLayerProtocol protocol)
void SetMulticastTTL(unsigned char multicastTTL)
void BindLocalPort(unsigned short localPort)
virtual int OnDataRead(unsigned long timeToWait=ULONG_MAX)
unsigned long int GetReadBufferSize()
void SetBindToDevice(const string &sInterface)
void BindLocalAddressAndPort(const string &localAddress, unsigned short localPort=0)
CSocket & operator<<(const string &sStr)
void JoinGroup(const string &multicastGroup)
unsigned short GetPeerPort()
unsigned short GetLocalPort()
static void FillAddr(const string &localAddress, unsigned short localPort, sockaddr_in &localAddr)
void ConnectToHost(const string &foreignAddress, unsigned short foreignPort)
void operator=(const CSocket &sock)
CSocket & operator>>(string &sStr)
CSocketException(const string &message, bool bSysMsg=false)
void SetNonBlocking(bool bBlocking)
int Recv(void *buffer, int bufferLen)
void LeaveGroup(const string &multicastGroup)
virtual ~CSocketException()
void SendDataGram(const void *buffer, int bufferLen, const string &foreignAddress, unsigned short foreignPort)
void Send(const void *buffer, int bufferLen)
void DisconnectFromHost()
int RecvDataGram(void *buffer, int bufferLen, string &sourceAddress, unsigned short &sourcePort)