nidas v1.2.3
|
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< Inet4NetworkInterface > | getInterfaces () 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 SocketAddress & | getRemoteSocketAddress () const |
Get remote address of this socket. | |
int | getRemotePort () const |
Get remote port number of this socket. | |
const SocketAddress & | getLocalSocketAddress () 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 |
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 | ) |
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.
IOException |
Referenced by createSocketPair().
Socket::Socket | ( | const Inet4Address & | addr, |
int | port ) |
Create an Internet domain stream socket connected to a remote address and port.
IOException |
Socket::Socket | ( | const std::string & | host, |
int | port ) |
Create an Internet domain stream socket connected to a remote host and port.
UnknownHostException | |
IOException |
Socket::Socket | ( | const SocketAddress & | addr | ) |
Create a stream socket connected to a remote address.
The socket domain will match the domain of the SocketAddress.
IOException |
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.
IOException |
|
inline |
|
inline |
IOException |
References _impl, and nidas::util::SocketImpl::close().
Referenced by nidas::core::RemoteSerialConnection::close(), nidas::core::ServerSocketIODevice::close(), nidas::core::Socket::close(), nidas::core::TCPSocketIODevice::close(), Socket(), Socket(), Socket(), nidas::dynld::UDPSampleOutput::ConnectionMonitor::updatePollfds(), nidas::dynld::UDPSampleOutput::ConnectionMonitor::~ConnectionMonitor(), and RemoteSerial::~RemoteSerial().
|
inline |
Connect to a given remote host address and port.
IOException |
References _impl, nidas::util::SocketImpl::connect(), and port.
|
inline |
Connect to a given remote socket address.
IOException |
References _impl, and nidas::util::SocketImpl::connect().
|
inline |
Connect to a given remote host and port.
IOException | |
UnknownHostException |
References _impl, nidas::util::SocketImpl::connect(), and port.
Referenced by nidas::core::Socket::connect(), main(), nidas::core::TCPSocketIODevice::open(), Socket(), Socket(), and Socket().
|
static |
Create a pair of connnected unix sockets.
See man page for socketpair() system call.
IOException |
References Socket().
|
inline |
References _impl, and nidas::util::SocketImpl::getDomain().
|
inline |
Fetch the file descriptor associate with this socket.
References _impl, and nidas::util::SocketImpl::getFd().
Referenced by nidas::core::RemoteSerialConnection::close(), nidas::core::ServerSocketIODevice::close(), nidas::core::TCPSocketIODevice::close(), nidas::core::RemoteSerialConnection::getFd(), nidas::core::Socket::getFd(), nidas::core::ServerSocketIODevice::getReadFd(), nidas::core::TCPSocketIODevice::getReadFd(), nidas::core::ServerSocketIODevice::getWriteFd(), nidas::core::TCPSocketIODevice::getWriteFd(), RemoteSerial::run(), and nidas::core::RemoteSerialConnection::setSensor().
|
inline |
IOException |
References _impl, and nidas::util::SocketImpl::getInQueueSize().
|
inline |
Get a list of all my network interfaces.
IOException |
References _impl, and nidas::util::SocketImpl::getInterfaces().
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.
IOException |
References _impl, and nidas::util::SocketImpl::getKeepAlive().
|
inline |
Get the current value of TCP_KEEPIDLE on this socket.
IOException |
References _impl, and nidas::util::SocketImpl::getKeepAliveIdleSecs().
Referenced by nidas::core::Socket::getKeepAliveIdleSecs(), and nidas::core::Socket::Socket().
|
inline |
Get local port number of this socket.
)
References _impl, and nidas::util::SocketImpl::getLocalPort().
|
inline |
Get local address of this socket.
)
References _impl, and nidas::util::SocketImpl::getLocalSocketAddress().
Referenced by nidas::core::Socket::connect(), and nidas::util::McSocketMulticaster< SocketTT >::run().
|
inline |
IOException |
References _impl, and nidas::util::SocketImpl::getOutQueueSize().
|
inline |
IOException |
References _impl, and nidas::util::SocketImpl::getReceiveBufferSize().
Referenced by nidas::core::Socket::getBufferSize().
|
inline |
Get remote port number of this socket.
)
References _impl, and nidas::util::SocketImpl::getRemotePort().
|
inline |
Get remote address of this socket.
)
References _impl, and nidas::util::SocketImpl::getRemoteSocketAddress().
Referenced by nidas::core::Socket::connect(), main(), RemoteSerial::run(), nidas::util::McSocketMulticaster< SocketTT >::run(), and nidas::core::RemoteSerialConnection::setSensor().
|
inline |
IOException |
References _impl, and nidas::util::SocketImpl::getSendBufferSize().
|
inline |
IOException |
References _impl, and nidas::util::SocketImpl::getTcpNoDelay().
|
inline |
IOException |
References _impl, and nidas::util::SocketImpl::isNonBlocking().
Referenced by nidas::core::Socket::isNonBlocking(), and nidas::core::Socket::Socket().
|
inline |
IOException |
References _impl, len, and nidas::util::SocketImpl::recv().
Referenced by nidas::core::ServerSocketIODevice::read(), nidas::core::Socket::read(), nidas::core::TCPSocketIODevice::read(), nidas::core::ServerSocketIODevice::read(), nidas::core::TCPSocketIODevice::read(), nidas::core::RemoteSerialConnection::readSensorName(), RemoteSerial::run(), and RemoteSerial::socketReadLine().
|
inline |
IOException |
References _impl, and nidas::util::SocketImpl::send().
|
inline |
send data on socket, see man page for send system function.
buf | pointer to buffer. |
len | number of bytes to send. @flags bitwise OR of flags for send. Default: MSG_NOSIGNAL. |
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().
|
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. @flags bitwise OR of flags for send. Default: MSG_NOSIGNAL. |
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().
|
inline |
Set or unset the SO_KEEPALIVE socket option.
IOException |
References _impl, and nidas::util::SocketImpl::setKeepAlive().
|
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. |
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().
|
inline |
Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.
val | true=set O_NONBLOCK, false=unset O_NONBLOCK. |
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().
|
inline |
IOException |
References _impl, and nidas::util::SocketImpl::setReceiveBufferSize().
|
inline |
IOException |
References _impl, and nidas::util::SocketImpl::setSendBufferSize().
|
inline |
IOException |
References _impl, and nidas::util::SocketImpl::setTcpNoDelay().
Referenced by nidas::core::ServerSocketIODevice::open(), and nidas::core::TCPSocketIODevice::open().
|
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. |
References _impl, and nidas::util::SocketImpl::setTimeout().
Referenced by nidas::core::ServerSocketIODevice::read(), and nidas::core::TCPSocketIODevice::read().
|
protected |
Referenced by close(), connect(), connect(), connect(), getDomain(), getFd(), getInQueueSize(), getInterfaces(), getKeepAlive(), getKeepAliveIdleSecs(), getLocalPort(), getLocalSocketAddress(), getOutQueueSize(), getReceiveBufferSize(), getRemotePort(), getRemoteSocketAddress(), getSendBufferSize(), getTcpNoDelay(), isNonBlocking(), recv(), send(), send(), sendall(), setKeepAlive(), setKeepAliveIdleSecs(), setNonBlocking(), setReceiveBufferSize(), setSendBufferSize(), setTcpNoDelay(), and setTimeout().