Visual Servoing Platform  version 3.0.0
vpServer.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * TCP Server
32  *
33  * Authors:
34  * Aurelien Yol
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpServer.h>
39 
40 
44 vpServer::vpServer( ) : adress(), port(0), started(false), max_clients(10)
45 {
46  int protocol = 0;
47  emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
48 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
50 #else
51  if (emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
52 #endif
53  {
54  vpERROR_TRACE( "vpServer::vpServer(), cannot open socket." );
55  }
56  emitter.emitterAddress.sin_family = AF_INET;
57  emitter.emitterAddress.sin_addr.s_addr = INADDR_ANY;
58  emitter.emitterAddress.sin_port = 0;
59 
60  adress = inet_ntoa(emitter.emitterAddress.sin_addr);
61  port = emitter.emitterAddress.sin_port;
62 }
63 
69 vpServer::vpServer( const int &port_serv ) : adress(), port(0), started(false), max_clients(10)
70 {
71  int protocol = 0;
72  emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
73 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
75 #else
76  if (emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
77 #endif
78  {
79  vpERROR_TRACE( "vpServer::vpServer(const int &port_serv), cannot open socket." );
80  }
81  emitter.emitterAddress.sin_family = AF_INET;
82  emitter.emitterAddress.sin_addr.s_addr = INADDR_ANY; //inet_addr("127.0.0.1");;
83  emitter.emitterAddress.sin_port = htons( (unsigned short)port_serv );
84 
85  adress = inet_ntoa(emitter.emitterAddress.sin_addr);
86  port = port_serv;
87 }
88 
95 vpServer::vpServer( const std::string &adress_serv,const int &port_serv )
96  : adress(), port(0), started(false), max_clients(10)
97 {
98  int protocol = 0;
99  emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
100 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
102 #else
103  if (emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
104 #endif
105  {
106  vpERROR_TRACE( "vpServer::vpServer(const std::string &adress_serv,const int &port_serv), cannot open socket." );
107  }
108  emitter.emitterAddress.sin_family = AF_INET;
109  emitter.emitterAddress.sin_addr.s_addr = inet_addr(adress_serv.c_str());
110  emitter.emitterAddress.sin_port = htons( (unsigned short)port_serv );
111 
112  adress = adress_serv;
113  port = port_serv;
114 }
115 
120 {
121 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
123 #else //Win32
124  closesocket( (unsigned)emitter.socketFileDescriptorEmitter );
125 #endif
126 
127  for(unsigned int i = 0 ; i < receptor_list.size() ; i++)
128 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
129  close( receptor_list[i].socketFileDescriptorReceptor );
130 #else //Win32
131  closesocket( (unsigned)receptor_list[i].socketFileDescriptorReceptor );
132 #endif
133 
134 }
135 
142 {
143  int serverStructLength = sizeof(emitter.emitterAddress);
144 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
145  int bindResult = bind( emitter.socketFileDescriptorEmitter, (struct sockaddr *) &emitter.emitterAddress, (unsigned)serverStructLength );
146 #else //Win32
147  int bindResult = bind( (unsigned)emitter.socketFileDescriptorEmitter, (struct sockaddr *) &emitter.emitterAddress, serverStructLength );
148 #endif
149 
150 
151  if( bindResult < 0 )
152  {
153  std::cout << "Error id : " << bindResult << std::endl;
154  std::string errorMessage( "vpServer::vpServer(), cannot bind to port" );
155  char posrtNumberString[32];
156  sprintf( posrtNumberString, "%d", port );
157  errorMessage += " ";
158  errorMessage += posrtNumberString;
159  errorMessage += " The port may be already used.";
160  vpERROR_TRACE( errorMessage.c_str() );
161  return false;
162  }
163 
164 #ifdef SO_NOSIGPIPE
165  // Mac OS X does not have the MSG_NOSIGNAL flag. It does have this
166  // connections based version, however.
168  int set_option = 1;
169  if (0 == setsockopt(emitter.socketFileDescriptorEmitter, SOL_SOCKET, SO_NOSIGPIPE, &set_option, sizeof(set_option))) {
170  } else {
171  std::cout << "Failed to set socket signal option" << std::endl;
172  }
173  }
174 #endif // SO_NOSIGPIPE
175 
176 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
177  listen( emitter.socketFileDescriptorEmitter, (int)max_clients );
178 #else //Win32
179  listen( (unsigned)emitter.socketFileDescriptorEmitter, (int)max_clients );
180 #endif
181 
182  std::cout << "Server ready" << std::endl;
183 
184  started = true;
185 
186  return true;
187 }
188 
195 {
196  if(!started)
197  if(!start()){
198  return false;
199  }
200 
201  tv.tv_sec = tv_sec;
202  tv.tv_usec = tv_usec;
203 
204  FD_ZERO(&readFileDescriptor);
205 
208 
209  for(unsigned int i=0; i<receptor_list.size(); i++){
210  FD_SET((unsigned)receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor);
211 
212  if(i == 0)
213  socketMax = receptor_list[i].socketFileDescriptorReceptor;
214 
215  if(socketMax < receptor_list[i].socketFileDescriptorReceptor) socketMax = receptor_list[i].socketFileDescriptorReceptor;
216  }
217 
218  int value = select((int)socketMax+1,&readFileDescriptor,NULL,NULL,&tv);
219  if(value == -1){
220  //vpERROR_TRACE( "vpServer::run(), select()" );
221  return false;
222  }
223  else if(value == 0){
224  return false;
225  }
226  else{
227  if(FD_ISSET((unsigned int)emitter.socketFileDescriptorEmitter,&readFileDescriptor)){
228  vpNetwork::vpReceptor client;
229  client.receptorAddressSize = sizeof(client.receptorAddress);
230 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
231  client.socketFileDescriptorReceptor = accept(emitter.socketFileDescriptorEmitter,(struct sockaddr*) &client.receptorAddress, &client.receptorAddressSize);
232 #else //Win32
233  client.socketFileDescriptorReceptor = accept((unsigned int)emitter.socketFileDescriptorEmitter,(struct sockaddr*) &client.receptorAddress, &client.receptorAddressSize);
234 #endif
235 
236 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
237  if((client.socketFileDescriptorReceptor) == -1)
238 #else
239  if((client.socketFileDescriptorReceptor) == INVALID_SOCKET)
240 #endif
241  vpERROR_TRACE( "vpServer::run(), accept()" );
242 
243  client.receptorIP = inet_ntoa(client.receptorAddress.sin_addr);
244  printf("New client connected : %s\n", inet_ntoa(client.receptorAddress.sin_addr));
245  receptor_list.push_back(client);
246 
247  return true;
248  }
249  else{
250  for(unsigned int i=0; i<receptor_list.size(); i++){
251  if(FD_ISSET((unsigned int)receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor)){
252  char deco;
253 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
254  int numbytes = recv(receptor_list[i].socketFileDescriptorReceptor, &deco, 1, MSG_PEEK);
255 #else //Win32
256  int numbytes = recv((unsigned int)receptor_list[i].socketFileDescriptorReceptor, &deco, 1, MSG_PEEK);
257 #endif
258 
259 
260  if(numbytes == 0)
261  {
262  std::cout << "Disconnected : " << inet_ntoa(receptor_list[i].receptorAddress.sin_addr) << std::endl;
263  receptor_list.erase(receptor_list.begin()+(int)i);
264  return 0;
265  }
266  }
267  }
268  }
269  }
270 
271  return false;
272 }
273 
278 {
279  vpNetwork::print("Client");
280 }
281 
socklen_t receptorAddressSize
Definition: vpNetwork.h:86
fd_set readFileDescriptor
Definition: vpNetwork.h:119
int socketFileDescriptorEmitter
Definition: vpNetwork.h:100
void print(const char *id="")
Definition: vpNetwork.cpp:117
#define vpERROR_TRACE
Definition: vpDebug.h:391
bool checkForConnections()
Definition: vpServer.cpp:194
bool start()
Definition: vpServer.cpp:141
int socketFileDescriptorReceptor
Definition: vpNetwork.h:85
vpServer()
Definition: vpServer.cpp:44
int socketMax
Definition: vpNetwork.h:121
vpEmitter emitter
Definition: vpNetwork.h:117
long tv_sec
Definition: vpNetwork.h:138
virtual ~vpServer()
Definition: vpServer.cpp:119
struct sockaddr_in emitterAddress
Definition: vpNetwork.h:98
struct timeval tv
Definition: vpNetwork.h:137
std::vector< vpReceptor > receptor_list
Definition: vpNetwork.h:118
long tv_usec
Definition: vpNetwork.h:139
void print()
Definition: vpServer.cpp:277
struct sockaddr_in receptorAddress
Definition: vpNetwork.h:91
std::string receptorIP
Definition: vpNetwork.h:92