39 #include <visp3/core/vpClient.h> 42 #ifdef VISP_HAVE_FUNC_INET_NTOP 65 struct hostent *server = gethostbyname(hostname.c_str());
68 std::string noSuchHostMessage(
"ERROR, ");
69 noSuchHostMessage.append(hostname);
70 noSuchHostMessage.append(
": no such host\n");
71 vpERROR_TRACE(noSuchHostMessage.c_str(),
"vpClient::connectToHostname(" 72 "const std::string &hostname, " 73 "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) {
86 vpERROR_TRACE(
"ERROR opening socket",
"vpClient::connectToHostname()");
90 memset((
char *)&serv.receptorAddress,
'\0',
sizeof(serv.receptorAddress));
91 serv.receptorAddress.sin_family = AF_INET;
92 memmove((
char *)&serv.receptorAddress.sin_addr.s_addr, (
char *)server->h_addr, (
unsigned)server->h_length);
93 serv.receptorAddress.sin_port = htons((
unsigned short)port_serv);
94 serv.receptorIP = inet_ntoa(*(in_addr *)server->h_addr);
96 return connectServer(serv);
111 vpNetwork::vpReceptor serv;
113 serv.socketFileDescriptorReceptor = socket(AF_INET, SOCK_STREAM, 0);
115 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 116 if (serv.socketFileDescriptorReceptor < 0) {
118 if (serv.socketFileDescriptorReceptor == INVALID_SOCKET) {
120 vpERROR_TRACE(
"ERROR opening socket",
"vpClient::connectToIP()");
124 memset((
char *)&serv.receptorAddress,
'\0',
sizeof(serv.receptorAddress));
125 serv.receptorAddress.sin_family = AF_INET;
126 serv.receptorAddress.sin_addr.s_addr = inet_addr(ip.c_str());
127 serv.receptorAddress.sin_port = htons((
unsigned short)port_serv);
129 return connectServer(serv);
140 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 141 shutdown(
receptor_list[index].socketFileDescriptorReceptor, SHUT_RDWR);
143 shutdown(
receptor_list[index].socketFileDescriptorReceptor, SD_BOTH);
155 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 156 shutdown(
receptor_list[i].socketFileDescriptorReceptor, SHUT_RDWR);
158 shutdown(
receptor_list[i].socketFileDescriptorReceptor, SD_BOTH);
171 bool vpClient::connectServer(vpNetwork::vpReceptor &serv)
173 serv.receptorAddressSize =
sizeof(serv.receptorAddress);
175 numberOfAttempts = 15;
176 unsigned int ind = 1;
177 int connectionResult = -1;
179 while (ind <= numberOfAttempts) {
180 std::cout <<
"Attempt number " << ind <<
"..." << std::endl;
183 connect(serv.socketFileDescriptorReceptor, (sockaddr *)&serv.receptorAddress, serv.receptorAddressSize);
184 if (connectionResult >= 0)
191 if (connectionResult < 0) {
192 vpERROR_TRACE(
"ERROR connecting, the server may not be waiting for " 193 "connection at this port.",
194 "vpClient::connectServer()");
204 if (serv.socketFileDescriptorReceptor > 0) {
206 if (0 == setsockopt(serv.socketFileDescriptorReceptor, SOL_SOCKET, SO_NOSIGPIPE, &set_option,
sizeof(set_option))) {
208 std::cout <<
"Failed to set socket signal option" << std::endl;
211 #endif // SO_NOSIGPIPE 213 std::cout <<
"Connected!" << std::endl;
217 #elif !defined(VISP_BUILD_SHARED_LIBS) 219 void dummy_vpClient(){};
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