ViSP  2.8.0
vpClient.cpp
1 /****************************************************************************
2  *
3  * $Id: vpClient.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 unsigned 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 #ifdef UNIX
86  if ( serv.socketFileDescriptorReceptor < 0){
87 #else
88  if ( serv.socketFileDescriptorReceptor == INVALID_SOCKET){
89 #endif
90  vpERROR_TRACE( "ERROR opening socket",
91  "vpClient::connectToHostname()" );
92  return false;
93  }
94 
95  memset((char *) &serv.receptorAddress, '\0', sizeof(serv.receptorAddress));
96  serv.receptorAddress.sin_family = AF_INET;
97  memmove( (char *) &serv.receptorAddress.sin_addr.s_addr, (char *) server->h_addr, (unsigned)server->h_length );
98  serv.receptorAddress.sin_port = htons( (unsigned short)port_serv );
99  serv.receptorIP = inet_ntoa(*(in_addr *)server->h_addr);
100 
101  return connectServer(serv);
102 }
103 
114 bool vpClient::connectToIP(const std::string &ip, const unsigned int &port_serv)
115 {
117 
118  serv.socketFileDescriptorReceptor = socket( AF_INET, SOCK_STREAM, 0 );
119 
120 #ifdef UNIX
121  if ( serv.socketFileDescriptorReceptor < 0){
122 #else
123  if ( serv.socketFileDescriptorReceptor == INVALID_SOCKET){
124 #endif
125  vpERROR_TRACE( "ERROR opening socket",
126  "vpClient::connectToIP()" );
127  return false;
128  }
129 
130  memset((char *) &serv.receptorAddress, '\0', sizeof(serv.receptorAddress));
131  serv.receptorAddress.sin_family = AF_INET;
132  serv.receptorAddress.sin_addr.s_addr = inet_addr(ip.c_str());
133  serv.receptorAddress.sin_port = htons( (unsigned short)port_serv );
134 
135  return connectServer(serv);
136 }
137 
143 void vpClient::deconnect(const unsigned int &index)
144 {
145  if(index < receptor_list.size())
146  {
147 #ifdef UNIX
148  shutdown( receptor_list[index].socketFileDescriptorReceptor, SHUT_RDWR );
149 #else // WIN32
150  shutdown( receptor_list[index].socketFileDescriptorReceptor, SD_BOTH );
151 #endif
152  receptor_list.erase(receptor_list.begin()+(int)index);
153  }
154 }
155 
160 {
161  for(unsigned int i = 0 ; i < receptor_list.size() ; i++){
162 #ifdef UNIX
163  shutdown( receptor_list[i].socketFileDescriptorReceptor, SHUT_RDWR );
164 #else // WIN32
165  shutdown( receptor_list[i].socketFileDescriptorReceptor, SD_BOTH );
166 #endif
167  receptor_list.erase(receptor_list.begin()+(int)i);
168  i--;
169  }
170 }
171 
176 {
177  vpNetwork::print("Server");
178 }
179 
180 //Private function
181 bool vpClient::connectServer(vpNetwork::vpReceptor &serv)
182 {
183  serv.receptorAddressSize = sizeof( serv.receptorAddress );
184 
185  numberOfAttempts = 15;
186  unsigned int ind = 1;
187  int connectionResult;
188 
189  while(ind <= numberOfAttempts){
190  std::cout << "Attempt number " << ind << "..." << std::endl;
191 
192  connectionResult = connect( serv.socketFileDescriptorReceptor,
193  (sockaddr*) &serv.receptorAddress,
194  serv.receptorAddressSize );
195  if(connectionResult >= 0)
196  break;
197 
198  ind++;
199  vpTime::wait(1000);
200  }
201 
202  if( connectionResult< 0 )
203  {
204  vpERROR_TRACE( "ERROR connecting, the server may not be waiting for connection at this port.",
205  "vpClient::connectServer()");
206 
207  return false;
208  }
209 
210  receptor_list.push_back(serv);
211 
212 #ifdef SO_NOSIGPIPE
213  // Mac OS X does not have the MSG_NOSIGNAL flag. It does have this
214  // connections based version, however.
215  if (serv.socketFileDescriptorReceptor > 0) {
216  int set_option = 1;
217  if (0 == setsockopt(serv.socketFileDescriptorReceptor, SOL_SOCKET, SO_NOSIGPIPE, &set_option, sizeof(set_option))) {
218  } else {
219  std::cout << "Failed to set socket signal option" << std::endl;
220  }
221  }
222 #endif // SO_NOSIGPIPE
223 
224  std::cout << "Connected!" << std::endl;
225  return true;
226 }
socklen_t receptorAddressSize
Definition: vpNetwork.h:90
#define vpERROR_TRACE
Definition: vpDebug.h:379
void print(const char *id="")
Definition: vpNetwork.cpp:125
virtual ~vpClient()
Definition: vpClient.cpp:51
bool connectToIP(const std::string &ip, const unsigned int &port_serv)
Definition: vpClient.cpp:114
void stop()
Definition: vpClient.cpp:159
void deconnect(const unsigned int &index=0)
Definition: vpClient.cpp:143
This class represents a Transmission Control Protocol (TCP) network.
Definition: vpNetwork.h:83
int socketFileDescriptorReceptor
Definition: vpNetwork.h:89
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:66
vpClient()
Definition: vpClient.cpp:45
std::vector< vpReceptor > receptor_list
Definition: vpNetwork.h:113
void print()
Definition: vpClient.cpp:175
struct sockaddr_in receptorAddress
Definition: vpNetwork.h:95
std::string receptorIP
Definition: vpNetwork.h:96