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