39 #include <visp3/core/vpClient.h> 62 struct hostent *server = gethostbyname(hostname.c_str());
65 std::string noSuchHostMessage(
"ERROR, ");
66 noSuchHostMessage.append(hostname);
67 noSuchHostMessage.append(
": no such host\n");
68 vpERROR_TRACE(noSuchHostMessage.c_str(),
"vpClient::connectToHostname(" 69 "const std::string &hostname, " 70 "const int &port_serv)");
74 vpNetwork::vpReceptor serv;
76 serv.socketFileDescriptorReceptor = socket(AF_INET, SOCK_STREAM, 0);
78 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 79 if (serv.socketFileDescriptorReceptor < 0) {
81 if (serv.socketFileDescriptorReceptor == INVALID_SOCKET) {
83 vpERROR_TRACE(
"ERROR opening socket",
"vpClient::connectToHostname()");
87 memset((
char *)&serv.receptorAddress,
'\0',
sizeof(serv.receptorAddress));
88 serv.receptorAddress.sin_family = AF_INET;
89 memmove((
char *)&serv.receptorAddress.sin_addr.s_addr, (
char *)server->h_addr, (
unsigned)server->h_length);
90 serv.receptorAddress.sin_port = htons((
unsigned short)port_serv);
91 serv.receptorIP = inet_ntoa(*(in_addr *)server->h_addr);
93 return connectServer(serv);
108 vpNetwork::vpReceptor serv;
110 serv.socketFileDescriptorReceptor = socket(AF_INET, SOCK_STREAM, 0);
112 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 113 if (serv.socketFileDescriptorReceptor < 0) {
115 if (serv.socketFileDescriptorReceptor == INVALID_SOCKET) {
117 vpERROR_TRACE(
"ERROR opening socket",
"vpClient::connectToIP()");
121 memset((
char *)&serv.receptorAddress,
'\0',
sizeof(serv.receptorAddress));
122 serv.receptorAddress.sin_family = AF_INET;
123 serv.receptorAddress.sin_addr.s_addr = inet_addr(ip.c_str());
124 serv.receptorAddress.sin_port = htons((
unsigned short)port_serv);
126 return connectServer(serv);
137 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 138 shutdown(
receptor_list[index].socketFileDescriptorReceptor, SHUT_RDWR);
140 shutdown(
receptor_list[index].socketFileDescriptorReceptor, SD_BOTH);
152 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 153 shutdown(
receptor_list[i].socketFileDescriptorReceptor, SHUT_RDWR);
155 shutdown(
receptor_list[i].socketFileDescriptorReceptor, SD_BOTH);
168 bool vpClient::connectServer(vpNetwork::vpReceptor &serv)
170 serv.receptorAddressSize =
sizeof(serv.receptorAddress);
172 numberOfAttempts = 15;
173 unsigned int ind = 1;
174 int connectionResult = -1;
176 while (ind <= numberOfAttempts) {
177 std::cout <<
"Attempt number " << ind <<
"..." << std::endl;
180 connect(serv.socketFileDescriptorReceptor, (sockaddr *)&serv.receptorAddress, serv.receptorAddressSize);
181 if (connectionResult >= 0)
188 if (connectionResult < 0) {
189 vpERROR_TRACE(
"ERROR connecting, the server may not be waiting for " 190 "connection at this port.",
191 "vpClient::connectServer()");
201 if (serv.socketFileDescriptorReceptor > 0) {
203 if (0 == setsockopt(serv.socketFileDescriptorReceptor, SOL_SOCKET, SO_NOSIGPIPE, &set_option,
sizeof(set_option))) {
205 std::cout <<
"Failed to set socket signal option" << std::endl;
208 #endif // SO_NOSIGPIPE 210 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