ViSP  2.6.2
vpServer.cpp
1 /****************************************************************************
2  *
3  * $Id: vpServer.cpp 3820 2012-06-27 13:13:29Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 
47 vpServer::vpServer( ) : vpNetwork(), started(false)
48 {
49  int protocol = 0;
50  emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
52  {
53  vpERROR_TRACE( "vpServer::vpServer(), cannot open socket." );
54  }
55  emitter.emitterAdress.sin_family = AF_INET;
56  emitter.emitterAdress.sin_addr.s_addr = INADDR_ANY;
57  emitter.emitterAdress.sin_port = 0;
58 
59  adress = inet_ntoa(emitter.emitterAdress.sin_addr);
60  port = emitter.emitterAdress.sin_port;
61 }
62 
68 vpServer::vpServer( const int &port_serv ) : vpNetwork(), started(false)
69 {
70  int protocol = 0;
71  emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
73  {
74  vpERROR_TRACE( "vpServer::vpServer(const int &port_serv), cannot open socket." );
75  }
76  emitter.emitterAdress.sin_family = AF_INET;
77  emitter.emitterAdress.sin_addr.s_addr = INADDR_ANY; //inet_addr("127.0.0.1");;
78  emitter.emitterAdress.sin_port = htons( port_serv );
79 
80  adress = inet_ntoa(emitter.emitterAdress.sin_addr);
81  port = port_serv;
82 }
83 
90 vpServer::vpServer( const std::string &adress_serv,const int &port_serv ) : vpNetwork(), started(false)
91 {
92  int protocol = 0;
93  emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
95  {
96  vpERROR_TRACE( "vpServer::vpServer(const std::string &adress_serv,const int &port_serv), cannot open socket." );
97  }
98  emitter.emitterAdress.sin_family = AF_INET;
99  emitter.emitterAdress.sin_addr.s_addr = inet_addr(adress_serv.c_str());
100  emitter.emitterAdress.sin_port = htons( port_serv );
101 
102  adress = adress_serv;
103  port = port_serv;
104 }
105 
110 {
111 #ifdef UNIX
113 #else //Win32
114  closesocket( emitter.socketFileDescriptorEmitter );
115 #endif
116 
117  for(unsigned int i = 0 ; i < receptor_list.size() ; i++)
118 #ifdef UNIX
119  close( receptor_list[i].socketFileDescriptorReceptor );
120 #else //Win32
121  closesocket( receptor_list[i].socketFileDescriptorReceptor );
122 #endif
123 
124 }
125 
132 {
133  int serverStructLength = sizeof(emitter.emitterAdress);
134  int bindResult = bind( emitter.socketFileDescriptorEmitter, (struct sockaddr *) &emitter.emitterAdress, serverStructLength );
135 
136  if( bindResult < 0 )
137  {
138  std::cout << "Error id : " << bindResult << std::endl;
139  std::string errorMessage( "vpServer::vpServer(), cannot bind to port" );
140  char posrtNumberString[32];
141  sprintf( posrtNumberString, "%d", port );
142  errorMessage += " ";
143  errorMessage += posrtNumberString;
144  errorMessage += " The port may be already used.";
145  vpERROR_TRACE( errorMessage.c_str() );
146  return false;
147  }
148 
149 #ifdef SO_NOSIGPIPE
150  // Mac OS X does not have the MSG_NOSIGNAL flag. It does have this
151  // connections based version, however.
153  int set_option = 1;
154  if (0 == setsockopt(emitter.socketFileDescriptorEmitter, SOL_SOCKET, SO_NOSIGPIPE, &set_option, sizeof(set_option))) {
155  } else {
156  std::cout << "Failed to set socket signal option" << std::endl;
157  }
158  }
159 #endif // SO_NOSIGPIPE
160 
161  listen( emitter.socketFileDescriptorEmitter, max_clients );
162  std::cout << "Server ready" << std::endl;
163 
164  started = true;
165 
166  return true;
167 }
168 
175 {
176  if(!started)
177  if(!start()){
178  return false;
179  }
180 
181  tv.tv_sec = tv_sec;
182  tv.tv_usec = tv_usec;
183 
184  FD_ZERO(&readFileDescriptor);
185 
188 
189  for(unsigned int i=0; i<receptor_list.size(); i++){
190  FD_SET(receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor);
191 
192  if(i == 0)
193  socketMax = receptor_list[i].socketFileDescriptorReceptor;
194 
195  if(socketMax < receptor_list[i].socketFileDescriptorReceptor) socketMax = receptor_list[i].socketFileDescriptorReceptor;
196  }
197 
198  int value = select(socketMax+1,&readFileDescriptor,NULL,NULL,&tv);
199  if(value == -1){
200  //vpERROR_TRACE( "vpServer::run(), select()" );
201  return false;
202  }
203  else if(value == 0){
204  return false;
205  }
206  else{
208  vpNetwork::vpReceptor client;
209  client.receptorAddressSize = sizeof(client.receptorAddress);
210  client.socketFileDescriptorReceptor = accept(emitter.socketFileDescriptorEmitter,(struct sockaddr*) &client.receptorAddress, &client.receptorAddressSize);
211  if((client.socketFileDescriptorReceptor) == -1)
212  vpERROR_TRACE( "vpServer::run(), accept()" );
213 
214  client.receptorIP = inet_ntoa(client.receptorAddress.sin_addr);
215  printf("New client connected : %s\n", inet_ntoa(client.receptorAddress.sin_addr));
216  receptor_list.push_back(client);
217 
218  return true;
219  }
220  else{
221  for(unsigned int i=0; i<receptor_list.size(); i++){
222  if(FD_ISSET(receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor)){
223  char deco;
224  int numbytes = recv(receptor_list[i].socketFileDescriptorReceptor, &deco, 1, MSG_PEEK);
225 
226  if(numbytes == 0)
227  {
228  std::cout << "Disconnected : " << inet_ntoa(receptor_list[i].receptorAddress.sin_addr) << std::endl;
229  receptor_list.erase(receptor_list.begin()+i);
230  return 0;
231  }
232  }
233  }
234  }
235  }
236 
237  return false;
238 }
239 
244 {
245  vpNetwork::print("Client");
246 }
socklen_t receptorAddressSize
Definition: vpNetwork.h:90
fd_set readFileDescriptor
Definition: vpNetwork.h:109
int socketFileDescriptorEmitter
Definition: vpNetwork.h:100
#define vpERROR_TRACE
Definition: vpDebug.h:379
virtual void print(const char *id="")
Definition: vpNetwork.cpp:124
bool checkForConnections()
Definition: vpServer.cpp:174
This class represents a Transmission Control Protocol (TCP) network.
Definition: vpNetwork.h:83
bool start()
Definition: vpServer.cpp:131
int socketFileDescriptorReceptor
Definition: vpNetwork.h:88
vpServer()
Definition: vpServer.cpp:47
int socketMax
Definition: vpNetwork.h:110
vpEmitter emitter
Definition: vpNetwork.h:107
long tv_sec
Definition: vpNetwork.h:124
virtual ~vpServer()
Definition: vpServer.cpp:109
struct timeval tv
Definition: vpNetwork.h:123
std::vector< vpReceptor > receptor_list
Definition: vpNetwork.h:108
long tv_usec
Definition: vpNetwork.h:125
virtual void print()
Definition: vpServer.cpp:243
struct sockaddr_in receptorAddress
Definition: vpNetwork.h:94
std::string receptorIP
Definition: vpNetwork.h:95
struct sockaddr_in emitterAdress
Definition: vpNetwork.h:99