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

A stream (TCP) socket that is used to listen for connections. More...

#include <Socket.h>

Public Member Functions

 ServerSocket (int port=0, int backlog=10)
 Create a AF_INET ServerSocket bound to a port on all local interfaces.
 
 ServerSocket (const Inet4Address &bindAddr, int port, int backlog=10)
 Create a ServerSocket bound to port on a given local address, and set listen backlog parameter.
 
 ServerSocket (const SocketAddress &bindAddr, int backlog=10)
 Create a ServerSocket bound to an address, and set listen backlog parameter.
 
 ~ServerSocket ()
 Destructor.
 
int getFd () const
 
void close ()
 Close the socket.
 
Socketaccept ()
 Accept connection, return a connected Socket instance.
 
void setReceiveBufferSize (int size)
 
int getReceiveBufferSize ()
 
void setSendBufferSize (int size)
 
int getSendBufferSize ()
 
const SocketAddressgetLocalSocketAddress () const
 Fetch the local address that this socket is bound to.
 
int getLocalPort () const
 
int getDomain () const
 
void setNonBlocking (bool val)
 
bool isNonBlocking () const
 

Protected Attributes

SocketImpl _impl
 

Detailed Description

A stream (TCP) socket that is used to listen for connections.

This class is patterned after the java.net.ServerSocket class.

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, so it is the user's responsibility to call Socket::close() when finished with the connection.

Usage scenario of a server which listens for connections on port 5000, and spawns a thread to handle each connection.

using namespace nidas::util;
class DetachedSocketThread: public DetachedThread {
public:
// Thread constructor
DetachedSocketThread(Socket s) throw(IOException):
DetachedThread("reader"),sock(s) {}
// Thread run method
int run() throw(nidas::util::Exception) {
for (;;) {
char buf[512];
if (recv(buf,sizeof(buf),0) == 0) break;
...
}
sock.close();
return RUN_OK;
}
// my socket
Socket sock;
};
ServerSocket ssock(5000); // bind and listen on port 5000
for (;;) {
Socket* sock = ssock.accept(); // accept a connection
DetachedSockThread* thrd = new DetachedSockThread(sock);
thrd->start();
// DetachedThreads delete themselves when they're done.
}
A Thread with a constructor that sets detached=true.
Definition Thread.h:558
Definition Exception.h:35
Definition IOException.h:37
A stream (TCP) socket that is used to listen for connections.
Definition Socket.h:981
A stream (TCP) socket.
Definition Socket.h:573
General utility classes. nidas::util contains classes of general utility, like Socket,...
Definition DSMServer.h:37
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31

Constructor & Destructor Documentation

◆ ServerSocket() [1/3]

ServerSocket::ServerSocket ( int port = 0,
int backlog = 10 )

Create a AF_INET ServerSocket bound to a port on all local interfaces.

Parameters
portPort number, 0<=port<=65535. If zero, the system will select an available port number. To find out which port number was selected, use getLocalPort().
Exceptions
IOException

References _impl, nidas::util::SocketImpl::bind(), close(), port, nidas::util::SocketImpl::setBacklog(), and nidas::util::SocketImpl::setNonBlocking().

◆ ServerSocket() [2/3]

ServerSocket::ServerSocket ( const Inet4Address & bindAddr,
int port,
int backlog = 10 )

Create a ServerSocket bound to port on a given local address, and set listen backlog parameter.

Exceptions
IOException

References _impl, nidas::util::SocketImpl::bind(), close(), port, nidas::util::SocketImpl::setBacklog(), and nidas::util::SocketImpl::setNonBlocking().

◆ ServerSocket() [3/3]

ServerSocket::ServerSocket ( const SocketAddress & bindAddr,
int backlog = 10 )

Create a ServerSocket bound to an address, and set listen backlog parameter.

Exceptions
IOException

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

◆ ~ServerSocket()

nidas::util::ServerSocket::~ServerSocket ( )
inline

Destructor.

Does not close the socket. You must explicitly close it with the close method.

Exceptions

)

Member Function Documentation

◆ accept()

Socket * nidas::util::ServerSocket::accept ( )
inline

Accept connection, return a connected Socket instance.

This method does the following in addition to the basic accept() system call.

  1. The pselect or ppoll system calls are used to wait on the socket file descriptor, with SIGUSR1 unset in the signal mask. In order to catch a SIGUSR1 signal with this accept() method, SIGUSR1 should first be blocked in the thread before calling accept(). If SIGUSR1 or any other signal is caught by this method, it will throw an IOException with a getErrno() of EINTR. The actual value of the signal is not available.
  2. If the accept() system call returns EAGAIN, EWOULDBLOCK or ECONNABORTED, the pselect()/ppoll() and accept() system calls are retried.
Exceptions
IOException

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

Referenced by nidas::core::ServerSocket::connect(), and nidas::core::ServerSocketIODevice::open().

◆ close()

void ServerSocket::close ( )

◆ getDomain()

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

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

Referenced by close().

◆ getFd()

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

◆ getLocalPort()

int nidas::util::ServerSocket::getLocalPort ( ) const
inline
Exceptions

)

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

◆ getLocalSocketAddress()

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

Fetch the local address that this socket is bound to.

Exceptions

)

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

Referenced by close(), and nidas::core::RemoteSerialListener::getName().

◆ getReceiveBufferSize()

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

◆ getSendBufferSize()

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

◆ isNonBlocking()

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

◆ setNonBlocking()

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

◆ setReceiveBufferSize()

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

◆ setSendBufferSize()

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

Member Data Documentation

◆ _impl

SocketImpl nidas::util::ServerSocket::_impl
protected

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