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