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