38 #include <visp3/core/vpClient.h>
65 struct hostent *server = gethostbyname( hostname.c_str() );
69 std::string noSuchHostMessage(
"ERROR, " );
70 noSuchHostMessage.append( hostname );
71 noSuchHostMessage.append(
": no such host\n" );
73 "vpClient::connectToHostname(const std::string &hostname, const int &port_serv)" );
77 vpNetwork::vpReceptor serv;
79 serv.socketFileDescriptorReceptor = socket( AF_INET, SOCK_STREAM, 0 );
81 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
82 if ( serv.socketFileDescriptorReceptor < 0){
84 if ( serv.socketFileDescriptorReceptor == INVALID_SOCKET){
87 "vpClient::connectToHostname()" );
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);
97 return connectServer(serv);
112 vpNetwork::vpReceptor serv;
114 serv.socketFileDescriptorReceptor = socket( AF_INET, SOCK_STREAM, 0 );
116 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
117 if ( serv.socketFileDescriptorReceptor < 0){
119 if ( serv.socketFileDescriptorReceptor == INVALID_SOCKET){
122 "vpClient::connectToIP()" );
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 );
131 return connectServer(serv);
143 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
144 shutdown(
receptor_list[index].socketFileDescriptorReceptor, SHUT_RDWR );
146 shutdown(
receptor_list[index].socketFileDescriptorReceptor, SD_BOTH );
158 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
159 shutdown(
receptor_list[i].socketFileDescriptorReceptor, SHUT_RDWR );
161 shutdown(
receptor_list[i].socketFileDescriptorReceptor, SD_BOTH );
177 bool vpClient::connectServer(vpNetwork::vpReceptor &serv)
179 serv.receptorAddressSize =
sizeof( serv.receptorAddress );
181 numberOfAttempts = 15;
182 unsigned int ind = 1;
183 int connectionResult=-1;
185 while(ind <= numberOfAttempts){
186 std::cout <<
"Attempt number " << ind <<
"..." << std::endl;
188 connectionResult = connect( serv.socketFileDescriptorReceptor,
189 (sockaddr*) &serv.receptorAddress,
190 serv.receptorAddressSize );
191 if(connectionResult >= 0)
198 if( connectionResult< 0 )
200 vpERROR_TRACE(
"ERROR connecting, the server may not be waiting for connection at this port.",
201 "vpClient::connectServer()");
211 if (serv.socketFileDescriptorReceptor > 0) {
213 if (0 == setsockopt(serv.socketFileDescriptorReceptor, SOL_SOCKET, SO_NOSIGPIPE, &set_option,
sizeof(set_option))) {
215 std::cout <<
"Failed to set socket signal option" << std::endl;
218 #endif // SO_NOSIGPIPE
220 std::cout <<
"Connected!" << std::endl;
VISP_EXPORT int wait(double t0, double t)
void print(const char *id="")
bool connectToIP(const std::string &ip, const unsigned int &port_serv)
void deconnect(const unsigned int &index=0)
This class represents a Transmission Control Protocol (TCP) network.
bool connectToHostname(const std::string &hostname, const unsigned int &port_serv)
std::vector< vpReceptor > receptor_list