class mrpt::comms::CServerTCPSocket
Overview
A TCP socket that can be wait for client connections to enter.
Unless otherwise noticed, operations are blocking.
#include <mrpt/comms/CServerTCPSocket.h> class CServerTCPSocket: public mrpt::system::COutputLogger { public: // construction CServerTCPSocket( unsigned short listenPort, const std::string& IPaddress = std::string("127.0.0.1"), int maxConnectionsWaiting = 50, mrpt::system::VerbosityLevel verbosityLevel = mrpt::system::LVL_INFO ); // methods bool isListening(); std::unique_ptr<CClientTCPSocket> accept(int timeout_ms = -1); };
Inherited Members
public: // structs struct TMsg; // fields bool logging_enable_console_output {true}; bool logging_enable_keep_record {false}; // methods void logStr(const VerbosityLevel level, std::string_view msg_str) const; void logFmt(const VerbosityLevel level, const char* fmt, ...) const; void void logCond(const VerbosityLevel level, bool cond, const std::string& msg_str) const; void setLoggerName(const std::string& name); std::string getLoggerName() const; void setVerbosityLevel(const VerbosityLevel level); void setVerbosityLevelForCallbacks(const VerbosityLevel level); void setMinLoggingLevel(const VerbosityLevel level); VerbosityLevel getMinLoggingLevel() const; VerbosityLevel getMinLoggingLevelForCallbacks() const; bool isLoggingLevelVisible(VerbosityLevel level) const; void getLogAsString(std::string& log_contents) const; std::string getLogAsString() const; void writeLogToFile(const std::optional<std::string>& fname_in = std::nullopt) const; void dumpLogToConsole() const; std::string getLoggerLastMsg() const; void getLoggerLastMsg(std::string& msg_str) const; void loggerReset(); void logRegisterCallback(output_logger_callback_t userFunc); bool logDeregisterCallback(output_logger_callback_t userFunc); COutputLogger& operator = (const COutputLogger&); COutputLogger& operator = (COutputLogger&&); static std::array<mrpt::system::ConsoleForegroundColor, NUMBER_OF_VERBOSITY_LEVELS>& logging_levels_to_colors(); static const std::array<const char*, NUMBER_OF_VERBOSITY_LEVELS>& logging_levels_to_names();
Construction
CServerTCPSocket( unsigned short listenPort, const std::string& IPaddress = std::string("127.0.0.1"), int maxConnectionsWaiting = 50, mrpt::system::VerbosityLevel verbosityLevel = mrpt::system::LVL_INFO )
Constructor that creates the socket, performs binding, and start listening mode.
Parameters:
listenPort |
The port to bound to. |
IPaddress |
The interface to bound the socket to. By default is 127.0.0.1 for localhost, for all network interfaces use 0.0.0.0. |
maxConnectionsWaiting |
Maximum number of incoming connections waiting for “accept” before new ones are rejected. You can check if the socket has been created OK with “isListening”. |
std::exception |
If there is any error creating the socket, with a textual description of the error. |
See also:
Methods
bool isListening()
Returns true if the socket was successfully open and it’s bound to the desired port.
std::unique_ptr<CClientTCPSocket> 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_ms |
The 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.