nidas v1.2.3
Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
nidas::util::Socket Class Reference

A stream (TCP) socket. More...

#include <Socket.h>

Public Member Functions

 Socket (int domain=AF_INET)
 Create an unconnected stream socket.
 
 Socket (const Inet4Address &addr, int port)
 Create an Internet domain stream socket connected to a remote address and port.
 
 Socket (const std::string &host, int port)
 Create an Internet domain stream socket connected to a remote host and port.
 
 Socket (const SocketAddress &addr)
 Create a stream socket connected to a remote address.
 
 Socket (int fd, const SocketAddress &raddr)
 Called by ServerSocket after a connection is accepted.
 
 ~Socket () throw ()
 
void close ()
 
void setTimeout (int val)
 Set the timeout for receive(), recv(), and recvfrom() methods.
 
void setNonBlocking (bool val)
 Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.
 
bool isNonBlocking () const
 
void setTcpNoDelay (bool val)
 
bool getTcpNoDelay ()
 
void setKeepAlive (bool val)
 Set or unset the SO_KEEPALIVE socket option.
 
bool getKeepAlive ()
 Get the current value of the SO_KEEPALIVE socket option.
 
void setKeepAliveIdleSecs (int val)
 Set the number of seconds a connection needs to be idle before TCP begins sending out keep-alive probes (TCP_KEEPIDLE).
 
int getKeepAliveIdleSecs ()
 Get the current value of TCP_KEEPIDLE on this socket.
 
int getInQueueSize () const
 
int getOutQueueSize () const
 
std::list< Inet4NetworkInterfacegetInterfaces () const
 Get a list of all my network interfaces.
 
void connect (const std::string &host, int port)
 Connect to a given remote host and port.
 
void connect (const Inet4Address &addr, int port)
 Connect to a given remote host address and port.
 
void connect (const SocketAddress &addr)
 Connect to a given remote socket address.
 
int getFd () const
 Fetch the file descriptor associate with this socket.
 
size_t recv (void *buf, size_t len, int flags=0)
 
size_t send (const void *buf, size_t len, int flags=MSG_NOSIGNAL)
 send data on socket, see man page for send system function.
 
size_t send (const struct iovec *iov, int iovcnt, int flags=MSG_NOSIGNAL)
 
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 exception occurs).
 
void setReceiveBufferSize (int size)
 
int getReceiveBufferSize ()
 
void setSendBufferSize (int size)
 
int getSendBufferSize ()
 
const SocketAddressgetRemoteSocketAddress () const
 Get remote address of this socket.
 
int getRemotePort () const
 Get remote port number of this socket.
 
const SocketAddressgetLocalSocketAddress () const
 Get local address of this socket.
 
int getLocalPort () const
 Get local port number of this socket.
 
int getDomain () const
 

Static Public Member Functions

static std::vector< Socket * > createSocketPair (int type=SOCK_STREAM)
 Create a pair of connnected unix sockets.
 

Protected Attributes

SocketImpl _impl
 

Detailed Description

A stream (TCP) socket.

This class is patterned after the java.net.Socket class. The Socket will be either a AF_INET or AF_UNIX socket depending on the domain or address family of the SocketAddressess argument to the constructor.

This class provides the public copy constructors and assignment operators. Objects of this class can be copied and assigned to without restriction. However, because of this, the destructor does not close the socket file descriptor, so, in general, you should make sure that the socket is closed once at some point.

Usage scenario:

// create a connection to host big_server, on port 5000
Socket sock("big_server",5000);
for (int i = 0; i < 10; i++) {
sock.send("hello\n",6,0);
}
sock.close();
A stream (TCP) socket.
Definition Socket.h:573

Constructor & Destructor Documentation

◆ Socket() [1/5]

Socket::Socket ( int domain = AF_INET)

Create an unconnected stream socket.

Caution: this no-arg constructor opens a file descriptor, and the destructor does not close the file descriptor. You must either do Socket::close() to close it, or make a copy of Socket, and close the copy, since the new copy will own the file descriptor.

Exceptions
IOException

Referenced by createSocketPair().

◆ Socket() [2/5]

Socket::Socket ( const Inet4Address & addr,
int port )

Create an Internet domain stream socket connected to a remote address and port.

Exceptions
IOException

References close(), connect(), and port.

◆ Socket() [3/5]

Socket::Socket ( const std::string & host,
int port )

Create an Internet domain stream socket connected to a remote host and port.

Exceptions
UnknownHostException
IOException

References close(), connect(), and port.

◆ Socket() [4/5]

Socket::Socket ( const SocketAddress & addr)

Create a stream socket connected to a remote address.

The socket domain will match the domain of the SocketAddress.

Exceptions
IOException

References close(), and connect().

◆ Socket() [5/5]

Socket::Socket ( int fd,
const SocketAddress & raddr )

Called by ServerSocket after a connection is accepted.

The socket domain will match the domain of the SocketAddress.

Exceptions
IOException

◆ ~Socket()

nidas::util::Socket::~Socket ( )
throw ( )
inline

Member Function Documentation

◆ close()

void nidas::util::Socket::close ( )
inline

◆ connect() [1/3]

void nidas::util::Socket::connect ( const Inet4Address & addr,
int port )
inline

Connect to a given remote host address and port.

Exceptions
IOException

References _impl, nidas::util::SocketImpl::connect(), and port.

◆ connect() [2/3]

void nidas::util::Socket::connect ( const SocketAddress & addr)
inline

Connect to a given remote socket address.

Exceptions
IOException

References _impl, and nidas::util::SocketImpl::connect().

◆ connect() [3/3]

void nidas::util::Socket::connect ( const std::string & host,
int port )
inline

◆ createSocketPair()

vector< Socket * > Socket::createSocketPair ( int type = SOCK_STREAM)
static

Create a pair of connnected unix sockets.

See man page for socketpair() system call.

Exceptions
IOException

References Socket().

◆ getDomain()

int nidas::util::Socket::getDomain ( ) const
inline

◆ getFd()

int nidas::util::Socket::getFd ( ) const
inline

◆ getInQueueSize()

int nidas::util::Socket::getInQueueSize ( ) const
inline

◆ getInterfaces()

std::list< Inet4NetworkInterface > nidas::util::Socket::getInterfaces ( ) const
inline

◆ getKeepAlive()

bool nidas::util::Socket::getKeepAlive ( )
inline

Get the current value of the SO_KEEPALIVE socket option.

Exceptions
IOException

References _impl, and nidas::util::SocketImpl::getKeepAlive().

◆ getKeepAliveIdleSecs()

int nidas::util::Socket::getKeepAliveIdleSecs ( )
inline

Get the current value of TCP_KEEPIDLE on this socket.

Returns
Number of seconds a connection needs to be idle before TCP begins sending out keep-alive probes (TCP_KEEPIDLE).
Exceptions
IOException

References _impl, and nidas::util::SocketImpl::getKeepAliveIdleSecs().

Referenced by nidas::core::Socket::getKeepAliveIdleSecs(), and nidas::core::Socket::Socket().

◆ getLocalPort()

int nidas::util::Socket::getLocalPort ( ) const
inline

Get local port number of this socket.

Exceptions

)

References _impl, and nidas::util::SocketImpl::getLocalPort().

◆ getLocalSocketAddress()

const SocketAddress & nidas::util::Socket::getLocalSocketAddress ( ) const
inline

◆ getOutQueueSize()

int nidas::util::Socket::getOutQueueSize ( ) const
inline

◆ getReceiveBufferSize()

int nidas::util::Socket::getReceiveBufferSize ( )
inline

◆ getRemotePort()

int nidas::util::Socket::getRemotePort ( ) const
inline

Get remote port number of this socket.

Exceptions

)

References _impl, and nidas::util::SocketImpl::getRemotePort().

◆ getRemoteSocketAddress()

const SocketAddress & nidas::util::Socket::getRemoteSocketAddress ( ) const
inline

◆ getSendBufferSize()

int nidas::util::Socket::getSendBufferSize ( )
inline

◆ getTcpNoDelay()

bool nidas::util::Socket::getTcpNoDelay ( )
inline

◆ isNonBlocking()

bool nidas::util::Socket::isNonBlocking ( ) const
inline

◆ recv()

size_t nidas::util::Socket::recv ( void * buf,
size_t len,
int flags = 0 )
inline

◆ send() [1/2]

size_t nidas::util::Socket::send ( const struct iovec * iov,
int iovcnt,
int flags = MSG_NOSIGNAL )
inline
Exceptions
IOException

References _impl, and nidas::util::SocketImpl::send().

◆ send() [2/2]

size_t nidas::util::Socket::send ( const void * buf,
size_t len,
int flags = MSG_NOSIGNAL )
inline

send data on socket, see man page for send system function.

Parameters
bufpointer to buffer.
lennumber of bytes to send. @flags bitwise OR of flags for send. Default: MSG_NOSIGNAL.
Exceptions
IOException

References _impl, len, and nidas::util::SocketImpl::send().

Referenced by nidas::core::RemoteSerialConnection::doEscCmds(), DataWriter::run(), nidas::core::RemoteSerialConnection::sensorNotFound(), nidas::core::RemoteSerialConnection::setSensor(), nidas::core::Socket::write(), nidas::core::ServerSocketIODevice::write(), nidas::core::Socket::write(), and nidas::core::TCPSocketIODevice::write().

◆ sendall()

void nidas::util::Socket::sendall ( const void * buf,
size_t len,
int flags = MSG_NOSIGNAL )
inline

send all data in buffer on socket, repeating send() as necessary, until all data is sent (or an exception occurs).

Parameters
bufpointer to buffer.
lennumber of bytes to send. @flags bitwise OR of flags for send. Default: MSG_NOSIGNAL.
Exceptions
IOException

It is not recommended to use this method if using non-blocking IO.

References _impl, len, and nidas::util::SocketImpl::sendall().

Referenced by RemoteSerial::openConnection(), and RemoteSerial::run().

◆ setKeepAlive()

void nidas::util::Socket::setKeepAlive ( bool val)
inline

Set or unset the SO_KEEPALIVE socket option.

Exceptions
IOException

References _impl, and nidas::util::SocketImpl::setKeepAlive().

◆ setKeepAliveIdleSecs()

void nidas::util::Socket::setKeepAliveIdleSecs ( int val)
inline

Set the number of seconds a connection needs to be idle before TCP begins sending out keep-alive probes (TCP_KEEPIDLE).

Only appropriate for stream (TCP) connections, not datagrams (UDP). Calls setKeepAlive(true) if necessary so that the SO_KEEPALIVE option is set.

Parameters
valNumber of seconds.
Exceptions
IOException

man 7 tcp: tcp_keepalive_time The number of seconds a connection needs to be idle before TCP begins send- ing out keep-alive probes. Keep-alives are only sent when the SO_KEEPALIVE socket option is enabled. The default value is 7200 seconds (2 hours). An idle connection is terminated after approximately an additional 11 minutes (9 probes an interval of 75 seconds apart) when keep-alive is enabled.

Note that underlying connection tracking mechanisms and application timeouts may be much shorter.

References _impl, and nidas::util::SocketImpl::setKeepAliveIdleSecs().

Referenced by nidas::core::McSocket::connect(), nidas::core::Socket::connect(), nidas::core::ServerSocket::connect(), nidas::core::TCPSocketIODevice::open(), ServerThread::run(), and nidas::core::Socket::setKeepAliveIdleSecs().

◆ setNonBlocking()

void nidas::util::Socket::setNonBlocking ( bool val)
inline

Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.

Parameters
valtrue=set O_NONBLOCK, false=unset O_NONBLOCK.
Exceptions
IOException

References _impl, and nidas::util::SocketImpl::setNonBlocking().

Referenced by nidas::core::Socket::connect(), nidas::core::ServerSocketIODevice::open(), nidas::core::Socket::setNonBlocking(), and nidas::core::RemoteSerialConnection::setSensor().

◆ setReceiveBufferSize()

void nidas::util::Socket::setReceiveBufferSize ( int size)
inline

◆ setSendBufferSize()

void nidas::util::Socket::setSendBufferSize ( int size)
inline

◆ setTcpNoDelay()

void nidas::util::Socket::setTcpNoDelay ( bool val)
inline

◆ setTimeout()

void nidas::util::Socket::setTimeout ( int val)
inline

Set the timeout for receive(), recv(), and recvfrom() methods.

Parameters
valtimeout in milliseconds. 0=no timeout (infinite) The receive methods will return IOTimeoutException if an operation times out.

References _impl, and nidas::util::SocketImpl::setTimeout().

Referenced by nidas::core::ServerSocketIODevice::read(), and nidas::core::TCPSocketIODevice::read().

Member Data Documentation

◆ _impl

SocketImpl nidas::util::Socket::_impl
protected

The documentation for this class was generated from the following files: