39 #include <visp3/core/vpConfig.h>
42 #ifdef VISP_HAVE_FUNC_INET_NTOP
44 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
45 #include <arpa/inet.h>
51 #if defined(__MINGW32__)
53 #define _WIN32_WINNT _WIN32_WINNT_VISTA
59 #include <visp3/core/vpUDPClient.h>
67 : m_is_init(false), m_serverAddress(), m_serverLength(0), m_socketFileDescriptor()
82 : m_is_init(false), m_serverAddress(), m_serverLength(0), m_socketFileDescriptor()
99 void vpUDPClient::close()
102 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
103 ::close(m_socketFileDescriptor);
105 closesocket(m_socketFileDescriptor);
124 if (WSAStartup(MAKEWORD(2, 2), &m_wsa) != 0) {
125 std::stringstream ss;
126 ss <<
"Failed WSAStartup for the server, error code: " << WSAGetLastError();
132 m_socketFileDescriptor = socket(AF_INET, SOCK_DGRAM, 0);
134 if (m_socketFileDescriptor == INVALID_SOCKET)
136 if (m_socketFileDescriptor < 0)
141 memset(&m_serverAddress, 0,
sizeof(m_serverAddress));
142 std::stringstream ss;
144 struct addrinfo hints;
145 struct addrinfo *result = NULL;
146 struct addrinfo *ptr = NULL;
148 memset(&hints, 0,
sizeof(hints));
149 hints.ai_family = AF_INET;
150 hints.ai_socktype = SOCK_DGRAM;
151 hints.ai_protocol = IPPROTO_UDP;
153 DWORD dwRetval = getaddrinfo(hostname.c_str(), ss.str().c_str(), &hints, &result);
156 ss <<
"getaddrinfo failed with error: " << dwRetval;
160 for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
161 if (ptr->ai_family == AF_INET && ptr->ai_socktype == SOCK_DGRAM) {
162 m_serverAddress = *(
struct sockaddr_in *)ptr->ai_addr;
167 freeaddrinfo(result);
169 m_serverLength =
sizeof(m_serverAddress);
202 FD_SET(m_socketFileDescriptor, &s);
203 struct timeval timeout;
205 timeout.tv_sec = timeoutMs / 1000;
206 timeout.tv_usec = (timeoutMs % 1000) * 1000;
208 int retval = select((
int)m_socketFileDescriptor + 1, &s, NULL, NULL, timeoutMs > 0 ? &timeout : NULL);
211 std::cerr <<
"Error select!" << std::endl;
217 int length =
static_cast<int>(recvfrom(m_socketFileDescriptor, m_buf,
sizeof(m_buf), 0,
218 (
struct sockaddr *)&m_serverAddress, (socklen_t *)&m_serverLength));
220 return length < 0 ? -1 : 0;
223 msg = std::string(m_buf, length);
248 FD_SET(m_socketFileDescriptor, &s);
249 struct timeval timeout;
251 timeout.tv_sec = timeoutMs / 1000;
252 timeout.tv_usec = (timeoutMs % 1000) * 1000;
254 int retval = select((
int)m_socketFileDescriptor + 1, &s, NULL, NULL, timeoutMs > 0 ? &timeout : NULL);
257 std::cerr <<
"Error select!" << std::endl;
263 int length =
static_cast<int>(recvfrom(m_socketFileDescriptor, (
char *)msg, (
int)len, 0,
264 (
struct sockaddr *)&m_serverAddress, (socklen_t *)&m_serverLength));
266 return length < 0 ? -1 : 0;
306 if (msg.size() > VP_MAX_UDP_PAYLOAD) {
307 std::cerr <<
"Message is too long!" << std::endl;
312 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
313 return static_cast<int>(
314 sendto(m_socketFileDescriptor, msg.c_str(), msg.size(), 0, (
struct sockaddr *)&m_serverAddress, m_serverLength));
316 return sendto(m_socketFileDescriptor, msg.c_str(), (
int)msg.size(), 0, (
struct sockaddr *)&m_serverAddress,
335 if (len > VP_MAX_UDP_PAYLOAD) {
336 std::cerr <<
"Message is too long!" << std::endl;
341 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
342 return static_cast<int>(
343 sendto(m_socketFileDescriptor, msg, len, 0, (
struct sockaddr *)&m_serverAddress, m_serverLength));
345 return sendto(m_socketFileDescriptor, (
char *)msg, (
int)len, 0, (
struct sockaddr *)&m_serverAddress, m_serverLength);
349 #elif !defined(VISP_BUILD_SHARED_LIBS)
351 void dummy_vpUDPClient(){};
error that can be emitted by ViSP classes.
@ notInitialized
Used to indicate that a parameter is not initialized.
int receive(std::string &msg, int timeoutMs=0)
void init(const std::string &hostname, int port)
int send(const std::string &msg)