1
1
/*
2
2
* 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
4
6
*
5
7
* This program is free software; you can redistribute it and/or modify
6
8
* it under the terms of the GNU General Public License as published by
@@ -60,14 +62,14 @@ const char *SocketException::what() const throw() {
60
62
}
61
63
62
64
// 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,
64
66
sockaddr_in &addr) {
65
67
memset (&addr, 0 , sizeof (addr)); // Zero out address structure
66
68
addr.sin_family = AF_INET; // Internet address
67
69
68
70
hostent *host; // Resolve name
69
71
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()
71
73
// is supposedly obsolete
72
74
throw SocketException (" Failed to resolve name (gethostbyname())" );
73
75
}
@@ -169,13 +171,13 @@ unsigned short Socket::resolveService(const string &service,
169
171
170
172
if ((serv = getservbyname (service.c_str (), protocol.c_str ())) == NULL )
171
173
return atoi (service.c_str ()); /* Service is port number */
172
- else
174
+ else
173
175
return ntohs (serv->s_port ); /* Found port (network byte order) by name */
174
176
}
175
177
176
178
// CommunicatingSocket Code
177
179
178
- CommunicatingSocket::CommunicatingSocket (int type, int protocol)
180
+ CommunicatingSocket::CommunicatingSocket (int type, int protocol)
179
181
throw(SocketException) : Socket(type, protocol) {
180
182
}
181
183
@@ -194,14 +196,14 @@ void CommunicatingSocket::connect(const string &foreignAddress,
194
196
}
195
197
}
196
198
197
- void CommunicatingSocket::send (const void *buffer, int bufferLen)
199
+ void CommunicatingSocket::send (const void *buffer, int bufferLen)
198
200
throw(SocketException) {
199
201
if (::send (sockDesc, (raw_type *) buffer, bufferLen, 0 ) < 0 ) {
200
202
throw SocketException (" Send failed (send())" , true );
201
203
}
202
204
}
203
205
204
- int CommunicatingSocket::recv (void *buffer, int bufferLen)
206
+ int CommunicatingSocket::recv (void *buffer, int bufferLen)
205
207
throw(SocketException) {
206
208
int rtn;
207
209
if ((rtn = ::recv (sockDesc, (raw_type *) buffer, bufferLen, 0 )) < 0 ) {
@@ -211,7 +213,7 @@ int CommunicatingSocket::recv(void *buffer, int bufferLen)
211
213
return rtn;
212
214
}
213
215
214
- string CommunicatingSocket::getForeignAddress ()
216
+ string CommunicatingSocket::getForeignAddress ()
215
217
throw(SocketException) {
216
218
sockaddr_in addr;
217
219
unsigned int addr_len = sizeof (addr);
@@ -234,8 +236,8 @@ unsigned short CommunicatingSocket::getForeignPort() throw(SocketException) {
234
236
235
237
// TCPSocket Code
236
238
237
- TCPSocket::TCPSocket ()
238
- throw(SocketException) : CommunicatingSocket(SOCK_STREAM,
239
+ TCPSocket::TCPSocket ()
240
+ throw(SocketException) : CommunicatingSocket(SOCK_STREAM,
239
241
IPPROTO_TCP) {
240
242
}
241
243
@@ -249,14 +251,14 @@ TCPSocket::TCPSocket(int newConnSD) : CommunicatingSocket(newConnSD) {
249
251
250
252
// TCPServerSocket Code
251
253
252
- TCPServerSocket::TCPServerSocket (unsigned short localPort, int queueLen)
254
+ TCPServerSocket::TCPServerSocket (unsigned short localPort, int queueLen)
253
255
throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) {
254
256
setLocalPort (localPort);
255
257
setListen (queueLen);
256
258
}
257
259
258
- TCPServerSocket::TCPServerSocket (const string &localAddress,
259
- unsigned short localPort, int queueLen)
260
+ TCPServerSocket::TCPServerSocket (const string &localAddress,
261
+ unsigned short localPort, int queueLen)
260
262
throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) {
261
263
setLocalAddressAndPort (localAddress, localPort);
262
264
setListen (queueLen);
@@ -284,23 +286,23 @@ UDPSocket::UDPSocket() throw(SocketException) : CommunicatingSocket(SOCK_DGRAM,
284
286
setBroadcast ();
285
287
}
286
288
287
- UDPSocket::UDPSocket (unsigned short localPort) throw(SocketException) :
289
+ UDPSocket::UDPSocket (unsigned short localPort) throw(SocketException) :
288
290
CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) {
289
291
setLocalPort (localPort);
290
292
setBroadcast ();
291
293
}
292
294
293
- UDPSocket::UDPSocket (const string &localAddress, unsigned short localPort)
295
+ UDPSocket::UDPSocket (const string &localAddress, unsigned short localPort)
294
296
throw(SocketException) : CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) {
295
297
setLocalAddressAndPort (localAddress, localPort);
296
298
setBroadcast ();
297
299
}
298
300
299
301
void 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
301
303
// system that cannot broadcast to continue if they don't plan to broadcast
302
304
int broadcastPermission = 1 ;
303
- setsockopt (sockDesc, SOL_SOCKET, SO_BROADCAST,
305
+ setsockopt (sockDesc, SOL_SOCKET, SO_BROADCAST,
304
306
(raw_type *) &broadcastPermission, sizeof (broadcastPermission));
305
307
}
306
308
@@ -321,8 +323,8 @@ void UDPSocket::disconnect() throw(SocketException) {
321
323
}
322
324
}
323
325
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)
326
328
throw (SocketException) {
327
329
sockaddr_in destAddr;
328
330
fillAddr (foreignAddress, foreignPort, destAddr);
@@ -339,7 +341,7 @@ int UDPSocket::recvFrom(void *buffer, int bufferLen, string &sourceAddress,
339
341
sockaddr_in clntAddr;
340
342
socklen_t addrLen = sizeof (clntAddr);
341
343
int rtn;
342
- if ((rtn = recvfrom (sockDesc, (raw_type *) buffer, bufferLen, 0 ,
344
+ if ((rtn = recvfrom (sockDesc, (raw_type *) buffer, bufferLen, 0 ,
343
345
(sockaddr *) &clntAddr, (socklen_t *) &addrLen)) < 0 ) {
344
346
throw SocketException (" Receive failed (recvfrom())" , true );
345
347
}
@@ -350,7 +352,7 @@ int UDPSocket::recvFrom(void *buffer, int bufferLen, string &sourceAddress,
350
352
}
351
353
352
354
void 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,
354
356
(raw_type *) &multicastTTL, sizeof (multicastTTL)) < 0 ) {
355
357
throw SocketException (" Multicast TTL set failed (setsockopt())" , true );
356
358
}
@@ -361,8 +363,8 @@ void UDPSocket::joinGroup(const string &multicastGroup) throw(SocketException) {
361
363
362
364
multicastRequest.imr_multiaddr .s_addr = inet_addr (multicastGroup.c_str ());
363
365
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,
366
368
sizeof (multicastRequest)) < 0 ) {
367
369
throw SocketException (" Multicast group join failed (setsockopt())" , true );
368
370
}
@@ -373,8 +375,8 @@ void UDPSocket::leaveGroup(const string &multicastGroup) throw(SocketException)
373
375
374
376
multicastRequest.imr_multiaddr .s_addr = inet_addr (multicastGroup.c_str ());
375
377
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,
378
380
sizeof (multicastRequest)) < 0 ) {
379
381
throw SocketException (" Multicast group leave failed (setsockopt())" , true );
380
382
}
0 commit comments