39 #include <visp3/core/vpClient.h>
42 #ifdef VISP_HAVE_FUNC_INET_NTOP
44 #include <visp3/core/vpDebug.h>
68 struct hostent *server = gethostbyname(hostname.c_str());
70 if (server ==
nullptr) {
71 std::string noSuchHostMessage(
"ERROR, ");
72 noSuchHostMessage.append(hostname);
73 noSuchHostMessage.append(
": no such host\n");
74 vpERROR_TRACE(noSuchHostMessage.c_str(),
"vpClient::connectToHostname("
75 "const std::string &hostname, "
76 "const int &port_serv)");
80 vpNetwork::vpReceptor serv;
82 serv.socketFileDescriptorReceptor = socket(AF_INET, SOCK_STREAM, 0);
84 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
85 if (serv.socketFileDescriptorReceptor < 0) {
87 if (serv.socketFileDescriptorReceptor == INVALID_SOCKET) {
89 vpERROR_TRACE(
"ERROR opening socket",
"vpClient::connectToHostname()");
93 memset((
char *)&serv.receptorAddress,
'\0',
sizeof(serv.receptorAddress));
94 serv.receptorAddress.sin_family = AF_INET;
95 memmove((
char *)&serv.receptorAddress.sin_addr.s_addr, (
char *)server->h_addr, (
unsigned)server->h_length);
96 serv.receptorAddress.sin_port = htons((
unsigned short)port_serv);
97 serv.receptorIP = inet_ntoa(*(in_addr *)server->h_addr);
99 return connectServer(serv);
114 vpNetwork::vpReceptor serv;
116 serv.socketFileDescriptorReceptor = socket(AF_INET, SOCK_STREAM, 0);
118 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
119 if (serv.socketFileDescriptorReceptor < 0) {
121 if (serv.socketFileDescriptorReceptor == INVALID_SOCKET) {
123 vpERROR_TRACE(
"ERROR opening socket",
"vpClient::connectToIP()");
127 memset((
char *)&serv.receptorAddress,
'\0',
sizeof(serv.receptorAddress));
128 serv.receptorAddress.sin_family = AF_INET;
129 serv.receptorAddress.sin_addr.s_addr = inet_addr(ip.c_str());
130 serv.receptorAddress.sin_port = htons((
unsigned short)port_serv);
132 return connectServer(serv);
143 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
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__)))
159 shutdown(
receptor_list[i].socketFileDescriptorReceptor, SHUT_RDWR);
161 shutdown(
receptor_list[i].socketFileDescriptorReceptor, SD_BOTH);
174 bool vpClient::connectServer(vpNetwork::vpReceptor &serv)
176 serv.receptorAddressSize =
sizeof(serv.receptorAddress);
178 m_numberOfAttempts = 15;
179 unsigned int ind = 1;
180 int connectionResult = -1;
182 while (ind <= m_numberOfAttempts) {
183 std::cout <<
"Attempt number " << ind <<
"..." << std::endl;
186 connect(serv.socketFileDescriptorReceptor, (sockaddr *)&serv.receptorAddress, serv.receptorAddressSize);
187 if (connectionResult >= 0)
194 if (connectionResult < 0) {
195 vpERROR_TRACE(
"ERROR connecting, the server may not be waiting for "
196 "connection at this port.",
197 "vpClient::connectServer()");
207 if (serv.socketFileDescriptorReceptor > 0) {
209 if (0 == setsockopt(serv.socketFileDescriptorReceptor, SOL_SOCKET, SO_NOSIGPIPE, &set_option,
sizeof(set_option))) {
212 std::cout <<
"Failed to set socket signal option" << std::endl;
217 std::cout <<
"Connected!" << std::endl;
221 #elif !defined(VISP_BUILD_SHARED_LIBS)
223 void 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)
This class represents a Transmission Control Protocol (TCP) network.
std::vector< vpReceptor > receptor_list
void print(const char *id="")
VISP_EXPORT int wait(double t0, double t)