19 #if defined(MRPT_OS_LINUX) || defined(MRPT_OS_APPLE) 20 #define INVALID_SOCKET (-1) 22 #include <sys/socket.h> 26 #include <sys/types.h> 27 #include <sys/ioctl.h> 29 #include <arpa/inet.h> 30 #include <netinet/in.h> 33 #ifdef MRPT_OS_WINDOWS 35 typedef int socklen_t;
43 unsigned short listenPort,
45 int maxConnectionsWaiting )
57 sockaddr_in desiredIP;
59 desiredIP.sin_family = AF_INET;
60 desiredIP.sin_addr.s_addr = inet_addr( IPaddress.c_str() );
61 desiredIP.sin_port = htons((
unsigned short)listenPort);
71 MRPT_LOG_DEBUG(
format(
"[CServerTCPSocket] Listening at %s:%i\n",IPaddress.c_str(), listenPort ));
95 struct timeval timeoutSelect;
96 struct timeval *ptrTimeout;
110 timeoutSelect.tv_sec = timeout_ms / 1000;
111 timeoutSelect.tv_usec = 1000 * (timeout_ms % 1000);
112 ptrTimeout = &timeoutSelect;
116 MRPT_LOG_DEBUG(
"[CServerTCPSocket::accept] Waiting incoming connections");
118 int selRet = ::select(
133 MRPT_LOG_WARN(
"[CServerTCPSocket::accept] Timeout waiting incoming connections\n" );
140 MRPT_LOG_DEBUG(
"[CServerTCPSocket::accept] Incoming connection accepted\n" );
145 sockaddr_in otherPart;
146 socklen_t otherPartSize =
sizeof(otherPart);
150 (
struct sockaddr*)&otherPart,
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
CClientTCPSocket * accept(int timeout_ms=-1)
Waits for an incoming connection (indefinitely, or with a given timeout) The returned object represen...
#define THROW_EXCEPTION(msg)
void setupSocket(unsigned short listenPort, const std::string &IPaddress, int maxConnectionsWaiting)
Common code called from the platform-dependant constructor.
bool isListening()
Returns true if the socket was successfully open and it's bound to the desired port.
int BASE_IMPEXP fprintf(FILE *fil, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(2
An OS-independent version of fprintf.
unsigned short m_remotePartPort
The TCP port of the remote part of the connection.
int m_serverSock
The handle for the listening server TCP socket.
#define MRPT_LOG_WARN(_STRING)
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
#define MRPT_LOG_DEBUG(_STRING)
std::string getLastErrorStr()
Returns a description of the last Sockets error.
GLsizei const GLchar ** string
int m_hSock
The handle for the connected TCP socket, or -1.
std::string m_remotePartIP
The IP address of the remote part of the connection.
A TCP socket that can be connected to a TCP server, implementing MRPT's CStream interface for passing...