32#ifndef NIDAS_UTIL_SOCKET_H
33#define NIDAS_UTIL_SOCKET_H
45#include <sys/socket.h>
50#include <netinet/in.h>
56namespace nidas {
namespace util {
317 size_t recv(
void* buf,
size_t len,
int flags=0);
342 size_t send(
const void* buf,
size_t len,
int flags = 0);
347 size_t send(
const struct iovec* iov,
int iovcnt,
int flags = 0);
352 size_t sendto(
const void* buf,
size_t len,
int flags,
358 size_t sendto(
const struct iovec* iov,
int iovcnt,
int flags,
374 void sendall(
const void* buf,
size_t len,
int flags = 0);
586 Socket(
int domain=AF_INET);
813 size_t recv(
void* buf,
size_t len,
int flags = 0)
826 size_t send(
const void* buf,
size_t len,
int flags=MSG_NOSIGNAL)
834 size_t send(
const struct iovec* iov,
int iovcnt,
int flags=MSG_NOSIGNAL)
851 void sendall(
const void* buf,
size_t len,
int flags=MSG_NOSIGNAL)
1373 size_t send(
const void* buf,
size_t len,
int flags = 0)
1381 size_t send(
const struct iovec* iov,
int iovcnt,
int flags=MSG_NOSIGNAL)
1398 size_t sendto(
const struct iovec* iov,
int iovcnt,
int flags,
Abstract base class for a UDP datagram.
Definition DatagramPacket.h:39
A socket for sending or receiving datagrams, either unicast, broadcast or multicast.
Definition Socket.h:1178
void setTimeout(int val)
Set the timeout for receive(), recv(), and recvfrom() methods.
Definition Socket.h:1245
bool getBroadcastEnable() const
Definition Socket.h:1494
size_t recv(void *buf, size_t len, int flags=0)
Definition Socket.h:1356
int getLocalPort() const
Get local port number of this socket.
Definition Socket.h:1419
void setSendBufferSize(int size)
Definition Socket.h:1462
void setBroadcastEnable(bool val)
Definition Socket.h:1486
size_t recvfrom(void *buf, size_t len, int flags, SocketAddress &from)
Definition Socket.h:1364
virtual ~DatagramSocket()
MulticastSocket is derived from DatagramSocket, so we provide a virtual destructor.
Definition Socket.h:1226
void close()
Definition Socket.h:1233
bool isNonBlocking() const
Definition Socket.h:1438
void setReceiveBufferSize(int size)
Definition Socket.h:1446
void bind(const Inet4Address &addr, int port)
Bind the DatagramSocket to the specified local address.
Definition Socket.h:1294
int getReceiveBufferSize()
Definition Socket.h:1454
void connect(const std::string &host, int port)
Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default d...
Definition Socket.h:1257
void connect(const Inet4Address &addr, int port)
Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default d...
Definition Socket.h:1269
void receive(DatagramPacketBase &packet)
Read a packet from the DatagramSocket.
Definition Socket.h:1323
void connect(const SocketAddress &addr)
Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default d...
Definition Socket.h:1281
void bind(const SocketAddress &addr)
Bind the DatagramSocket to the specified local address.
Definition Socket.h:1307
int getSendBufferSize()
Definition Socket.h:1470
size_t sendto(const void *buf, size_t len, int flags, const SocketAddress &to)
Definition Socket.h:1389
void setPktInfo(bool val)
Control whether a IP_PKTINFO ancillary message is received with each datagram.
Definition Socket.h:1508
SocketImpl _impl
Definition Socket.h:1514
std::list< Inet4NetworkInterface > getInterfaces() const
Definition Socket.h:1478
int getFd() const
Definition Socket.h:1312
size_t sendto(const struct iovec *iov, int iovcnt, int flags, const SocketAddress &to)
Definition Socket.h:1398
void setNonBlocking(bool val)
Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.
Definition Socket.h:1430
size_t send(const struct iovec *iov, int iovcnt, int flags=MSG_NOSIGNAL)
Definition Socket.h:1381
void receive(DatagramPacketBase &packet, Inet4PacketInfo &info, int flags=0)
Read a packet from the DatagramSocket.
Definition Socket.h:1340
size_t send(const void *buf, size_t len, int flags=0)
Definition Socket.h:1373
DatagramSocket()
Create a DatagramSocket not bound to a port.
Definition Socket.cc:1554
void send(const DatagramPacketBase &packet, int flags=0)
Definition Socket.h:1348
const SocketAddress & getLocalSocketAddress() const
Get local address of this socket.
Definition Socket.h:1409
Support for IP version 4 host address.
Definition Inet4Address.h:46
Definition Inet4NetworkInterface.h:36
Ancillary information that can be determined about an incoming UDP packet.
Definition Inet4PacketInfo.h:40
A IP version 4 socket address, containing a host address, and a port number.
Definition Inet4SocketAddress.h:41
A datagram socket to be used for multicasts.
Definition Socket.h:1582
MulticastSocket(int port)
Create multicast socket, bind it to a local port.
Definition Socket.h:1606
MulticastSocket(const Inet4SocketAddress &addr)
Creates a datagram socket and binds it to the specified local address.
Definition Socket.h:1617
int getTimeToLive() const
Definition Socket.h:1677
MulticastSocket()
Create an unbound multicast socket.
Definition Socket.h:1596
Inet4NetworkInterface getInterface() const
Definition Socket.h:1744
void setInterface(Inet4Address maddr, const Inet4NetworkInterface &iface)
Set the interface for a given multicast address.
Definition Socket.h:1722
void joinGroup(Inet4Address groupAddr)
Join a multicast group on all interfaces.
Definition Socket.h:1627
void leaveGroup(Inet4Address groupAddr)
Leave a multicast group on all interfaces.
Definition Socket.h:1652
void setInterface(Inet4Address iaddr)
Set the interface for a given multicast address.
Definition Socket.h:1737
bool getMulticastLoop() const
Definition Socket.h:1703
Inet4NetworkInterface findInterface(const Inet4Address &iaddr) const
Definition Socket.h:1751
void leaveGroup(Inet4Address groupAddr, const Inet4NetworkInterface &iface)
Leave a multicast group on a given interface.
Definition Socket.h:1661
void setMulticastLoop(bool val)
Whether to set the IP_MULTICAST_LOOP socket option.
Definition Socket.h:1695
void setTimeToLive(int val)
Definition Socket.h:1669
void joinGroup(Inet4Address groupAddr, const Inet4NetworkInterface &iface)
Join a multicast group on a given interface.
Definition Socket.h:1643
std::list< Inet4NetworkInterface > getInterfaces() const
Return the IP addresses of all my network interfaces.
Definition Socket.h:1761
A stream (TCP) socket that is used to listen for connections.
Definition Socket.h:981
void close()
Close the socket.
Definition Socket.cc:1537
int getSendBufferSize()
Definition Socket.h:1077
void setSendBufferSize(int size)
Definition Socket.h:1069
void setNonBlocking(bool val)
Definition Socket.h:1106
bool isNonBlocking() const
Definition Socket.h:1114
ServerSocket(int port=0, int backlog=10)
Create a AF_INET ServerSocket bound to a port on all local interfaces.
Definition Socket.cc:1489
SocketImpl _impl
Definition Socket.h:1120
int getReceiveBufferSize()
Definition Socket.h:1061
void setReceiveBufferSize(int size)
Definition Socket.h:1053
~ServerSocket()
Destructor.
Definition Socket.h:1016
int getFd() const
Definition Socket.h:1019
const SocketAddress & getLocalSocketAddress() const
Fetch the local address that this socket is bound to.
Definition Socket.h:1087
int getLocalPort() const
Definition Socket.h:1095
int getDomain() const
Definition Socket.h:1099
Socket * accept()
Accept connection, return a connected Socket instance.
Definition Socket.h:1045
An interface for a socket address.
Definition SocketAddress.h:36
virtual int getPort() const =0
return the port number of this address, or -1 if there is no associated port number,...
Implementation of a socket, providing a C++ interface to system socket calls: socket,...
Definition Socket.h:76
void getLocalAddr()
Do system call to determine local address of this socket.
Definition Socket.cc:378
void setReuseAddress(bool val)
Definition Socket.h:151
void setNonBlocking(bool val)
Definition Socket.cc:426
bool isNonBlocking() const
Definition Socket.cc:451
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:994
size_t sendto(const void *buf, size_t len, int flags, const SocketAddress &to)
Definition Socket.cc:858
void setSendBufferSize(int size)
Definition Socket.cc:926
void leaveGroup(Inet4Address groupAddr)
Leave a multicast group on all interfaces.
Definition Socket.cc:1138
void setReceiveBufferSize(int size)
Definition Socket.cc:906
int getDomain() const
Get the domain of this socket: AF_UNIX, AF_INET, etc, from sys/socket.h.
Definition Socket.h:107
SocketImpl(int domain, int type)
Definition Socket.cc:62
bool getMulticastLoop() const
Definition Socket.cc:1305
void setMulticastLoop(bool val)
Whether to set the IP_MULTICAST_LOOP socket option.
Definition Socket.cc:1294
const SocketAddress & getLocalSocketAddress() const
Get local socket address of this socket.
Definition Socket.h:116
void setInterface(Inet4Address maddr, const Inet4NetworkInterface &iaddr)
Definition Socket.cc:1168
SocketAddress * _remoteaddr
Definition Socket.h:525
bool getTcpNoDelay()
Get the TCP_NODELAY (man 7 tcp) option on the socket.
Definition Socket.cc:958
int getTimeToLive() const
Definition Socket.cc:1281
struct timespec _timeout
Definition Socket.h:530
bool _pktInfo
Definition Socket.h:532
int getKeepAliveIdleSecs() const
Get the current value of TCP_KEEPIDLE on this socket.
Definition Socket.cc:1018
SocketImpl & operator=(const SocketImpl &rhs)
Assignment operator.
Definition Socket.cc:98
void setBroadcastEnable(bool val)
Enable or disable SO_BROADCAST.
Definition Socket.cc:1318
Socket * accept()
Definition Socket.cc:232
SocketAddress * _localaddr
Definition Socket.h:524
void setBacklog(int val)
Definition Socket.h:109
int getTimeout() const
Definition Socket.cc:129
int getOutQueueSize() const
Get number of bytes currently unsent in the local output queue.
Definition Socket.cc:1043
void setPktInfo(bool val)
Control whether a IP_PKTINFO ancillary message is received with each datagram.
Definition Socket.cc:1429
const SocketAddress & getRemoteSocketAddress() const
Get remote socket address of this socket.
Definition Socket.h:136
void connect(const std::string &dest, int port)
Definition Socket.cc:145
bool getPktInfo() const
Definition Socket.h:453
~SocketImpl()
Definition Socket.cc:116
void bind(int port)
Definition Socket.cc:175
int getLocalPort() const
Get local port number of this socket.
Definition Socket.h:126
void joinGroup(Inet4Address groupAddr)
Join a multicast group on all interfaces.
Definition Socket.cc:1057
void getRemoteAddr()
Do system call to determine address of remote end.
Definition Socket.cc:402
int _fd
Definition Socket.h:526
void setTimeToLive(int val)
Definition Socket.cc:1271
Inet4NetworkInterface getInterface() const
Definition Socket.cc:1222
int getReceiveBufferSize()
Definition Socket.cc:914
void receive(DatagramPacketBase &packet)
Definition Socket.cc:462
int _sockdomain
Definition Socket.h:522
void setTimeout(int val)
Set the timeout for receive(), recv(), and recvfrom() methods.
Definition Socket.cc:122
bool _hasTimeout
Definition Socket.h:529
bool getKeepAlive() const
Get the current value of the SO_KEEPALIVE socket option.
Definition Socket.cc:982
int _socktype
Definition Socket.h:523
void close()
Definition Socket.cc:134
std::list< Inet4NetworkInterface > getInterfaces() const
Definition Socket.cc:1382
int _backlog
Definition Socket.h:527
int getFd() const
Definition Socket.h:101
Inet4NetworkInterface findInterface(const Inet4Address &) const
Definition Socket.cc:1202
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:889
bool getBroadcastEnable() const
Definition Socket.cc:1329
bool _reuseaddr
Definition Socket.h:528
int getRemotePort() const
Get remote port number of this socket.
Definition Socket.h:146
void setTcpNoDelay(bool val)
Set the TCP_NODELAY (man 7 tcp) option on the socket.
Definition Socket.cc:946
size_t recvfrom(void *buf, size_t len, int flags, SocketAddress &from)
Definition Socket.cc:746
void setKeepAlive(bool val)
Set or unset the SO_KEEPALIVE socket option.
Definition Socket.cc:970
size_t recv(void *buf, size_t len, int flags=0)
Receive data on a socket.
Definition Socket.cc:679
void send(const DatagramPacketBase &packet, int flags=0)
Definition Socket.cc:817
void listen()
Definition Socket.cc:222
int getInQueueSize() const
Get number of bytes currently unread in the local input queue.
Definition Socket.cc:1031
int getSendBufferSize()
Definition Socket.cc:934
A stream (TCP) socket.
Definition Socket.h:573
static std::vector< Socket * > createSocketPair(int type=SOCK_STREAM)
Create a pair of connnected unix sockets.
Definition Socket.cc:1596
int getSendBufferSize()
Definition Socket.h:883
void close()
Definition Socket.h:629
void setNonBlocking(bool val)
Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.
Definition Socket.h:658
void connect(const std::string &host, int port)
Connect to a given remote host and port.
Definition Socket.h:777
int getKeepAliveIdleSecs()
Get the current value of TCP_KEEPIDLE on this socket.
Definition Socket.h:740
int getLocalPort() const
Get local port number of this socket.
Definition Socket.h:922
~Socket()
Definition Socket.h:623
bool getKeepAlive()
Get the current value of the SO_KEEPALIVE socket option.
Definition Socket.h:702
void setKeepAlive(bool val)
Set or unset the SO_KEEPALIVE socket option.
Definition Socket.h:692
void setSendBufferSize(int size)
Definition Socket.h:875
Socket(int domain=AF_INET)
Create an unconnected stream socket.
Definition Socket.cc:1441
int getInQueueSize() const
Definition Socket.h:748
size_t recv(void *buf, size_t len, int flags=0)
Definition Socket.h:813
void setTcpNoDelay(bool val)
Definition Socket.h:674
void connect(const SocketAddress &addr)
Connect to a given remote socket address.
Definition Socket.h:797
SocketImpl _impl
Definition Socket.h:932
void setReceiveBufferSize(int size)
Definition Socket.h:859
int getRemotePort() const
Get remote port number of this socket.
Definition Socket.h:903
std::list< Inet4NetworkInterface > getInterfaces() const
Get a list of all my network interfaces.
Definition Socket.h:766
int getFd() const
Fetch the file descriptor associate with this socket.
Definition Socket.h:805
int getReceiveBufferSize()
Definition Socket.h:867
const SocketAddress & getRemoteSocketAddress() const
Get remote address of this socket.
Definition Socket.h:893
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:851
int getDomain() const
Definition Socket.h:926
void connect(const Inet4Address &addr, int port)
Connect to a given remote host address and port.
Definition Socket.h:787
int getOutQueueSize() const
Definition Socket.h:756
size_t send(const struct iovec *iov, int iovcnt, int flags=MSG_NOSIGNAL)
Definition Socket.h:834
const SocketAddress & getLocalSocketAddress() const
Get local address of this socket.
Definition Socket.h:912
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:728
bool getTcpNoDelay()
Definition Socket.h:682
void setTimeout(int val)
Set the timeout for receive(), recv(), and recvfrom() methods.
Definition Socket.h:649
bool isNonBlocking() const
Definition Socket.h:666
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:826
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31
static n_u::SerialPort port
Definition sing.cc:68
int len
Definition sing.cc:948
int fd
Definition twod.c:56