Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpClient.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 Client
32  *
33  * Authors:
34  * Aurelien Yol
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpClient.h>
39 
40 vpClient::vpClient() : vpNetwork(), numberOfAttempts(0)
41 {
42 }
43 
48 {
49  stop();
50 }
51 
62 bool vpClient::connectToHostname(const std::string &hostname, const unsigned int &port_serv)
63 {
64  // get server host information from hostname
65  struct hostent *server = gethostbyname( hostname.c_str() );
66 
67  if ( server == NULL )
68  {
69  std::string noSuchHostMessage( "ERROR, " );
70  noSuchHostMessage.append( hostname );
71  noSuchHostMessage.append( ": no such host\n" );
72  vpERROR_TRACE( noSuchHostMessage.c_str(),
73  "vpClient::connectToHostname(const std::string &hostname, const int &port_serv)" );
74  return false;
75  }
76 
77  vpNetwork::vpReceptor serv;
78 
79  serv.socketFileDescriptorReceptor = socket( AF_INET, SOCK_STREAM, 0 );
80 
81 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
82  if ( serv.socketFileDescriptorReceptor < 0){
83 #else
84  if ( serv.socketFileDescriptorReceptor == INVALID_SOCKET){
85 #endif
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, (unsigned)server->h_length );
94  serv.receptorAddress.sin_port = htons( (unsigned short)port_serv );
95  serv.receptorIP = inet_ntoa(*(in_addr *)server->h_addr);
96 
97  return connectServer(serv);
98 }
99 
110 bool vpClient::connectToIP(const std::string &ip, const unsigned int &port_serv)
111 {
112  vpNetwork::vpReceptor serv;
113 
114  serv.socketFileDescriptorReceptor = socket( AF_INET, SOCK_STREAM, 0 );
115 
116 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
117  if ( serv.socketFileDescriptorReceptor < 0){
118 #else
119  if ( serv.socketFileDescriptorReceptor == INVALID_SOCKET){
120 #endif
121  vpERROR_TRACE( "ERROR opening socket",
122  "vpClient::connectToIP()" );
123  return false;
124  }
125 
126  memset((char *) &serv.receptorAddress, '\0', sizeof(serv.receptorAddress));
127  serv.receptorAddress.sin_family = AF_INET;
128  serv.receptorAddress.sin_addr.s_addr = inet_addr(ip.c_str());
129  serv.receptorAddress.sin_port = htons( (unsigned short)port_serv );
130 
131  return connectServer(serv);
132 }
133 
139 void vpClient::deconnect(const unsigned int &index)
140 {
141  if(index < receptor_list.size())
142  {
143 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
144  shutdown( receptor_list[index].socketFileDescriptorReceptor, SHUT_RDWR );
145 #else // _WIN32
146  shutdown( receptor_list[index].socketFileDescriptorReceptor, SD_BOTH );
147 #endif
148  receptor_list.erase(receptor_list.begin()+(int)index);
149  }
150 }
151 
156 {
157  for(unsigned int i = 0 ; i < receptor_list.size() ; i++){
158 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
159  shutdown( receptor_list[i].socketFileDescriptorReceptor, SHUT_RDWR );
160 #else // _WIN32
161  shutdown( receptor_list[i].socketFileDescriptorReceptor, SD_BOTH );
162 #endif
163  receptor_list.erase(receptor_list.begin()+(int)i);
164  i--;
165  }
166 }
167 
172 {
173  vpNetwork::print("Server");
174 }
175 
176 //Private function
177 bool vpClient::connectServer(vpNetwork::vpReceptor &serv)
178 {
179  serv.receptorAddressSize = sizeof( serv.receptorAddress );
180 
181  numberOfAttempts = 15;
182  unsigned int ind = 1;
183  int connectionResult=-1;
184 
185  while(ind <= numberOfAttempts){
186  std::cout << "Attempt number " << ind << "..." << std::endl;
187 
188  connectionResult = connect( serv.socketFileDescriptorReceptor,
189  (sockaddr*) &serv.receptorAddress,
190  serv.receptorAddressSize );
191  if(connectionResult >= 0)
192  break;
193 
194  ind++;
195  vpTime::wait(1000);
196  }
197 
198  if( connectionResult< 0 )
199  {
200  vpERROR_TRACE( "ERROR connecting, the server may not be waiting for connection at this port.",
201  "vpClient::connectServer()");
202 
203  return false;
204  }
205 
206  receptor_list.push_back(serv);
207 
208 #ifdef SO_NOSIGPIPE
209  // Mac OS X does not have the MSG_NOSIGNAL flag. It does have this
210  // connections based version, however.
211  if (serv.socketFileDescriptorReceptor > 0) {
212  int set_option = 1;
213  if (0 == setsockopt(serv.socketFileDescriptorReceptor, SOL_SOCKET, SO_NOSIGPIPE, &set_option, sizeof(set_option))) {
214  } else {
215  std::cout << "Failed to set socket signal option" << std::endl;
216  }
217  }
218 #endif // SO_NOSIGPIPE
219 
220  std::cout << "Connected!" << std::endl;
221  return true;
222 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:157
void print(const char *id="")
Definition: vpNetwork.cpp:121
virtual ~vpClient()
Definition: vpClient.cpp:47
#define vpERROR_TRACE
Definition: vpDebug.h:391
bool connectToIP(const std::string &ip, const unsigned int &port_serv)
Definition: vpClient.cpp:110
void stop()
Definition: vpClient.cpp:155
void deconnect(const unsigned int &index=0)
Definition: vpClient.cpp:139
This class represents a Transmission Control Protocol (TCP) network.
Definition: vpNetwork.h:82
bool connectToHostname(const std::string &hostname, const unsigned int &port_serv)
Definition: vpClient.cpp:62
vpClient()
Definition: vpClient.cpp:40
std::vector< vpReceptor > receptor_list
Definition: vpNetwork.h:122
void print()
Definition: vpClient.cpp:171