Main MRPT website > C++ reference for MRPT 1.9.9
List of all members | Public Member Functions | Private Member Functions | Private Attributes
mrpt::comms::CServerTCPSocket Class Reference

Detailed Description

A TCP socket that can be wait for client connections to enter.

Unless otherwise noticed, operations are blocking.

Definition at line 26 of file CServerTCPSocket.h.

#include <mrpt/comms/CServerTCPSocket.h>

Inheritance diagram for mrpt::comms::CServerTCPSocket:
Inheritance graph

Public Member Functions

 CServerTCPSocket (unsigned short listenPort, const std::string &IPaddress=std::string("127.0.0.1"), int maxConnectionsWaiting=50, mrpt::utils::VerbosityLevel verbosityLevel=mrpt::utils::LVL_INFO)
 Constructor that creates the socket, performs binding, and start listening mode. More...
 
virtual ~CServerTCPSocket ()
 Dtor. More...
 
bool isListening ()
 Returns true if the socket was successfully open and it's bound to the desired port. More...
 
std::unique_ptr< CClientTCPSocketaccept (int timeout_ms=-1)
 Waits for an incoming connection (indefinitely, or with a given timeout) The returned object represents the new connection, and MUST BE deleted by the user when no longer needed. More...
 

Private Member Functions

std::string getLastErrorStr ()
 Returns a description of the last Sockets error. More...
 
void setupSocket (unsigned short listenPort, const std::string &IPaddress, int maxConnectionsWaiting)
 Common code called from the platform-dependant constructor. More...
 

Private Attributes

int m_serverSock
 The handle for the listening server TCP socket. More...
 

Constructor & Destructor Documentation

◆ CServerTCPSocket()

mrpt::comms::CServerTCPSocket::CServerTCPSocket ( unsigned short  listenPort,
const std::string IPaddress = std::string("127.0.0.1"),
int  maxConnectionsWaiting = 50,
mrpt::utils::VerbosityLevel  verbosityLevel = mrpt::utils::LVL_INFO 
)

Constructor that creates the socket, performs binding, and start listening mode.

Parameters
listenPortThe port to bound to.
IPaddressThe interface to bound the socket to. By default is 127.0.0.1 for localhost, for all network interfaces use 0.0.0.0.
maxConnectionsWaitingMaximum number of incoming connections waiting for "accept" before new ones are rejected. You can check if the socket has been created OK with "isListening".
See also
isListening
Exceptions
std::exceptionIf there is any error creating the socket, with a textual description of the error.

◆ ~CServerTCPSocket()

virtual mrpt::comms::CServerTCPSocket::~CServerTCPSocket ( )
virtual

Dtor.

Member Function Documentation

◆ accept()

std::unique_ptr< CClientTCPSocket > CServerTCPSocket::accept ( int  timeout_ms = -1)

Waits for an incoming connection (indefinitely, or with a given timeout) The returned object represents the new connection, and MUST BE deleted by the user when no longer needed.

Parameters
timeout_msThe timeout for the waiting, in milliseconds. Set this to "-1" to disable timeout (i.e. timeout=infinite)
Returns
The incoming connection, or nullptr on timeout or error.

Definition at line 86 of file CServerTCPSocket_common.cpp.

References mrpt::system::os::fprintf(), getLastErrorStr(), INVALID_SOCKET, m_serverSock, MRPT_END, MRPT_LOG_DEBUG, MRPT_LOG_DEBUG_FMT, MRPT_LOG_ERROR_FMT, MRPT_LOG_WARN, and MRPT_START.

Here is the call graph for this function:

◆ getLastErrorStr()

std::string CServerTCPSocket::getLastErrorStr ( )
private

Returns a description of the last Sockets error.

Definition at line 38 of file CServerTCPSocket_common.cpp.

References mrpt::comms::net::getLastSocketErrorStr().

Referenced by accept(), and setupSocket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isListening()

bool CServerTCPSocket::isListening ( )

Returns true if the socket was successfully open and it's bound to the desired port.

Definition at line 82 of file CServerTCPSocket_common.cpp.

References INVALID_SOCKET, and m_serverSock.

◆ setupSocket()

void CServerTCPSocket::setupSocket ( unsigned short  listenPort,
const std::string IPaddress,
int  maxConnectionsWaiting 
)
private

Common code called from the platform-dependant constructor.

Definition at line 43 of file CServerTCPSocket_common.cpp.

References mrpt::format(), getLastErrorStr(), INVALID_SOCKET, m_serverSock, MRPT_END, MRPT_LOG_DEBUG, MRPT_START, and THROW_EXCEPTION.

Here is the call graph for this function:

Member Data Documentation

◆ m_serverSock

int mrpt::comms::CServerTCPSocket::m_serverSock
private

The handle for the listening server TCP socket.

Definition at line 69 of file CServerTCPSocket.h.

Referenced by accept(), isListening(), and setupSocket().




Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019