Dv::Net::ServerSocket Class Reference

A class representing an internet server. More...

#include <serversocket.h>

Inheritance diagram for Dv::Net::ServerSocket:
Inheritance graph
[legend]
Collaboration diagram for Dv::Net::ServerSocket:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ServerSocket (int port=0, int backlog=10, const std::string &address=std::string(), unsigned int min_debug_level=0, Debugable *debug_master=0) throw (std::runtime_error)
 Start a server on port.
virtual ~ServerSocket ()
 Destructor; also closes the socket.
int port ()
unsigned long address ()
Dv::shared_ptr< InetAddressinetaddress ()
virtual Dv::shared_ptr< Socketaccept (time_t delay=0, size_t bufsize=1024, bool non_blocking=false)
 Accept a new connection from a client.
virtual Dv::shared_ptr< Socketaccept_or_timeout (time_t timeout, time_t delay=0, size_t bufsize=1024, bool non_blocking=false)
 Accept a new connection from a client within a certain delay.
bool connection (time_t timeout, int *syserr=0)
 Check whether connections are waiting to be accepted.
int fd () const
void close ()
 Closes underlying socket, makes ServerSocket unusable by performing shut_down(2) en close(2) on it and setting the underlying file descriptor to -1.

Protected Member Functions

int fd_accept ()
 Auxiliary function for accept().

Private Member Functions

 ServerSocket (const ServerSocket &)
 Forbidden.
ServerSocketoperator= (const ServerSocket &)
 Forbidden.

Private Attributes

int socket_fd_
void * server_address_
 A pointer to a sockaddr_in; server_address_ is opaque so we don't need to include config.h.
bool closed_

Detailed Description

A class representing an internet server.

Example usage:

 try {
   Dv::Net::ServerSocket server(server-port);
   Dv::Util::shared_ptr<Socket> client(server.accept());
   std::cerr << "Connection from " << client.tostring() << std::endl;

   std::string request;
   std::string reply;

   while (*client>>request)
     *client << reply;
 }
 catch (std::runtime_error& e) {
   std::cerr << e.what() << std::endl;
   return 1;
 }

Definition at line 37 of file serversocket.h.


Constructor & Destructor Documentation

Dv::Net::ServerSocket::ServerSocket ( int  port = 0,
int  backlog = 10,
const std::string &  address = std::string(),
unsigned int  min_debug_level = 0,
Debugable debug_master = 0 
) throw (std::runtime_error) [explicit]

Start a server on port.

Parameters:
port on which server will listen for connections.
backlog no of connections allowed in backlog.
address local address to bind to, this is useful if e.g. you want the server to listen to a specific network interface, the default (empty string) will bind to the localhost (INADDR_ANY) interface.
min_debug_level if a debug_master is connected, logging info will only be written if the master's level is at least min_debug_level
debug_master from where debug info will be taken
See also:
Dv::DebugSlave
Exceptions:
std::runtime_error if anything goes wrong.
virtual Dv::Net::ServerSocket::~ServerSocket (  )  [virtual]

Destructor; also closes the socket.

Dv::Net::ServerSocket::ServerSocket ( const ServerSocket  )  [private]

Forbidden.


Member Function Documentation

int Dv::Net::ServerSocket::port (  ) 
Returns:
the port on which server listens.
unsigned long Dv::Net::ServerSocket::address (  ) 
Returns:
the numeric internet address of the server host
Dv::shared_ptr<InetAddress> Dv::Net::ServerSocket::inetaddress (  ) 
Returns:
the internet address of server.
See also:
Dv::Net::InetAddress
virtual Dv::shared_ptr<Socket> Dv::Net::ServerSocket::accept ( time_t  delay = 0,
size_t  bufsize = 1024,
bool  non_blocking = false 
) [virtual]

Accept a new connection from a client.

The client Dv::Net::Socket will be a Dv::DebugSlave of the same debug_master as the Dv::Net::ServerSocket.

Parameters:
delay in milliseconds: the time allowed for any subsequent I/O operation on the returned Socket. A delay of 0 implies that I/O operations will wait indefinitely.
bufsize the size of the buffer in the returned Socket.
non_blocking whether the underlying fdstreambuf for the new Socket should be non-blocking
Returns:
A new socket representing a connection with a client. The status of the returned Socket must be tested to verify whether the operation was succesful.
See also:
Dv::Util::fdstreambuf
virtual Dv::shared_ptr<Socket> Dv::Net::ServerSocket::accept_or_timeout ( time_t  timeout,
time_t  delay = 0,
size_t  bufsize = 1024,
bool  non_blocking = false 
) [virtual]

Accept a new connection from a client within a certain delay.

The client Dv::Net::Socket will be a Dv::DebugSlave of the same debug_master as the Dv::Net::ServerSocket.

Parameters:
timeout number of millisecs within which the accept operation must have succeeded. If it does not succeed the status of the return socket will be Socket::ACCEPT_ERROR.
delay in milliseconds: the time allowed for any subsequent I/O operation on the returned Socket. A delay of 0 implies that I/O operations will wait indefinitely.
bufsize the size of the buffer in the returned Socket.
non_blocking whether the underlying fdstreambuf for the new Socket should be non-blocking
Returns:
A new socket representing a connection with a client. The status of the returned Socket must be tested to verify whether the operation was succesful.
See also:
Dv::Util::fdstreambuf
bool Dv::Net::ServerSocket::connection ( time_t  timeout,
int *  syserr = 0 
)

Check whether connections are waiting to be accepted.

Parameters:
timeout in milliseconds:
syserr if not 0, it will contain a copy of errno upon failure
Returns:
true iff accept should succeed, no iff if no input activity on this serversocket was detected for timeout millisecs

Example:

 Dv::Net::ServerSocket ss(port);
 ..
 if ( ss.connection(2000) ) { // only if a connection is waiting within 2 sec
   Dv::Util::ref<Dv::Net::Socket> so(ss.accept());
   *so << "+OK" << std::endl;
 }
 else {
   // do something else
 }
See also:
Dv::Util::fdstreambuf::fdwait
int Dv::Net::ServerSocket::fd (  )  const [inline]
Returns:
underlying file descriptor (-1 if it was closed).

Definition at line 127 of file serversocket.h.

References socket_fd_.

void Dv::Net::ServerSocket::close (  ) 

Closes underlying socket, makes ServerSocket unusable by performing shut_down(2) en close(2) on it and setting the underlying file descriptor to -1.

int Dv::Net::ServerSocket::fd_accept (  )  [protected]

Auxiliary function for accept().

Returns:
new socket connected with client
ServerSocket& Dv::Net::ServerSocket::operator= ( const ServerSocket  )  [private]

Forbidden.


Member Data Documentation

Definition at line 139 of file serversocket.h.

Referenced by fd().

A pointer to a sockaddr_in; server_address_ is opaque so we don't need to include config.h.

Definition at line 142 of file serversocket.h.

Definition at line 143 of file serversocket.h.


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

dvnet-0.9.24 [ 5 December, 2009]