ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
StreamSocketServer Class Reference

Socket server for connection-oriented communications. More...

#include <StreamSocketServer.hpp>

Public Member Functions

 StreamSocketServer (const uint16_t port, int maxPendingConnections=STREAM_MAX_PENDING_CONNECTIONS)
 Constructor for TCP connection-oriented sockets.
 StreamSocketServer (const std::string &name, int maxPendingConnections=STREAM_MAX_PENDING_CONNECTIONS)
 Constructor for local connection-oriented sockets.
virtual ~StreamSocketServer ()
 Destructor.
void close ()
 Close the descriptor.
int getDescriptorNumber () const
 Method to get descriptor number.

Private Member Functions

 StreamSocketServer (const StreamSocketServer &)
StreamSocketServeroperator= (const StreamSocketServer &)

Private Attributes

int fd_
 Number of the file descriptor.

Detailed Description

Socket server for connection-oriented communications.

This class corresponds to a socket created with socket(), that must be given to the constructor of StreamSocketServerDescriptor to accept incoming connections.

Definition at line 47 of file StreamSocketServer.hpp.

Constructor & Destructor Documentation

StreamSocketServer ( const StreamSocketServer )
private
StreamSocketServer ( const uint16_t  port,
int  maxPendingConnections = STREAM_MAX_PENDING_CONNECTIONS 
)

Constructor for TCP connection-oriented sockets.

This constructor creates a connection-oriented AF_INET socket. It calls socket()+bind()+listen(). If the protocol is a stream, it also calls listen().

Parameters
portPort of the socket
Exceptions
runtime_errorin case of error in socket(), bind() or listen()

Definition at line 77 of file StreamSocketServer.cpp.

{
// socket()
fd_ = socket(AF_INET, SOCK_STREAM, 0);
if (fd_ < 0) {
ERROR("Creating client socket");
throw std::runtime_error ("Socket error");
}
// bind()
struct sockaddr_in serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(fd_, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
ERROR("bind()");
throw std::runtime_error ("Bind error");
}
// listen()
if (listen(fd_, maxPendingConnections) < 0) {
ERROR("listen()");
throw std::runtime_error ("Listen error");
}
}

Here is the call graph for this function:

StreamSocketServer ( const std::string &  name,
int  maxPendingConnections = STREAM_MAX_PENDING_CONNECTIONS 
)

Constructor for local connection-oriented sockets.

This constructor creates a connection-oriented AF_UNIX socket. It calls socket()+bind()+listen().

Parameters
nameName of the local socket on the filesystem
Exceptions
runtime_errorin case of error in socket(), bind() or listen()

Definition at line 37 of file StreamSocketServer.cpp.

{
// socket()
fd_ = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd_ < 0) {
ERROR("Creating client socket");
throw std::runtime_error ("Socket error");
}
// bind()
struct sockaddr_un serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strncpy(serv_addr.sun_path, name.c_str(),
sizeof(serv_addr.sun_path) - 1);
if (bind(fd_, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
ERROR("bind()");
throw std::runtime_error ("Bind error");
}
// listen()
if (listen(fd_, maxPendingConnections) < 0) {
ERROR("listen()");
throw std::runtime_error ("Listen error");
}
}

Here is the call graph for this function:

virtual ~StreamSocketServer ( )
inlinevirtual

Destructor.

It just calls close() to close the descriptor.

Definition at line 72 of file StreamSocketServer.hpp.

{
close();
}

Here is the call graph for this function:

Member Function Documentation

void close ( )
inline

Close the descriptor.

Definition at line 79 of file StreamSocketServer.hpp.

{
}

Here is the caller graph for this function:

int getDescriptorNumber ( ) const
inline

Method to get descriptor number.

Returns
Descriptor number.

Definition at line 88 of file StreamSocketServer.hpp.

{
return fd_;
}

Here is the caller graph for this function:

StreamSocketServer& operator= ( const StreamSocketServer )
private

Member Data Documentation

int fd_
private

Number of the file descriptor.

This is the return value of socket().

Definition at line 57 of file StreamSocketServer.hpp.


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