00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://www.mrpt.org/ | 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 * \ingroup network_grp 00046 */ 00047 class BASE_IMPEXP CServerTCPSocket : public utils::CDebugOutputCapable 00048 { 00049 private: 00050 00051 #ifdef MRPT_OS_WINDOWS 00052 /** The handle for the listening server TCP socket. 00053 */ 00054 unsigned int m_serverSock; 00055 #else 00056 /** The handle for the listening server TCP socket. 00057 */ 00058 int m_serverSock; 00059 #endif 00060 00061 /** Returns a description of the last error */ 00062 std::string getLastErrorStr(); 00063 00064 bool m_verbose; 00065 00066 /** Common code called from the platform-dependant constructor */ 00067 void setupSocket( 00068 unsigned short listenPort, 00069 const std::string &IPaddress, 00070 int maxConnectionsWaiting ); 00071 00072 public: 00073 /** Constructor that creates the socket, performs binding, and start listening mode. 00074 * \param listenPort The port to bound to. 00075 * \param IPaddress The interface to bound the socket to. By default 127.0.0.1 implies listening at all network interfaces. 00076 * \param maxConnectionsWaiting Maximum number of incoming connections waiting for "accept" before new ones are rejected. 00077 * \param verbose Whether to dump state information to the output defined in the utils::CDebugOutputCapable interface. 00078 * You can check if the socket has been created OK with "isListening". 00079 * \sa isListening 00080 * \exception std::exception If there is any error creating the socket, with a textual description of the error. 00081 */ 00082 CServerTCPSocket( 00083 unsigned short listenPort, 00084 const std::string &IPaddress = std::string("127.0.0.1"), 00085 int maxConnectionsWaiting = 50, 00086 bool verbose = false 00087 ); 00088 00089 /** Destructor 00090 */ 00091 ~CServerTCPSocket( ); 00092 00093 /** Returns true if the socket was successfully open and it's bound to the desired port. 00094 */ 00095 bool isListening(); 00096 00097 /** Waits for an incoming connection (indefinitely, or with a given timeout) 00098 * The returned object represents the new connection, and MUST BE deleted by the user when no longer needed. 00099 * \param timeout_ms The timeout for the waiting, in milliseconds. Set this to "-1" to disable timeout (i.e. timeout=infinite) 00100 * \return The incoming connection, or NULL on timeout or error. 00101 */ 00102 CClientTCPSocket * accept( int timeout_ms = -1 ); 00103 00104 00105 00106 }; // End of class def. 00107 00108 } // End of namespace 00109 } // End of namespace 00110 00111 #endif // file
| Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011 |