ViSP  2.6.2
vpClient.cpp
1 /****************************************************************************
2  *
3  * $Id: vpClient.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 Client
36  *
37  * Authors:
38  * Aurelien Yol
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpClient.h>
43 
44 
46 {}
47 
52 {
53  stop();
54 }
55 
66 bool vpClient::connectToHostname(const std::string &hostname, const int &port_serv)
67 {
68  // get server host information from hostname
69  struct hostent *server = gethostbyname( hostname.c_str() );
70 
71  if ( server == NULL )
72  {
73  std::string noSuchHostMessage( "ERROR, " );
74  noSuchHostMessage.append( hostname );
75  noSuchHostMessage.append( ": no such host\n" );
76  vpERROR_TRACE( noSuchHostMessage.c_str(),
77  "vpClient::connectToHostname(const std::string &hostname, const int &port_serv)" );
78  return false;
79  }
80 
82 
83  serv.socketFileDescriptorReceptor = socket( AF_INET, SOCK_STREAM, 0 );
84 
85  if ( serv.socketFileDescriptorReceptor < 0){
86  vpERROR_TRACE( "ERROR opening socket",
87  "vpClient::connectToHostname()" );
88  return false;
89  }
90 
91  memset((char *) &serv.receptorAddress, '\0', sizeof(serv.receptorAddress));
92  serv.receptorAddress.sin_family = AF_INET;
93  memmove( (char *) &serv.receptorAddress.sin_addr.s_addr, (char *) server->h_addr,
94  server->h_length );
95  serv.receptorAddress.sin_port = htons( port_serv );
96  serv.receptorIP = inet_ntoa(*(in_addr *)server->h_addr);
97 
98  return connectServer(serv);
99 }
100 
111 bool vpClient::connectToIP(const std::string &ip, const int &port_serv)
112 {
114 
115  serv.socketFileDescriptorReceptor = socket( AF_INET, SOCK_STREAM, 0 );
116 
117  if ( serv.socketFileDescriptorReceptor < 0){
118  vpERROR_TRACE( "ERROR opening socket",
119  "vpClient::connectToIP()" );
120  return false;
121  }
122 
123  memset((char *) &serv.receptorAddress, '\0', sizeof(serv.receptorAddress));
124  serv.receptorAddress.sin_family = AF_INET;
125  serv.receptorAddress.sin_addr.s_addr = inet_addr(ip.c_str());
126  serv.receptorAddress.sin_port = htons( port_serv );
127 
128  return connectServer(serv);
129 }
130 
136 void vpClient::deconnect(const int &index)
137 {
138  if(index < (int)receptor_list.size() && index >= 0)
139  {
140 #ifdef UNIX
141  shutdown( receptor_list[index].socketFileDescriptorReceptor, SHUT_RDWR );
142 #else // WIN32
143  shutdown( receptor_list[index].socketFileDescriptorReceptor, SD_BOTH );
144 #endif
145  receptor_list.erase(receptor_list.begin()+index);
146  }
147 }
148 
153 {
154  for(unsigned int i = 0 ; i < receptor_list.size() ; i++){
155 #ifdef UNIX
156  shutdown( receptor_list[i].socketFileDescriptorReceptor, SHUT_RDWR );
157 #else // WIN32
158  shutdown( receptor_list[i].socketFileDescriptorReceptor, SD_BOTH );
159 #endif
160  receptor_list.erase(receptor_list.begin()+i);
161  i--;
162  }
163 }
164 
169 {
170  vpNetwork::print("Server");
171 }
172 
173 //Private function
174 bool vpClient::connectServer(vpNetwork::vpReceptor &serv)
175 {
176  serv.receptorAddressSize = sizeof( serv.receptorAddress );
177 
178  numberOfAttempts = 15;
179  unsigned int ind = 1;
180  int connectionResult;
181 
182  while(ind <= numberOfAttempts){
183  std::cout << "Attempt number " << ind << "..." << std::endl;
184 
185  connectionResult = connect( serv.socketFileDescriptorReceptor,
186  (sockaddr*) &serv.receptorAddress,
187  serv.receptorAddressSize );
188  if(connectionResult >= 0)
189  break;
190 
191  ind++;
192  vpTime::wait(1000);
193  }
194 
195  if( connectionResult< 0 )
196  {
197  vpERROR_TRACE( "ERROR connecting, the server may not be waiting for connection at this port.",
198  "vpClient::connectServer()");
199 
200  return false;
201  }
202 
203  receptor_list.push_back(serv);
204 
205 #ifdef SO_NOSIGPIPE
206  // Mac OS X does not have the MSG_NOSIGNAL flag. It does have this
207  // connections based version, however.
208  if (serv.socketFileDescriptorReceptor > 0) {
209  int set_option = 1;
210  if (0 == setsockopt(serv.socketFileDescriptorReceptor, SOL_SOCKET, SO_NOSIGPIPE, &set_option, sizeof(set_option))) {
211  } else {
212  std::cout << "Failed to set socket signal option" << std::endl;
213  }
214  }
215 #endif // SO_NOSIGPIPE
216 
217  std::cout << "Connected!" << std::endl;
218  return true;
219 }
bool connectToHostname(const std::string &hostname, const int &port_serv)
Definition: vpClient.cpp:66
socklen_t receptorAddressSize
Definition: vpNetwork.h:90
#define vpERROR_TRACE
Definition: vpDebug.h:379
virtual void print(const char *id="")
Definition: vpNetwork.cpp:124
virtual ~vpClient()
Definition: vpClient.cpp:51
void stop()
Definition: vpClient.cpp:152
This class represents a Transmission Control Protocol (TCP) network.
Definition: vpNetwork.h:83
int socketFileDescriptorReceptor
Definition: vpNetwork.h:88
static int wait(double t0, double t)
Definition: vpTime.cpp:149
bool connectToIP(const std::string &ip, const int &port_serv)
Definition: vpClient.cpp:111
vpClient()
Definition: vpClient.cpp:45
std::vector< vpReceptor > receptor_list
Definition: vpNetwork.h:108
void deconnect(const int &index=0)
Definition: vpClient.cpp:136
virtual void print()
Definition: vpClient.cpp:168
struct sockaddr_in receptorAddress
Definition: vpNetwork.h:94
std::string receptorIP
Definition: vpNetwork.h:95