Main MRPT website > C++ reference for MRPT 1.5.9
CServerTCPSocket_common.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 
15 #include <mrpt/system/os.h>
16 #include <cstdio> // stderr
17 using namespace mrpt::utils;
18 
19 #if defined(MRPT_OS_LINUX) || defined(MRPT_OS_APPLE)
20  #define INVALID_SOCKET (-1)
21 
22  #include <sys/socket.h>
23  #include <unistd.h>
24  #include <fcntl.h>
25  #include <errno.h>
26  #include <sys/types.h>
27  #include <sys/ioctl.h>
28  #include <netdb.h>
29  #include <arpa/inet.h>
30  #include <netinet/in.h>
31 #endif
32 
33 #ifdef MRPT_OS_WINDOWS
34  #include <winsock.h>
35  typedef int socklen_t;
36 #endif
37 
38 
39 /*---------------------------------------------------------------
40  setupSocket
41  ---------------------------------------------------------------*/
43  unsigned short listenPort,
44  const std::string &IPaddress,
45  int maxConnectionsWaiting )
46 {
48 
49  // Create the socket:
50  // ----------------------------
51  m_serverSock = socket(AF_INET, SOCK_STREAM, 0);
54 
55  // Bind it:
56  // ----------------------------
57  sockaddr_in desiredIP;
58 
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);
62 
63  if( INVALID_SOCKET == ::bind(m_serverSock,(struct sockaddr *)(&desiredIP),sizeof(desiredIP)) )
65 
66  // Put in listen mode:
67  // ----------------------------
68  if ( INVALID_SOCKET == listen(m_serverSock,maxConnectionsWaiting) )
70 
71  MRPT_LOG_DEBUG( format("[CServerTCPSocket] Listening at %s:%i\n",IPaddress.c_str(), listenPort ));
72 
73  MRPT_END
74 }
75 
76 
77 
78 /*---------------------------------------------------------------
79  isListening
80  ---------------------------------------------------------------*/
82 {
83  return INVALID_SOCKET != m_serverSock;
84 }
85 
86 /*---------------------------------------------------------------
87  accept
88  ---------------------------------------------------------------*/
90 {
92 
93  if( m_serverSock == INVALID_SOCKET) return NULL;
94 
95  struct timeval timeoutSelect;
96  struct timeval *ptrTimeout;
97  fd_set sockArr;
98 
99  // Init fd_set structure & add our socket to it:
100  FD_ZERO(&sockArr);
101  FD_SET(m_serverSock, &sockArr);
102 
103  // The timeout:
104  if (timeout_ms<0)
105  {
106  ptrTimeout = NULL;
107  }
108  else
109  {
110  timeoutSelect.tv_sec = timeout_ms / 1000;
111  timeoutSelect.tv_usec = 1000 * (timeout_ms % 1000);
112  ptrTimeout = &timeoutSelect;
113  }
114 
115  // Wait for READ flag (meaning incoming connections):
116  MRPT_LOG_DEBUG("[CServerTCPSocket::accept] Waiting incoming connections");
117 
118  int selRet = ::select(
119  m_serverSock+1,// __nfds
120  &sockArr, // Wait for read
121  NULL, // Wait for write
122  NULL, // Wait for except.
123  ptrTimeout); // Timeout
124 
125  if( selRet==INVALID_SOCKET)
126  {
127  fprintf(stderr,"%s\n", getLastErrorStr().c_str());
128  return NULL;
129  }
130 
131  if (selRet==0)
132  {
133  MRPT_LOG_WARN("[CServerTCPSocket::accept] Timeout waiting incoming connections\n" );
134 
135  // Timeout:
136  return NULL;
137  }
138  else
139  {
140  MRPT_LOG_DEBUG("[CServerTCPSocket::accept] Incoming connection accepted\n" );
141 
142  // We have a new connection:
143  CClientTCPSocket *ret = new CClientTCPSocket();
144 
145  sockaddr_in otherPart;
146  socklen_t otherPartSize = sizeof(otherPart);
147 
148  int aceptdSock = ::accept(
149  m_serverSock,
150  (struct sockaddr*)&otherPart,
151  &otherPartSize );
152 
153  if (aceptdSock==INVALID_SOCKET)
154  {
155  fprintf(stderr,"%s\n",getLastErrorStr().c_str());
156  delete ret;
157  return NULL;
158  }
159 
160  ret->m_hSock = aceptdSock;
161 
162  ret->m_remotePartIP = std::string( inet_ntoa( otherPart.sin_addr ) );
163  ret->m_remotePartPort = ntohs( otherPart.sin_port );
164 
165  MRPT_LOG_DEBUG(format("[CServerTCPSocket::accept] Connection accepted from %s:%u\n",
166  ret->m_remotePartIP.c_str(),
167  ret->m_remotePartPort ) );
168 
169  return ret;
170  }
171 
172  MRPT_END
173 }
174 
175 
#define INVALID_SOCKET
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
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&#39;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.
Definition: os.cpp:412
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_END
#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.
Definition: format.cpp:21
#define MRPT_LOG_DEBUG(_STRING)
std::string getLastErrorStr()
Returns a description of the last Sockets error.
GLsizei const GLchar ** string
Definition: glext.h:3919
#define MRPT_START
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&#39;s CStream interface for passing...



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020