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