nidas
v1.2-1520
|
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< Inet4NetworkInterface > | getInterfaces () 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 SocketAddress & | getRemoteSocketAddress () const throw () |
Get remote address of this socket. More... | |
int | getRemotePort () const throw () |
Get remote port number of this socket. More... | |
const SocketAddress & | getLocalSocketAddress () 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 |
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:
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.
|
inline |
|
inline |
Referenced by nidas::core::RemoteSerialConnection::close(), nidas::core::TCPSocketIODevice::close(), nidas::core::ServerSocketIODevice::close(), nidas::core::Socket::close(), nidas::util::Inet4NetworkInterface::getInterface(), main(), nidas::dynld::UDPSampleOutput::XMLSocketListener::run(), nidas::util::McSocketListener::run(), nidas::dynld::UDPSampleOutput::ConnectionMonitor::updatePollfds(), nidas::dynld::UDPSampleOutput::ConnectionMonitor::~ConnectionMonitor(), and RemoteSerial::~RemoteSerial().
|
inline |
Connect to a given remote host and port.
Referenced by nidas::core::Socket::connect(), main(), and nidas::util::McSocketListener::run().
|
inline |
Connect to a given remote host address and port.
|
inline |
Connect to a given remote socket address.
|
static |
Create a pair of connnected unix sockets.
See man page for socketpair() system call.
|
inline |
|
inline |
Fetch the file descriptor associate with this socket.
Referenced by nidas::core::RemoteSerialConnection::close(), nidas::core::TCPSocketIODevice::close(), nidas::core::ServerSocketIODevice::close(), nidas::core::RemoteSerialConnection::getFd(), nidas::core::Socket::getFd(), nidas::core::TCPSocketIODevice::getReadFd(), nidas::core::ServerSocketIODevice::getReadFd(), nidas::core::TCPSocketIODevice::getWriteFd(), nidas::core::ServerSocketIODevice::getWriteFd(), RemoteSerial::run(), and nidas::dynld::UDPSampleOutput::ConnectionMonitor::updatePollfds().
|
inline |
|
inline |
Get a list of all my network interfaces.
Referenced by nidas::core::Socket::connect(), nidas::util::Inet4NetworkInterface::getInterface(), and nidas::core::Socket::getInterfaces().
|
inline |
Get the current value of the SO_KEEPALIVE socket option.
|
inline |
Get the current value of TCP_KEEPIDLE on this socket.
Referenced by nidas::core::Socket::getKeepAliveIdleSecs(), and nidas::core::Socket::Socket().
|
inline |
Get local port number of this socket.
|
inline |
Get local address of this socket.
Referenced by nidas::core::Socket::connect(), and nidas::util::McSocketMulticaster< SocketTT >::run().
|
inline |
|
inline |
Referenced by nidas::core::Socket::getBufferSize().
|
inline |
Get remote port number of this socket.
|
inline |
Get remote address of this socket.
Referenced by nidas::dynld::UDPSampleOutput::ConnectionMonitor::addConnection(), nidas::core::Socket::connect(), main(), nidas::core::RemoteSerialConnection::RemoteSerialConnection(), nidas::dynld::UDPSampleOutput::ConnectionMonitor::removeConnection(), StatsProcess::run(), RemoteSerial::run(), nidas::dynld::UDPSampleOutput::ConnectionMonitor::run(), nidas::dynld::UDPSampleOutput::XMLSocketListener::run(), ServerThread::run(), and nidas::util::McSocketMulticaster< SocketTT >::run().
|
inline |
|
inline |
|
inline |
Referenced by nidas::core::Socket::isNonBlocking(), and nidas::core::Socket::Socket().
|
inline |
Referenced by main(), nidas::core::TCPSocketIODevice::read(), nidas::core::ServerSocketIODevice::read(), nidas::core::Socket::read(), nidas::core::RemoteSerialConnection::readSensorName(), RemoteSerial::run(), nidas::dynld::UDPSampleOutput::XMLSocketListener::run(), and RemoteSerial::socketReadLine().
|
inline |
send data on socket, see man page for send system function.
buf | pointer to buffer. |
len | number 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().
|
inline |
|
inline |
send all data in buffer on socket, repeating send() as necessary, until all data is sent (or an exception occurs).
buf | pointer to buffer. |
len | number 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().
|
inline |
Set or unset the SO_KEEPALIVE socket option.
Referenced by nidas::dynld::UDPSampleOutput::XMLSocketListener::run().
|
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.
val | Number 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().
|
inline |
Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.
val | true=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().
|
inline |
|
inline |
|
inline |
|
inline |
Set the timeout for receive(), recv(), and recvfrom() methods.
val | timeout 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.
|
protected |