MRPT  2.0.2
CServerTCPSocket.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "comms-precomp.h" // Precompiled headers
11 
14 #include <mrpt/core/exceptions.h>
15 
16 #ifdef _WIN32
17 // Windows
18 #include <winsock.h>
19 #if defined(_MSC_VER)
20 #pragma comment(lib, "WS2_32.LIB")
21 #endif
22 #else
23 // Linux & Apple
24 // Platform specific headers:
25 #include <fcntl.h>
26 #include <sys/socket.h>
27 #include <unistd.h>
28 #include <cerrno>
29 //#include <iostream>
30 #include <arpa/inet.h>
31 #include <netdb.h>
32 #include <netinet/in.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <sys/types.h>
36 #endif
37 
38 using namespace mrpt;
39 using namespace mrpt::comms;
40 
42  unsigned short listenPort, const std::string& IPaddress,
43  int maxConnectionsWaiting, mrpt::system::VerbosityLevel verbosityLevel)
44  : COutputLogger("CServerTCPSocket")
45 {
47  setVerbosityLevel(verbosityLevel);
48 
49 #if defined(_WIN32)
50  // Init the WinSock Library:
51  // ----------------------------
52  WORD wVersionRequested;
53  WSADATA wsaData;
54 
55  wVersionRequested = MAKEWORD(2, 0);
56 
57  if (0 != WSAStartup(wVersionRequested, &wsaData))
59 #endif
60 
61  setupSocket(listenPort, IPaddress, maxConnectionsWaiting);
63 }
64 
66 {
67 // Delete socket:
68 #if defined(_WIN32)
69  if (m_serverSock != INVALID_SOCKET) closesocket(m_serverSock);
70  WSACleanup();
71 #else
72  if (m_serverSock != -1) close(m_serverSock);
73 #endif
74 }
#define MRPT_TRY_END
The end of a standard MRPT "try...catch()" block that allows tracing throw the call stack after an ex...
Definition: exceptions.h:213
VerbosityLevel
Enumeration of available verbosity levels.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
std::string getLastErrorStr()
Returns a description of the last Sockets error.
mrpt::system::COutputLogger COutputLogger
unsigned int m_serverSock
The handle for the listening server TCP socket.
#define MRPT_TRY_START
The start of a standard MRPT "try...catch()" block that allows tracing throw the call stack after an ...
Definition: exceptions.h:206
void setupSocket(unsigned short listenPort, const std::string &IPaddress, int maxConnectionsWaiting)
Common code called from the platform-dependant constructor.
void setVerbosityLevel(const VerbosityLevel level)
alias of setMinLoggingLevel()
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Serial and networking devices and utilities.
CServerTCPSocket(unsigned short listenPort, const std::string &IPaddress=std::string("127.0.0.1"), int maxConnectionsWaiting=50, mrpt::system::VerbosityLevel verbosityLevel=mrpt::system::LVL_INFO)
Constructor that creates the socket, performs binding, and start listening mode.



Page generated by Doxygen 1.8.14 for MRPT 2.0.2 Git: 9efc2a654 Mon Apr 6 11:24:47 2020 +0200 at lun abr 6 11:30:12 CEST 2020