Main MRPT website > C++ reference
MRPT logo

CServerTCPSocket.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  CServerTCPSocket_H
00029 #define  CServerTCPSocket_H
00030 
00031 #include <mrpt/config.h>
00032 #include <mrpt/utils/utils_defs.h>
00033 #include <mrpt/utils/CStream.h>
00034 
00035 #include <mrpt/utils/CDebugOutputCapable.h>
00036 
00037 namespace mrpt
00038 {
00039 namespace utils
00040 {
00041         class CClientTCPSocket;
00042 
00043         /** A TCP socket that can be wait for client connections to enter.
00044           *  Unless otherwise noticed, operations are blocking.
00045           */
00046         class BASE_IMPEXP CServerTCPSocket : public utils::CDebugOutputCapable
00047         {
00048         private:
00049 
00050 #ifdef MRPT_OS_WINDOWS
00051                 /** The handle for the listening server TCP socket.
00052                   */
00053                 unsigned int    m_serverSock;
00054 #else
00055                 /** The handle for the listening server TCP socket.
00056                   */
00057                 int                             m_serverSock;
00058 #endif
00059 
00060                 /** Returns a description of the last error */
00061                 std::string  getLastErrorStr();
00062 
00063                 bool            m_verbose;
00064 
00065                 /** Common code called from the platform-dependant constructor */
00066                 void setupSocket(
00067                         unsigned short    listenPort,
00068                         const std::string &IPaddress,
00069                         int               maxConnectionsWaiting );
00070 
00071         public:
00072                 /** Constructor that creates the socket, performs binding, and start listening mode.
00073                   *  \param listenPort The port to bound to.
00074                   *  \param IPaddress The interface to bound the socket to. By default 127.0.0.1 implies listening at all network interfaces.
00075                   *  \param maxConnectionsWaiting Maximum number of incoming connections waiting for "accept" before new ones are rejected.
00076                   *  \param verbose Whether to dump state information to the output defined in the utils::CDebugOutputCapable interface.
00077                   *  You can check if the socket has been created OK with "isListening".
00078                   * \sa isListening
00079                   * \exception std::exception If there is any error creating the socket, with a textual description of the error.
00080                   */
00081                 CServerTCPSocket(
00082                         unsigned short    listenPort,
00083                         const std::string &IPaddress = std::string("127.0.0.1"),
00084                         int               maxConnectionsWaiting = 50,
00085                         bool                    verbose = false
00086                         );
00087 
00088                 /** Destructor
00089                  */
00090                 ~CServerTCPSocket( );
00091 
00092                 /** Returns true if the socket was successfully open and it's bound to the desired port.
00093                   */
00094                 bool isListening();
00095 
00096                 /** Waits for an incoming connection (indefinitely, or with a given timeout)
00097                   * The returned object represents the new connection, and MUST BE deleted by the user when no longer needed.
00098                   * \param timeout_ms The timeout for the waiting, in milliseconds. Set this to "-1" to disable timeout (i.e. timeout=infinite)
00099                   * \return The incoming connection, or NULL on timeout or error.
00100                   */
00101                 CClientTCPSocket *  accept( int timeout_ms = -1 );
00102 
00103 
00104 
00105         }; // End of class def.
00106 
00107 } // End of namespace
00108 } // End of namespace
00109 
00110 #endif  // file



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011