nidas  v1.2-1520
Socket.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2006, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 /*
27 
28  C++ classes supporting TCP, UDP and Unix sockets.
29 
30 */
31 
32 #ifndef NIDAS_UTIL_SOCKET_H
33 #define NIDAS_UTIL_SOCKET_H
34 
35 #include "Inet4SocketAddress.h"
36 #include "Inet4NetworkInterface.h"
37 #include "Inet4PacketInfo.h"
38 #include "UnixSocketAddress.h"
39 #include "Inet4Address.h"
40 #include "IOException.h"
41 #include "EOFException.h"
42 #include "DatagramPacket.h"
43 
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/uio.h>
47 
48 #include <net/if.h> // IFF_* bits for network interface flags
49 
50 #include <netinet/in.h>
51 #include <ctime>
52 #include <cassert>
53 
54 #include <vector>
55 
56 namespace nidas { namespace util {
57 
58 class Socket;
59 
76 class SocketImpl {
77 public:
78 
79  SocketImpl(int domain,int type) throw(IOException);
80 
81  SocketImpl(int fd, const SocketAddress& remoteaddr) throw(IOException);
82 
86  SocketImpl(const SocketImpl& x);
87 
91  SocketImpl& operator = (const SocketImpl& rhs);
92 
93  ~SocketImpl();
94 
95  int getFd() const { return _fd; }
96 
101  int getDomain() const { return _sockdomain; }
102 
103  void setBacklog(int val) { _backlog = val; }
104 
108  const SocketAddress& getLocalSocketAddress() const throw()
109  {
110  return *_localaddr;
111  }
112 
116  int getLocalPort() const throw()
117  {
118  return _localaddr->getPort();
119  }
120 
124  const SocketAddress& getRemoteSocketAddress() const throw()
125  {
126  return *_remoteaddr;
127  }
128 
132  int getRemotePort() const throw()
133  {
134  return _remoteaddr->getPort();
135  }
136 
137  void setReuseAddress(bool val) { _reuseaddr = val; }
138 
139  void setNonBlocking(bool val) throw(IOException);
140 
141  bool isNonBlocking() const throw(IOException);
142 
148  void setTcpNoDelay(bool val) throw(IOException);
149 
154  bool getTcpNoDelay() throw(IOException);
155 
163  void setTimeout(int val);
164 
165  int getTimeout() const;
166 
170  void setKeepAlive(bool val) throw(IOException);
171 
175  bool getKeepAlive() const throw(IOException);
176 
195  void setKeepAliveIdleSecs(int val) throw(IOException);
196 
202  int getKeepAliveIdleSecs() const throw(IOException);
203 
208  int getInQueueSize() const throw(IOException);
209 
214  int getOutQueueSize() const throw(IOException);
215 
216  void close() throw(IOException);
217 
218  void bind(int port)
219  throw(IOException);
220 
221  void bind(const Inet4Address& addr,int port)
222  throw(IOException);
223 
224  void bind(const SocketAddress& sockaddr)
225  throw(IOException);
226 
227  void listen() throw(IOException);
228 
229  Socket* accept() throw(IOException);
230 
231  void connect(const std::string& dest,int port)
232  throw(UnknownHostException,IOException);
233 
234  void connect(const Inet4Address& addr,int port)
235  throw(IOException);
236 
237  void connect(const SocketAddress& addr)
238  throw(IOException);
239 
240  void receive(DatagramPacketBase& packet) throw(IOException);
241 
242  void receive(DatagramPacketBase& packet,Inet4PacketInfo& info,int flags=0) throw(IOException);;
249  size_t recv(void* buf, size_t len, int flags=0)
250  throw(IOException);
251 
252  size_t recvfrom(void* buf, size_t len, int flags,
253  SocketAddress& from) throw(IOException);
254 
255 
256  void send(const DatagramPacketBase& packet,int flags=0) throw(IOException);
257 
269  size_t send(const void* buf, size_t len, int flags = 0)
270  throw(IOException);
271 
272  size_t send(const struct iovec* iov, int iovcnt, int flags = 0)
273  throw(IOException);
274 
275  size_t sendto(const void* buf, size_t len, int flags,
276  const SocketAddress& to) throw(IOException);
277 
278  size_t sendto(const struct iovec* iov, int iovcnt, int flags,const SocketAddress& to)
279  throw(IOException);
280 
292  void sendall(const void* buf, size_t len, int flags = 0)
293  throw(IOException);
294 
298  void joinGroup(Inet4Address groupAddr) throw(IOException);
299 
300  /*
301  void joinGroup(Inet4Address groupAddr,Inet4Address iaddr) throw(IOException);
302  */
303 
311  void joinGroup(Inet4Address groupAddr,const Inet4NetworkInterface&) throw(IOException);
315  void leaveGroup(Inet4Address groupAddr) throw(IOException);
316 
320  void leaveGroup(Inet4Address groupAddr,const Inet4NetworkInterface& iaddr)
321  throw(IOException);
322 
323  void setReceiveBufferSize(int size) throw(IOException);
324 
325  int getReceiveBufferSize() throw(IOException);
326 
327  void setSendBufferSize(int size) throw(IOException);
328 
329  int getSendBufferSize() throw(IOException);
330 
331  void setTimeToLive(int val) throw(IOException);
332 
333  int getTimeToLive() const throw(IOException);
334 
342  void setPktInfo(bool val) throw(IOException);
343 
344  bool getPktInfo() const { return _pktInfo; }
345 
356  void setMulticastLoop(bool val) throw(IOException);
357 
358  bool getMulticastLoop() const throw(IOException);
359 
360  void setInterface(Inet4Address maddr,const Inet4NetworkInterface& iaddr) throw(IOException);
361 
362  void setInterface(Inet4Address iaddr) throw(IOException);
363 
364  Inet4NetworkInterface getInterface() const throw(IOException);
365 
366  Inet4NetworkInterface getInterface(const std::string& name) const throw(IOException);
367 
368  Inet4NetworkInterface findInterface(const Inet4Address&) const throw(IOException);
369 
370  std::list<Inet4NetworkInterface> getInterfaces() const throw(IOException);
371 
376  void setBroadcastEnable(bool val) throw(IOException);
377 
378  bool getBroadcastEnable() const throw(IOException);
379 
380 protected:
381 
386  int _fd;
387  int _backlog;
390  struct timespec _timeout;
391 
392  bool _pktInfo;
393 
397  void getLocalAddr() throw(IOException);
398 
402  void getRemoteAddr() throw(IOException);
403 
404 };
405 
430 class Socket {
431 public:
432 
441  Socket(int domain=AF_INET) throw(IOException);
442 
447  Socket(const Inet4Address& addr,int port)
448  throw(IOException);
449 
454  Socket(const std::string& host,int port)
455  throw(UnknownHostException,IOException);
456 
462  Socket(const SocketAddress& addr) throw(IOException);
463 
469  Socket(int fd, const SocketAddress& raddr) throw(IOException);
470 
471  ~Socket() throw() {
472  }
473 
474  void close() throw(IOException)
475  {
476  _impl.close();
477  }
478 
483  static std::vector<Socket*> createSocketPair(int type=SOCK_STREAM)
484  throw(IOException);
485 
493  void setTimeout(int val) { _impl.setTimeout(val); }
494 
500  void setNonBlocking(bool val) throw(IOException)
501  {
502  _impl.setNonBlocking(val);
503  }
504 
505  bool isNonBlocking() const throw(IOException)
506  {
507  return _impl.isNonBlocking();
508  }
509 
510  void setTcpNoDelay(bool val) throw(IOException)
511  {
512  _impl.setTcpNoDelay(val);
513  }
514 
515  bool getTcpNoDelay() throw(IOException)
516  {
517  return _impl.getTcpNoDelay();
518  }
519 
523  void setKeepAlive(bool val) throw(IOException)
524  {
525  _impl.setKeepAlive(val);
526  }
527 
531  bool getKeepAlive() throw(IOException)
532  {
533  return _impl.getKeepAlive();
534  }
535 
554  void setKeepAliveIdleSecs(int val) throw(IOException)
555  {
556  _impl.setKeepAliveIdleSecs(val);
557  }
558 
564  int getKeepAliveIdleSecs() throw(IOException)
565  {
566  return _impl.getKeepAliveIdleSecs();
567  }
568 
569  int getInQueueSize() const throw(IOException)
570  {
571  return _impl.getInQueueSize();
572  }
573 
574  int getOutQueueSize() const throw(IOException)
575  {
576  return _impl.getOutQueueSize();
577  }
578 
582  std::list<Inet4NetworkInterface> getInterfaces() const throw(IOException)
583  {
584  return _impl.getInterfaces();
585  }
586 
590  void connect(const std::string& host,int port)
591  throw(UnknownHostException,IOException)
592  {
593  _impl.connect(host,port);
594  }
595 
599  void connect(const Inet4Address& addr, int port)
600  throw(IOException)
601  {
602  _impl.connect(addr,port);
603  }
604 
608  void connect(const SocketAddress& addr)
609  throw(IOException)
610  {
611  _impl.connect(addr);
612  }
613 
617  int getFd() const { return _impl.getFd(); }
618 
619  size_t recv(void* buf, size_t len, int flags = 0)
620  throw(IOException) {
621  return _impl.recv(buf,len,flags);
622  }
623 
630  size_t send(const void* buf, size_t len, int flags=MSG_NOSIGNAL)
631  throw(IOException) {
632  return _impl.send(buf,len,flags);
633  }
634 
635  size_t send(const struct iovec* iov, int iovcnt, int flags=MSG_NOSIGNAL)
636  throw(IOException) {
637  return _impl.send(iov,iovcnt,flags);
638  }
639 
651  void sendall(const void* buf, size_t len, int flags=MSG_NOSIGNAL)
652  throw(IOException) {
653  return _impl.sendall(buf,len,flags);
654  }
655 
656  void setReceiveBufferSize(int size) throw(IOException)
657  {
658  _impl.setReceiveBufferSize(size);
659  }
660  int getReceiveBufferSize() throw(IOException)
661  {
662  return _impl.getReceiveBufferSize();
663  }
664 
665  void setSendBufferSize(int size) throw(IOException)
666  {
667  _impl.setSendBufferSize(size);
668  }
669  int getSendBufferSize() throw(IOException)
670  {
671  return _impl.getSendBufferSize();
672  }
673 
677  const SocketAddress& getRemoteSocketAddress() const throw()
678  {
679  return _impl.getRemoteSocketAddress();
680  }
684  int getRemotePort() const throw() {
685  return _impl.getRemotePort();
686  }
687 
691  const SocketAddress& getLocalSocketAddress() const throw()
692  {
693  return _impl.getLocalSocketAddress();
694  }
695 
699  int getLocalPort() const throw() {
700  return _impl.getLocalPort();
701  }
702 
703  int getDomain() const { return _impl.getDomain(); }
704 
705 protected:
707 };
708 
756 public:
757 
765  ServerSocket(int port=0,int backlog=10) throw(IOException);
766 
771  ServerSocket(const Inet4Address& bindAddr, int port,int backlog=10)
772  throw(IOException);
773 
778  ServerSocket(const SocketAddress& bindAddr,int backlog=10)
779  throw(IOException);
780 
785  ~ServerSocket() throw() {
786  }
787 
788  int getFd() const { return _impl.getFd(); }
789 
793  void close() throw(IOException);
794 
808  Socket* accept() throw(IOException)
809  {
810  return _impl.accept();
811  }
812 
813  void setReceiveBufferSize(int size) throw(IOException)
814  {
815  _impl.setReceiveBufferSize(size);
816  }
817  int getReceiveBufferSize() throw(IOException)
818  {
819  return _impl.getReceiveBufferSize();
820  }
821 
822  void setSendBufferSize(int size) throw(IOException)
823  {
824  _impl.setSendBufferSize(size);
825  }
826  int getSendBufferSize() throw(IOException)
827  {
828  return _impl.getSendBufferSize();
829  }
830 
834  const SocketAddress& getLocalSocketAddress() const throw()
835  {
836  return _impl.getLocalSocketAddress();
837  }
838 
839  int getLocalPort() const throw() { return _impl.getLocalPort(); }
840 
841  int getDomain() const { return _impl.getDomain(); }
842 
843  void setNonBlocking(bool val) throw(IOException)
844  {
845  _impl.setNonBlocking(val);
846  }
847 
848  bool isNonBlocking() const throw(IOException)
849  {
850  return _impl.isNonBlocking();
851  }
852 
853 protected:
855 
856 };
857 
913 public:
914 
918  DatagramSocket() throw(IOException);
919 
927  DatagramSocket(int port) throw(IOException);
928 
936  DatagramSocket(const Inet4Address& addr,int port)
937  throw(IOException);
938 
946  DatagramSocket(const SocketAddress& addr)
947  throw(IOException);
948 
954  virtual ~DatagramSocket() throw()
955  {
956  }
957 
958  void close() throw(IOException)
959  {
960  _impl.close();
961  }
962 
970  void setTimeout(int val) {
971  _impl.setTimeout(val);
972  }
973 
979  void connect(const std::string& host,int port)
980  throw(UnknownHostException,IOException)
981  {
982  _impl.connect(host,port);
983  }
984 
990  void connect(const Inet4Address& addr, int port)
991  throw(IOException)
992  {
993  _impl.connect(addr,port);
994  }
995 
1001  void connect(const SocketAddress& addr)
1002  throw(IOException)
1003  {
1004  _impl.connect(addr);
1005  }
1006 
1013  void bind(const Inet4Address& addr, int port)
1014  throw(IOException)
1015  {
1016  _impl.bind(addr,port);
1017  }
1018 
1025  void bind(const SocketAddress& addr)
1026  throw(IOException)
1027  {
1028  _impl.bind(addr);
1029  }
1030 
1031  int getFd() const { return _impl.getFd(); }
1032 
1038  void receive(DatagramPacketBase& packet) throw(IOException)
1039  {
1040  _impl.receive(packet);
1041  }
1042 
1053  void receive(DatagramPacketBase& packet,Inet4PacketInfo& info,int flags=0) throw(IOException)
1054  {
1055  _impl.receive(packet,info,flags);
1056  }
1057 
1058  void send(const DatagramPacketBase& packet,int flags=0) throw(IOException)
1059  {
1060  _impl.send(packet,flags);
1061  }
1062 
1063  size_t recv(void* buf, size_t len, int flags=0) throw(IOException)
1064  {
1065  return _impl.recv(buf,len,flags);
1066  }
1067 
1068  size_t recvfrom(void* buf, size_t len, int flags,
1069  SocketAddress& from) throw(IOException)
1070  {
1071  return _impl.recvfrom(buf,len,flags,from);
1072  }
1073 
1074  size_t send(const void* buf, size_t len, int flags = 0)
1075  throw(IOException) {
1076  return _impl.send(buf,len,flags);
1077  }
1078 
1079  size_t send(const struct iovec* iov, int iovcnt, int flags=MSG_NOSIGNAL)
1080  throw(IOException) {
1081  return _impl.send(iov,iovcnt,flags);
1082  }
1083 
1084  size_t sendto(const void* buf, size_t len, int flags,
1085  const SocketAddress& to) throw(IOException)
1086  {
1087  return _impl.sendto(buf,len,flags,to);
1088  }
1089 
1090  size_t sendto(const struct iovec* iov, int iovcnt, int flags,
1091  const SocketAddress& to) throw(IOException)
1092  {
1093  return _impl.sendto(iov,iovcnt,flags,to);
1094  }
1095 
1099  const SocketAddress& getLocalSocketAddress() const throw()
1100  {
1101  return _impl.getLocalSocketAddress();
1102  }
1103 
1107  int getLocalPort() const throw() {
1108  return _impl.getLocalPort();
1109  }
1110 
1116  void setNonBlocking(bool val) throw(IOException)
1117  {
1118  _impl.setNonBlocking(val);
1119  }
1120 
1121  bool isNonBlocking() const throw(IOException)
1122  {
1123  return _impl.isNonBlocking();
1124  }
1125 
1126  void setReceiveBufferSize(int size) throw(IOException)
1127  {
1128  _impl.setReceiveBufferSize(size);
1129  }
1130 
1131  int getReceiveBufferSize() throw(IOException)
1132  {
1133  return _impl.getReceiveBufferSize();
1134  }
1135 
1136  void setSendBufferSize(int size) throw(IOException)
1137  {
1138  _impl.setSendBufferSize(size);
1139  }
1140  int getSendBufferSize() throw(IOException)
1141  {
1142  return _impl.getSendBufferSize();
1143  }
1144 
1145  std::list<Inet4NetworkInterface> getInterfaces() const throw(IOException)
1146  {
1147  return _impl.getInterfaces();
1148  }
1149 
1150  void setBroadcastEnable(bool val) throw(IOException)
1151  {
1152  _impl.setBroadcastEnable(val);
1153  }
1154 
1155  bool getBroadcastEnable() const throw(IOException)
1156  {
1157  return _impl.getBroadcastEnable();
1158  }
1159 
1167  void setPktInfo(bool val) throw(IOException)
1168  {
1169  _impl.setPktInfo(val);
1170  }
1171 
1172 protected:
1174 };
1175 
1242 public:
1243 
1253  MulticastSocket() throw(IOException): DatagramSocket()
1254  {
1255  // setInterface(Inet4Address(INADDR_ANY),Inet4Address(INADDR_ANY));
1256  }
1257 
1261  MulticastSocket(int port) throw(IOException) : DatagramSocket(port)
1262  {
1263  // setInterface(Inet4Address(INADDR_ANY),Inet4Address(INADDR_ANY));
1264  }
1265 
1270  MulticastSocket(const Inet4SocketAddress& addr)
1271  throw(IOException) : DatagramSocket(addr)
1272  {
1273  // setInterface(Inet4Address(INADDR_ANY),Inet4Address(INADDR_ANY));
1274  }
1275 
1279  void joinGroup(Inet4Address groupAddr) throw(IOException) {
1280  _impl.joinGroup(groupAddr);
1281  }
1282 
1286  /*
1287  void joinGroup(Inet4Address groupAddr,Inet4Address iaddr) throw(IOException) {
1288  _impl.joinGroup(groupAddr,iaddr);
1289  }
1290  */
1291 
1292  void joinGroup(Inet4Address groupAddr,const Inet4NetworkInterface & iface) throw(IOException) {
1293  _impl.joinGroup(groupAddr,iface);
1294  }
1295 
1299  void leaveGroup(Inet4Address groupAddr) throw(IOException) {
1300  _impl.leaveGroup(groupAddr);
1301  }
1302 
1306  void leaveGroup(Inet4Address groupAddr, const Inet4NetworkInterface& iface)
1307  throw(IOException)
1308  {
1309  _impl.leaveGroup(groupAddr,iface);
1310  }
1311 
1312  void setTimeToLive(int val) throw(IOException)
1313  {
1314  _impl.setTimeToLive(val);
1315  }
1316 
1317  int getTimeToLive() const throw(IOException)
1318  {
1319  return _impl.getTimeToLive();
1320  }
1321 
1333  void setMulticastLoop(bool val) throw(IOException)
1334  {
1335  _impl.setTimeToLive(val);
1336  }
1337 
1338  bool getMulticastLoop() const throw(IOException)
1339  {
1340  return _impl.getTimeToLive();
1341  }
1342 
1355  void setInterface(Inet4Address maddr,const Inet4NetworkInterface& iface) throw(IOException) {
1356  _impl.setInterface(maddr,iface);
1357  }
1358 
1368  void setInterface(Inet4Address iaddr) throw(IOException) {
1369  _impl.setInterface(iaddr);
1370  }
1371 
1372  Inet4NetworkInterface getInterface() const throw(IOException) {
1373  return _impl.getInterface();
1374  }
1375 
1377  throw(IOException)
1378  {
1379  return _impl.findInterface(iaddr);
1380  }
1381 
1385  std::list<Inet4NetworkInterface> getInterfaces() const throw(IOException)
1386  {
1387  return _impl.getInterfaces();
1388  }
1389 
1390 protected:
1391 };
1392 
1393 }} // namespace nidas namespace util
1394 
1395 #endif
void setInterface(Inet4Address iaddr)
Set the interface for a given multicast address.
Definition: Socket.h:1368
bool getPktInfo() const
Definition: Socket.h:344
size_t sendto(const struct iovec *iov, int iovcnt, int flags, const SocketAddress &to)
Definition: Socket.h:1090
Abstract base class for a UDP datagram.
Definition: DatagramPacket.h:39
int getInQueueSize() const
Get number of bytes currently unread in the local input queue.
Definition: Socket.cc:1034
int getKeepAliveIdleSecs() const
Get the current value of TCP_KEEPIDLE on this socket.
Definition: Socket.cc:1021
void setSendBufferSize(int size)
Definition: Socket.h:822
size_t recv(void *buf, size_t len, int flags=0)
Definition: Socket.h:1063
void listen()
Definition: Socket.cc:241
void setNonBlocking(bool val)
Definition: Socket.cc:438
A datagram socket to be used for multicasts.
Definition: Socket.h:1241
int getKeepAliveIdleSecs()
Get the current value of TCP_KEEPIDLE on this socket.
Definition: Socket.h:564
void receive(DatagramPacketBase &packet)
Definition: Socket.cc:474
void setMulticastLoop(bool val)
Whether to set the IP_MULTICAST_LOOP socket option.
Definition: Socket.cc:1300
void setReuseAddress(bool val)
Definition: Socket.h:137
void send(const DatagramPacketBase &packet, int flags=0)
Definition: Socket.h:1058
bool isNonBlocking() const
Definition: Socket.h:1121
int getSendBufferSize()
Definition: Socket.cc:937
bool getBroadcastEnable() const
Definition: Socket.cc:1335
void setPktInfo(bool val)
Control whether a IP_PKTINFO ancillary message is received with each datagram.
Definition: Socket.h:1167
Implementation of a socket, providing a C++ interface to system socket calls: socket,bind,listen,accept,setsockopt, etc.
Definition: Socket.h:76
int getSendBufferSize()
Definition: Socket.h:826
MulticastSocket(int port)
Create multicast socket, bind it to a local port.
Definition: Socket.h:1261
void setReceiveBufferSize(int size)
Definition: Socket.cc:909
Inet4NetworkInterface findInterface(const Inet4Address &) const
Definition: Socket.cc:1207
void setBacklog(int val)
Definition: Socket.h:103
void setTimeout(int val)
Set the timeout for receive(), recv(), and recvfrom() methods.
Definition: Socket.cc:123
void setReceiveBufferSize(int size)
Definition: Socket.h:813
Inet4NetworkInterface getInterface() const
Definition: Socket.h:1372
int getFd() const
Definition: Socket.h:1031
void receive(DatagramPacketBase &packet)
Read a packet from the DatagramSocket.
Definition: Socket.h:1038
void setBroadcastEnable(bool val)
Definition: Socket.h:1150
An interface for a socket address.
Definition: SocketAddress.h:36
MulticastSocket()
Create an unbound multicast socket.
Definition: Socket.h:1253
void receive(DatagramPacketBase &packet, Inet4PacketInfo &info, int flags=0)
Read a packet from the DatagramSocket.
Definition: Socket.h:1053
void connect(const SocketAddress &addr)
Datagrams are connectionless, so this doesn&#39;t establish a true connection, it just sets the default d...
Definition: Socket.h:1001
void connect(const Inet4Address &addr, int port)
Datagrams are connectionless, so this doesn&#39;t establish a true connection, it just sets the default d...
Definition: Socket.h:990
static Inet4NetworkInterface getInterface(const Inet4Address &addr)
Return an Inet4NetworkInterface corresponding to the given address.
Definition: Inet4NetworkInterface.cc:37
A socket for sending or receiving datagrams, either unicast, broadcast or multicast.
Definition: Socket.h:912
size_t send(const struct iovec *iov, int iovcnt, int flags=MSG_NOSIGNAL)
Definition: Socket.h:1079
bool isNonBlocking() const
Definition: Socket.h:848
int getOutQueueSize() const
Get number of bytes currently unsent in the local output queue.
Definition: Socket.cc:1046
void setMulticastLoop(bool val)
Whether to set the IP_MULTICAST_LOOP socket option.
Definition: Socket.h:1333
int getFd() const
Definition: Socket.h:788
void setKeepAliveIdleSecs(int val)
Set the number of seconds a connection needs to be idle before TCP begins sending out keep-alive prob...
Definition: Socket.cc:997
int getFd() const
Definition: Socket.h:95
void setTcpNoDelay(bool val)
Definition: Socket.h:510
bool getMulticastLoop() const
Definition: Socket.cc:1311
std::list< Inet4NetworkInterface > getInterfaces() const
Definition: Socket.h:1145
int getTimeToLive() const
Definition: Socket.h:1317
std::list< Inet4NetworkInterface > getInterfaces() const
Get a list of all my network interfaces.
Definition: Socket.h:582
size_t recvfrom(void *buf, size_t len, int flags, SocketAddress &from)
Definition: Socket.cc:749
void setKeepAlive(bool val)
Set or unset the SO_KEEPALIVE socket option.
Definition: Socket.cc:973
A IP version 4 socket address, containing a host address, and a port number.
Definition: Inet4SocketAddress.h:41
~SocketImpl()
Definition: Socket.cc:117
size_t sendto(const void *buf, size_t len, int flags, const SocketAddress &to)
Definition: Socket.h:1084
bool _pktInfo
Definition: Socket.h:392
void connect(const std::string &host, int port)
Datagrams are connectionless, so this doesn&#39;t establish a true connection, it just sets the default d...
Definition: Socket.h:979
int getReceiveBufferSize()
Definition: Socket.h:660
int getSendBufferSize()
Definition: Socket.h:1140
void close()
Definition: Socket.h:958
void bind(const Inet4Address &addr, int port)
Bind the DatagramSocket to the specified local address.
Definition: Socket.h:1013
int getOutQueueSize() const
Definition: Socket.h:574
void setSendBufferSize(int size)
Definition: Socket.h:1136
const SocketAddress & getRemoteSocketAddress() const
Get remote socket address of this socket.
Definition: Socket.h:124
size_t recvfrom(void *buf, size_t len, int flags, SocketAddress &from)
Definition: Socket.h:1068
int getLocalPort() const
Get local port number of this socket.
Definition: Socket.h:1107
void bind(int port)
Definition: Socket.cc:179
int getDomain() const
Definition: Socket.h:841
const SocketAddress & getLocalSocketAddress() const
Fetch the local address that this socket is bound to.
Definition: Socket.h:834
A stream (TCP) socket that is used to listen for connections.
Definition: Socket.h:755
Socket * accept()
Definition: Socket.cc:251
int getLocalPort() const
Get local port number of this socket.
Definition: Socket.h:699
size_t sendto(const void *buf, size_t len, int flags, const SocketAddress &to)
Definition: Socket.cc:860
void getLocalAddr()
Do system call to determine local address of this socket.
Definition: Socket.cc:390
int _backlog
Definition: Socket.h:387
bool _hasTimeout
Definition: Socket.h:389
bool getTcpNoDelay()
Get the TCP_NODELAY (man 7 tcp) option on the socket.
Definition: Socket.cc:961
void setSendBufferSize(int size)
Definition: Socket.cc:929
SocketImpl & operator=(const SocketImpl &rhs)
Assignment operator.
Definition: Socket.cc:99
void bind(const SocketAddress &addr)
Bind the DatagramSocket to the specified local address.
Definition: Socket.h:1025
bool getTcpNoDelay()
Definition: Socket.h:515
void connect(const Inet4Address &addr, int port)
Connect to a given remote host address and port.
Definition: Socket.h:599
void close()
Definition: Socket.cc:135
size_t recv(void *buf, size_t len, int flags=0)
Receive data on a socket.
Definition: Socket.cc:684
int _socktype
Definition: Socket.h:383
int getInQueueSize() const
Definition: Socket.h:569
int getFd() const
Fetch the file descriptor associate with this socket.
Definition: Socket.h:617
void connect(const std::string &dest, int port)
Definition: Socket.cc:146
void joinGroup(Inet4Address groupAddr)
Join a multicast group on all interfaces.
Definition: Socket.cc:1060
bool _reuseaddr
Definition: Socket.h:388
void leaveGroup(Inet4Address groupAddr, const Inet4NetworkInterface &iface)
Leave a multicast group on a given interface.
Definition: Socket.h:1306
void getRemoteAddr()
Do system call to determine address of remote end.
Definition: Socket.cc:414
void setInterface(Inet4Address maddr, const Inet4NetworkInterface &iaddr)
~ServerSocket()
Destructor.
Definition: Socket.h:785
void setReceiveBufferSize(int size)
Definition: Socket.h:1126
const SocketAddress & getLocalSocketAddress() const
Get local address of this socket.
Definition: Socket.h:1099
Definition: Inet4NetworkInterface.h:35
void joinGroup(Inet4Address groupAddr)
Join a multicast group on all interfaces.
Definition: Socket.h:1279
size_t send(const void *buf, size_t len, int flags=0)
Definition: Socket.h:1074
void setSendBufferSize(int size)
Definition: Socket.h:665
int getReceiveBufferSize()
Definition: Socket.cc:917
void joinGroup(Inet4Address groupAddr, const Inet4NetworkInterface &iface)
Join a multicast group on a given interface.
Definition: Socket.h:1292
void setTimeToLive(int val)
Definition: Socket.cc:1277
void setKeepAlive(bool val)
Set or unset the SO_KEEPALIVE socket option.
Definition: Socket.h:523
void leaveGroup(Inet4Address groupAddr)
Leave a multicast group on all interfaces.
Definition: Socket.cc:1141
int getLocalPort() const
Definition: Socket.h:839
bool getKeepAlive() const
Get the current value of the SO_KEEPALIVE socket option.
Definition: Socket.cc:985
void setBroadcastEnable(bool val)
Enable or disable SO_BROADCAST.
Definition: Socket.cc:1324
void setPktInfo(bool val)
Control whether a IP_PKTINFO ancillary message is received with each datagram.
Definition: Socket.cc:1435
int len
Definition: sing.cc:934
void setTimeout(int val)
Set the timeout for receive(), recv(), and recvfrom() methods.
Definition: Socket.h:970
SocketImpl _impl
Definition: Socket.h:706
bool getMulticastLoop() const
Definition: Socket.h:1338
int fd
Definition: twod.c:56
void connect(const std::string &host, int port)
Connect to a given remote host and port.
Definition: Socket.h:590
const SocketAddress & getRemoteSocketAddress() const
Get remote address of this socket.
Definition: Socket.h:677
int getRemotePort() const
Get remote port number of this socket.
Definition: Socket.h:684
std::list< Inet4NetworkInterface > getInterfaces() const
Definition: Socket.cc:1388
Definition: IOException.h:37
int getLocalPort() const
Get local port number of this socket.
Definition: Socket.h:116
bool isNonBlocking() const
Definition: Socket.h:505
int getReceiveBufferSize()
Definition: Socket.h:817
virtual ~DatagramSocket()
MulticastSocket is derived from DatagramSocket, so we provide a virtual destructor.
Definition: Socket.h:954
int getRemotePort() const
Get remote port number of this socket.
Definition: Socket.h:132
int getDomain() const
Get the domain of this socket: AF_UNIX, AF_INET, etc, from sys/socket.h.
Definition: Socket.h:101
SocketAddress * _remoteaddr
Definition: Socket.h:385
SocketAddress * _localaddr
Definition: Socket.h:384
int _fd
Definition: Socket.h:386
bool isNonBlocking() const
Definition: Socket.cc:463
Inet4NetworkInterface getInterface() const
Definition: Socket.cc:1228
Ancillary information that can be determined about an incoming UDP packet.
Definition: Inet4PacketInfo.h:39
static n_u::SerialPort port
Definition: sing.cc:68
int getSendBufferSize()
Definition: Socket.h:669
A stream (TCP) socket.
Definition: Socket.h:430
size_t recv(void *buf, size_t len, int flags=0)
Definition: Socket.h:619
virtual int getPort() const =0
return the port number of this address, or -1 if there is no associated port number, e.g.
size_t send(const void *buf, size_t len, int flags=MSG_NOSIGNAL)
send data on socket, see man page for send system function.
Definition: Socket.h:630
std::list< Inet4NetworkInterface > getInterfaces() const
Return the IP addresses of all my network interfaces.
Definition: Socket.h:1385
int getTimeout() const
Definition: Socket.cc:130
void leaveGroup(Inet4Address groupAddr)
Leave a multicast group on all interfaces.
Definition: Socket.h:1299
int getDomain() const
Definition: Socket.h:703
void setTimeToLive(int val)
Definition: Socket.h:1312
~Socket()
Definition: Socket.h:471
void setNonBlocking(bool val)
Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.
Definition: Socket.h:1116
const SocketAddress & getLocalSocketAddress() const
Get local address of this socket.
Definition: Socket.h:691
int getReceiveBufferSize()
Definition: Socket.h:1131
void setInterface(Inet4Address maddr, const Inet4NetworkInterface &iface)
Set the interface for a given multicast address.
Definition: Socket.h:1355
void sendall(const void *buf, size_t len, int flags=0)
send all data in buffer on socket, repeating send() as necessary, until all data is sent (or an excep...
Definition: Socket.cc:891
SocketImpl(int domain, int type)
Definition: Socket.cc:62
const SocketAddress & getLocalSocketAddress() const
Get local socket address of this socket.
Definition: Socket.h:108
struct timespec _timeout
Definition: Socket.h:390
Definition: UnknownHostException.h:35
void connect(const SocketAddress &addr)
Connect to a given remote socket address.
Definition: Socket.h:608
void setNonBlocking(bool val)
Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.
Definition: Socket.h:500
void send(const DatagramPacketBase &packet, int flags=0)
Definition: Socket.cc:817
Support for IP version 4 host address.
Definition: Inet4Address.h:46
SocketImpl _impl
Definition: Socket.h:854
void setNonBlocking(bool val)
Definition: Socket.h:843
Inet4NetworkInterface findInterface(const Inet4Address &iaddr) const
Definition: Socket.h:1376
bool getBroadcastEnable() const
Definition: Socket.h:1155
int _sockdomain
Definition: Socket.h:382
void setReceiveBufferSize(int size)
Definition: Socket.h:656
void setTcpNoDelay(bool val)
Set the TCP_NODELAY (man 7 tcp) option on the socket.
Definition: Socket.cc:949
bool getKeepAlive()
Get the current value of the SO_KEEPALIVE socket option.
Definition: Socket.h:531
int getTimeToLive() const
Definition: Socket.cc:1287
void setKeepAliveIdleSecs(int val)
Set the number of seconds a connection needs to be idle before TCP begins sending out keep-alive prob...
Definition: Socket.h:554
size_t send(const struct iovec *iov, int iovcnt, int flags=MSG_NOSIGNAL)
Definition: Socket.h:635
SocketImpl _impl
Definition: Socket.h:1173
void close()
Definition: Socket.h:474
void sendall(const void *buf, size_t len, int flags=MSG_NOSIGNAL)
send all data in buffer on socket, repeating send() as necessary, until all data is sent (or an excep...
Definition: Socket.h:651