11/*
22 * C++ sockets on Unix and Windows
3- * Copyright (C) 2002
3+ * Copyright (C) 2002 Michael J. Donahoo and Kenneth L. Calvert
4+ * http://cs.ecs.baylor.edu/%7Edonahoo/practical/CSockets/practical/
5+ * Modified code forked from https://github.com/chenxiaoqino/udp-image-streaming
46 *
57 * This program is free software; you can redistribute it and/or modify
68 * it under the terms of the GNU General Public License as published by
@@ -60,14 +62,14 @@ const char *SocketException::what() const throw() {
6062}
6163
6264// Function to fill in address structure given an address and port
63- static void fillAddr (const string &address, unsigned short port,
65+ static void fillAddr (const string &address, unsigned short port,
6466 sockaddr_in &addr) {
6567 memset (&addr, 0 , sizeof (addr)); // Zero out address structure
6668 addr.sin_family = AF_INET; // Internet address
6769
6870 hostent *host; // Resolve name
6971 if ((host = gethostbyname (address.c_str ())) == NULL ) {
70- // strerror() will not work for gethostbyname() and hstrerror()
72+ // strerror() will not work for gethostbyname() and hstrerror()
7173 // is supposedly obsolete
7274 throw SocketException (" Failed to resolve name (gethostbyname())" );
7375 }
@@ -169,13 +171,13 @@ unsigned short Socket::resolveService(const string &service,
169171
170172 if ((serv = getservbyname (service.c_str (), protocol.c_str ())) == NULL )
171173 return atoi (service.c_str ()); /* Service is port number */
172- else
174+ else
173175 return ntohs (serv->s_port ); /* Found port (network byte order) by name */
174176}
175177
176178// CommunicatingSocket Code
177179
178- CommunicatingSocket::CommunicatingSocket (int type, int protocol)
180+ CommunicatingSocket::CommunicatingSocket (int type, int protocol)
179181 throw(SocketException) : Socket(type, protocol) {
180182}
181183
@@ -194,14 +196,14 @@ void CommunicatingSocket::connect(const string &foreignAddress,
194196 }
195197}
196198
197- void CommunicatingSocket::send (const void *buffer, int bufferLen)
199+ void CommunicatingSocket::send (const void *buffer, int bufferLen)
198200 throw(SocketException) {
199201 if (::send (sockDesc, (raw_type *) buffer, bufferLen, 0 ) < 0 ) {
200202 throw SocketException (" Send failed (send())" , true );
201203 }
202204}
203205
204- int CommunicatingSocket::recv (void *buffer, int bufferLen)
206+ int CommunicatingSocket::recv (void *buffer, int bufferLen)
205207 throw(SocketException) {
206208 int rtn;
207209 if ((rtn = ::recv (sockDesc, (raw_type *) buffer, bufferLen, 0 )) < 0 ) {
@@ -211,7 +213,7 @@ int CommunicatingSocket::recv(void *buffer, int bufferLen)
211213 return rtn;
212214}
213215
214- string CommunicatingSocket::getForeignAddress ()
216+ string CommunicatingSocket::getForeignAddress ()
215217 throw(SocketException) {
216218 sockaddr_in addr;
217219 unsigned int addr_len = sizeof (addr);
@@ -234,8 +236,8 @@ unsigned short CommunicatingSocket::getForeignPort() throw(SocketException) {
234236
235237// TCPSocket Code
236238
237- TCPSocket::TCPSocket ()
238- throw(SocketException) : CommunicatingSocket(SOCK_STREAM,
239+ TCPSocket::TCPSocket ()
240+ throw(SocketException) : CommunicatingSocket(SOCK_STREAM,
239241 IPPROTO_TCP) {
240242}
241243
@@ -249,14 +251,14 @@ TCPSocket::TCPSocket(int newConnSD) : CommunicatingSocket(newConnSD) {
249251
250252// TCPServerSocket Code
251253
252- TCPServerSocket::TCPServerSocket (unsigned short localPort, int queueLen)
254+ TCPServerSocket::TCPServerSocket (unsigned short localPort, int queueLen)
253255 throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) {
254256 setLocalPort (localPort);
255257 setListen (queueLen);
256258}
257259
258- TCPServerSocket::TCPServerSocket (const string &localAddress,
259- unsigned short localPort, int queueLen)
260+ TCPServerSocket::TCPServerSocket (const string &localAddress,
261+ unsigned short localPort, int queueLen)
260262 throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) {
261263 setLocalAddressAndPort (localAddress, localPort);
262264 setListen (queueLen);
@@ -284,23 +286,23 @@ UDPSocket::UDPSocket() throw(SocketException) : CommunicatingSocket(SOCK_DGRAM,
284286 setBroadcast ();
285287}
286288
287- UDPSocket::UDPSocket (unsigned short localPort) throw(SocketException) :
289+ UDPSocket::UDPSocket (unsigned short localPort) throw(SocketException) :
288290 CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) {
289291 setLocalPort (localPort);
290292 setBroadcast ();
291293}
292294
293- UDPSocket::UDPSocket (const string &localAddress, unsigned short localPort)
295+ UDPSocket::UDPSocket (const string &localAddress, unsigned short localPort)
294296 throw(SocketException) : CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) {
295297 setLocalAddressAndPort (localAddress, localPort);
296298 setBroadcast ();
297299}
298300
299301void UDPSocket::setBroadcast () {
300- // If this fails, we'll hear about it when we try to send. This will allow
302+ // If this fails, we'll hear about it when we try to send. This will allow
301303 // system that cannot broadcast to continue if they don't plan to broadcast
302304 int broadcastPermission = 1 ;
303- setsockopt (sockDesc, SOL_SOCKET, SO_BROADCAST,
305+ setsockopt (sockDesc, SOL_SOCKET, SO_BROADCAST,
304306 (raw_type *) &broadcastPermission, sizeof (broadcastPermission));
305307}
306308
@@ -321,8 +323,8 @@ void UDPSocket::disconnect() throw(SocketException) {
321323 }
322324}
323325
324- void UDPSocket::sendTo (const void *buffer, int bufferLen,
325- const string &foreignAddress, unsigned short foreignPort)
326+ void UDPSocket::sendTo (const void *buffer, int bufferLen,
327+ const string &foreignAddress, unsigned short foreignPort)
326328 throw (SocketException) {
327329 sockaddr_in destAddr;
328330 fillAddr (foreignAddress, foreignPort, destAddr);
@@ -339,7 +341,7 @@ int UDPSocket::recvFrom(void *buffer, int bufferLen, string &sourceAddress,
339341 sockaddr_in clntAddr;
340342 socklen_t addrLen = sizeof (clntAddr);
341343 int rtn;
342- if ((rtn = recvfrom (sockDesc, (raw_type *) buffer, bufferLen, 0 ,
344+ if ((rtn = recvfrom (sockDesc, (raw_type *) buffer, bufferLen, 0 ,
343345 (sockaddr *) &clntAddr, (socklen_t *) &addrLen)) < 0 ) {
344346 throw SocketException (" Receive failed (recvfrom())" , true );
345347 }
@@ -350,7 +352,7 @@ int UDPSocket::recvFrom(void *buffer, int bufferLen, string &sourceAddress,
350352}
351353
352354void UDPSocket::setMulticastTTL (unsigned char multicastTTL) throw (SocketException) {
353- if (setsockopt (sockDesc, IPPROTO_IP, IP_MULTICAST_TTL,
355+ if (setsockopt (sockDesc, IPPROTO_IP, IP_MULTICAST_TTL,
354356 (raw_type *) &multicastTTL, sizeof (multicastTTL)) < 0 ) {
355357 throw SocketException (" Multicast TTL set failed (setsockopt())" , true );
356358 }
@@ -361,8 +363,8 @@ void UDPSocket::joinGroup(const string &multicastGroup) throw(SocketException) {
361363
362364 multicastRequest.imr_multiaddr .s_addr = inet_addr (multicastGroup.c_str ());
363365 multicastRequest.imr_interface .s_addr = htonl (INADDR_ANY);
364- if (setsockopt (sockDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP,
365- (raw_type *) &multicastRequest,
366+ if (setsockopt (sockDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP,
367+ (raw_type *) &multicastRequest,
366368 sizeof (multicastRequest)) < 0 ) {
367369 throw SocketException (" Multicast group join failed (setsockopt())" , true );
368370 }
@@ -373,8 +375,8 @@ void UDPSocket::leaveGroup(const string &multicastGroup) throw(SocketException)
373375
374376 multicastRequest.imr_multiaddr .s_addr = inet_addr (multicastGroup.c_str ());
375377 multicastRequest.imr_interface .s_addr = htonl (INADDR_ANY);
376- if (setsockopt (sockDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP,
377- (raw_type *) &multicastRequest,
378+ if (setsockopt (sockDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP,
379+ (raw_type *) &multicastRequest,
378380 sizeof (multicastRequest)) < 0 ) {
379381 throw SocketException (" Multicast group leave failed (setsockopt())" , true );
380382 }
0 commit comments