nidas  v1.2-1520
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) throw (IOException)
 Create a AF_INET ServerSocket bound to a port on all local interfaces. More...
 
 throw (IOException)
 Create a ServerSocket bound to port on a given local address, and set listen backlog parameter. More...
 
 throw (IOException)
 Create a ServerSocket bound to an address, and set listen backlog parameter. More...
 
 ~ServerSocket () throw ()
 Destructor. More...
 
int getFd () const
 
void close () throw (IOException)
 close the socket. More...
 
Socketaccept () throw (IOException)
 Accept connection, return a connected Socket instance. More...
 
void setReceiveBufferSize (int size) throw (IOException)
 
int getReceiveBufferSize () throw (IOException)
 
void setSendBufferSize (int size) throw (IOException)
 
int getSendBufferSize () throw (IOException)
 
const SocketAddressgetLocalSocketAddress () const throw ()
 Fetch the local address that this socket is bound to. More...
 
int getLocalPort () const throw ()
 
int getDomain () const
 
void setNonBlocking (bool val) throw (IOException)
 
bool isNonBlocking () const throw (IOException)
 

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.
}

Constructor & Destructor Documentation

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

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

References port.

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

Destructor.

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

Member Function Documentation

Socket* nidas::util::ServerSocket::accept ( )
throw (IOException
)
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.

Referenced by nidas::core::ServerSocket::connect(), main(), nidas::core::ServerSocketIODevice::open(), PSI::run(), ServerThread::run(), and nidas::util::McSocketMulticaster< SocketTT >::run().

void ServerSocket::close ( )
throw (IOException
)
int nidas::util::ServerSocket::getDomain ( ) const
inline

Referenced by close().

int nidas::util::ServerSocket::getFd ( ) const
inline
int nidas::util::ServerSocket::getLocalPort ( ) const
throw (
)
inline
const SocketAddress& nidas::util::ServerSocket::getLocalSocketAddress ( ) const
throw (
)
inline

Fetch the local address that this socket is bound to.

Referenced by close(), nidas::core::RemoteSerialListener::getName(), and nidas::util::McSocketMulticaster< SocketTT >::run().

int nidas::util::ServerSocket::getReceiveBufferSize ( )
throw (IOException
)
inline
int nidas::util::ServerSocket::getSendBufferSize ( )
throw (IOException
)
inline
bool nidas::util::ServerSocket::isNonBlocking ( ) const
throw (IOException
)
inline
void nidas::util::ServerSocket::setNonBlocking ( bool  val)
throw (IOException
)
inline
void nidas::util::ServerSocket::setReceiveBufferSize ( int  size)
throw (IOException
)
inline
void nidas::util::ServerSocket::setSendBufferSize ( int  size)
throw (IOException
)
inline
nidas::util::ServerSocket::throw ( IOException  )

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

nidas::util::ServerSocket::throw ( IOException  )

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

Member Data Documentation

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

Referenced by close().


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