34#include <visp3/core/vpConfig.h>
38#if defined(WINRT) || defined(_WIN32)
39#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
40#define _WINSOCK_DEPRECATED_NO_WARNINGS
45#ifdef VISP_HAVE_FUNC_INET_NTOP
47#include <visp3/core/vpClient.h>
48#include <visp3/core/vpDebug.h>
72 struct hostent *server = gethostbyname(hostname.c_str());
74 if (server ==
nullptr) {
75 std::string noSuchHostMessage(
"ERROR, ");
76 noSuchHostMessage.append(hostname);
77 noSuchHostMessage.append(
": no such host\n");
78 vpERROR_TRACE(noSuchHostMessage.c_str(),
"vpClient::connectToHostname("
79 "const std::string &hostname, "
80 "const int &port_serv)");
84 vpNetwork::vpReceptor serv;
86 serv.socketFileDescriptorReceptor = socket(AF_INET, SOCK_STREAM, 0);
88#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
89 if (serv.socketFileDescriptorReceptor < 0) {
91 if (serv.socketFileDescriptorReceptor == INVALID_SOCKET) {
93 vpERROR_TRACE(
"ERROR opening socket",
"vpClient::connectToHostname()");
97 memset((
char *)&serv.receptorAddress,
'\0',
sizeof(serv.receptorAddress));
98 serv.receptorAddress.sin_family = AF_INET;
99 memmove((
char *)&serv.receptorAddress.sin_addr.s_addr, (
char *)server->h_addr,
static_cast<unsigned int>(server->h_length));
100 serv.receptorAddress.sin_port = htons(
static_cast<unsigned short>(port_serv));
101 serv.receptorIP = inet_ntoa(*(in_addr *)server->h_addr);
103 return connectServer(serv);
118 vpNetwork::vpReceptor serv;
120 serv.socketFileDescriptorReceptor = socket(AF_INET, SOCK_STREAM, 0);
122#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
123 if (serv.socketFileDescriptorReceptor < 0) {
125 if (serv.socketFileDescriptorReceptor == INVALID_SOCKET) {
127 vpERROR_TRACE(
"ERROR opening socket",
"vpClient::connectToIP()");
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(
static_cast<unsigned short>(port_serv));
136 return connectServer(serv);
147#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
148 shutdown(
receptor_list[index].socketFileDescriptorReceptor, SHUT_RDWR);
150 shutdown(
receptor_list[index].socketFileDescriptorReceptor, SD_BOTH);
162#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
163 shutdown(
receptor_list[i].socketFileDescriptorReceptor, SHUT_RDWR);
165 shutdown(
receptor_list[i].socketFileDescriptorReceptor, SD_BOTH);
178bool vpClient::connectServer(vpNetwork::vpReceptor &serv)
180 serv.receptorAddressSize =
sizeof(serv.receptorAddress);
182 m_numberOfAttempts = 15;
183 unsigned int ind = 1;
184 int connectionResult = -1;
186 while (ind <= m_numberOfAttempts) {
187 std::cout <<
"Attempt number " << ind <<
"..." << std::endl;
190 connect(serv.socketFileDescriptorReceptor, (sockaddr *)&serv.receptorAddress, serv.receptorAddressSize);
191 if (connectionResult >= 0)
198 if (connectionResult < 0) {
199 vpERROR_TRACE(
"ERROR connecting, the server may not be waiting for "
200 "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))) {
216 std::cout <<
"Failed to set socket signal option" << std::endl;
221 std::cout <<
"Connected!" << std::endl;
225#elif !defined(VISP_BUILD_SHARED_LIBS)
227void dummy_vpClient() { }
bool connectToIP(const std::string &ip, const unsigned int &port_serv)
bool connectToHostname(const std::string &hostname, const unsigned int &port_serv)
virtual ~vpClient() VP_OVERRIDE
void deconnect(const unsigned int &index=0)
std::vector< vpReceptor > receptor_list
void print(const char *id="")
VISP_EXPORT int wait(double t0, double t)