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

Static Public Member Functions

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

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();

Constructor & Destructor Documentation

Socket::Socket ( int  domain = AF_INET)
throw (IOException
)

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.

Socket::Socket ( const SocketAddress addr)
throw (IOException
)

Create a stream socket connected to a remote address.

The socket domain will match the domain of the SocketAddress.

Socket::Socket ( int  fd,
const SocketAddress raddr 
)
throw (IOException
)

Called by ServerSocket after a connection is accepted.

The socket domain will match the domain of the SocketAddress.

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

Member Function Documentation

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

Connect to a given remote host and port.

Referenced by nidas::core::Socket::connect(), main(), and nidas::util::McSocketListener::run().

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

Connect to a given remote host address and port.

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

Connect to a given remote socket address.

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

Create a pair of connnected unix sockets.

See man page for socketpair() system call.

int nidas::util::Socket::getDomain ( ) const
inline
int nidas::util::Socket::getFd ( ) const
inline
int nidas::util::Socket::getInQueueSize ( ) const
throw (IOException
)
inline
std::list<Inet4NetworkInterface> nidas::util::Socket::getInterfaces ( ) const
throw (IOException
)
inline
bool nidas::util::Socket::getKeepAlive ( )
throw (IOException
)
inline

Get the current value of the SO_KEEPALIVE socket option.

int nidas::util::Socket::getKeepAliveIdleSecs ( )
throw (IOException
)
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).

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

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

Get local port number of this socket.

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

Get local address of this socket.

Referenced by nidas::core::Socket::connect(), and nidas::util::McSocketMulticaster< SocketTT >::run().

int nidas::util::Socket::getOutQueueSize ( ) const
throw (IOException
)
inline
int nidas::util::Socket::getReceiveBufferSize ( )
throw (IOException
)
inline
int nidas::util::Socket::getRemotePort ( ) const
throw (
)
inline

Get remote port number of this socket.

const SocketAddress& nidas::util::Socket::getRemoteSocketAddress ( ) const
throw (
)
inline
int nidas::util::Socket::getSendBufferSize ( )
throw (IOException
)
inline
bool nidas::util::Socket::getTcpNoDelay ( )
throw (IOException
)
inline
bool nidas::util::Socket::isNonBlocking ( ) const
throw (IOException
)
inline
size_t nidas::util::Socket::recv ( void *  buf,
size_t  len,
int  flags = 0 
)
throw (IOException
)
inline
size_t nidas::util::Socket::send ( const void *  buf,
size_t  len,
int  flags = MSG_NOSIGNAL 
)
throw (IOException
)
inline

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

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

Referenced by main(), nidas::core::RemoteSerialConnection::sensorNotFound(), nidas::core::TCPSocketIODevice::write(), nidas::core::ServerSocketIODevice::write(), and nidas::core::Socket::write().

size_t nidas::util::Socket::send ( const struct iovec *  iov,
int  iovcnt,
int  flags = MSG_NOSIGNAL 
)
throw (IOException
)
inline
void nidas::util::Socket::sendall ( const void *  buf,
size_t  len,
int  flags = MSG_NOSIGNAL 
)
throw (IOException
)
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. bitwise OR of flags for send. Default: MSG_NOSIGNAL. It is not recommended to use this method if using non-blocking IO.

Referenced by RemoteSerial::run().

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

Set or unset the SO_KEEPALIVE socket option.

Referenced by nidas::dynld::UDPSampleOutput::XMLSocketListener::run().

void nidas::util::Socket::setKeepAliveIdleSecs ( int  val)
throw (IOException
)
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. 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.

Referenced by nidas::core::Socket::connect(), nidas::core::McSocket::connect(), nidas::core::ServerSocket::connect(), nidas::core::McSocket::connected(), StatsProcess::run(), nidas::dynld::UDPSampleOutput::XMLSocketListener::run(), nidas::core::ServerSocket::ConnectionThread::run(), ServerThread::run(), and nidas::core::Socket::setKeepAliveIdleSecs().

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

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

Parameters
valtrue=set O_NONBLOCK, false=unset O_NONBLOCK.

Referenced by nidas::core::Socket::connect(), nidas::core::McSocket::connect(), nidas::core::McSocket::connected(), nidas::core::ServerSocket::ConnectionThread::run(), and nidas::core::Socket::setNonBlocking().

void nidas::util::Socket::setReceiveBufferSize ( int  size)
throw (IOException
)
inline
void nidas::util::Socket::setSendBufferSize ( int  size)
throw (IOException
)
inline
void nidas::util::Socket::setTcpNoDelay ( bool  val)
throw (IOException
)
inline
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.

Referenced by nidas::core::ServerSocketIODevice::read(), and nidas::dynld::UDPSampleOutput::XMLSocketListener::run().

nidas::util::Socket::throw ( IOException  )

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

nidas::util::Socket::throw ( UnknownHostException  ,
IOException   
)

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

Member Data Documentation

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

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