Main MRPT website > C++ reference for MRPT 1.9.9
CServerTCPSocket.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/utils/core_defs.h>
13 #include <string>
14 #include <memory> // unique_ptr
15 
16 namespace mrpt
17 {
18 namespace comms
19 {
20 class CClientTCPSocket;
21 
22 /** A TCP socket that can be wait for client connections to enter.
23  * Unless otherwise noticed, operations are blocking.
24  * \ingroup mrpt_comms_grp
25  */
26 class CServerTCPSocket : public utils::COutputLogger
27 {
28  public:
29  /** Constructor that creates the socket, performs binding, and start
30  * listening mode.
31  * \param listenPort The port to bound to.
32  * \param IPaddress The interface to bound the socket to. By default is
33  * 127.0.0.1 for localhost, for all network interfaces use 0.0.0.0.
34  * \param maxConnectionsWaiting Maximum number of incoming connections
35  * waiting for "accept" before new ones are rejected.
36  * You can check if the socket has been created OK with "isListening".
37  * \sa isListening
38  * \exception std::exception If there is any error creating the socket,
39  * with a textual description of the error.
40  */
42  unsigned short listenPort,
43  const std::string& IPaddress = std::string("127.0.0.1"),
44  int maxConnectionsWaiting = 50,
45  mrpt::utils::VerbosityLevel verbosityLevel = mrpt::utils::LVL_INFO);
46  /** Dtor */
47  virtual ~CServerTCPSocket();
48 
49  /** Returns true if the socket was successfully open and it's bound to the
50  * desired port. */
51  bool isListening();
52 
53  /** Waits for an incoming connection (indefinitely, or with a given timeout)
54  * The returned object represents the new connection, and MUST BE deleted
55  * by the user when no longer needed.
56  * \param timeout_ms The timeout for the waiting, in milliseconds. Set this
57  * to "-1" to disable timeout (i.e. timeout=infinite)
58  * \return The incoming connection, or nullptr on timeout or error.
59  */
60  std::unique_ptr<CClientTCPSocket> accept(int timeout_ms = -1);
61 
62  private:
63 /** The handle for the listening server TCP socket. */
64 #ifdef MRPT_OS_WINDOWS
65  unsigned int
66 #else
67  int
68 #endif
70 
71  /** Returns a description of the last Sockets error */
73 
74  /** Common code called from the platform-dependant constructor */
75  void setupSocket(
76  unsigned short listenPort, const std::string& IPaddress,
77  int maxConnectionsWaiting);
78 
79 }; // End of class def.
80 
81 } // End of namespace
82 } // End of namespace
A TCP socket that can be wait for client connections to enter.
bool isListening()
Returns true if the socket was successfully open and it's bound to the desired port.
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.
std::unique_ptr< CClientTCPSocket > accept(int timeout_ms=-1)
Waits for an incoming connection (indefinitely, or with a given timeout) The returned object represen...
void setupSocket(unsigned short listenPort, const std::string &IPaddress, int maxConnectionsWaiting)
Common code called from the platform-dependant constructor.
std::string getLastErrorStr()
Returns a description of the last Sockets error.
int m_serverSock
The handle for the listening server TCP socket.
virtual ~CServerTCPSocket()
Dtor.
GLsizei const GLchar ** string
Definition: glext.h:4101
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 63ea9d1f1 Thu Nov 23 00:06:53 2017 +0100 at mar 26 may 2026 12:19:29 CEST