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__))) // UNIX 45 #include <arpa/inet.h> 51 #if defined(__MINGW32__) 53 # define _WIN32_WINNT _WIN32_WINNT_VISTA // 0x0600 59 #include <visp3/core/vpUDPClient.h> 67 : m_is_init(false), m_serverAddress(), m_serverLength(0), m_socketFileDescriptor()
81 :
m_is_init(false), m_serverAddress(), m_serverLength(0), m_socketFileDescriptor()
97 void vpUDPClient::close()
100 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 101 ::close(m_socketFileDescriptor);
103 closesocket(m_socketFileDescriptor);
122 if (WSAStartup(MAKEWORD(2, 2), &m_wsa) != 0) {
123 std::stringstream ss;
124 ss <<
"Failed WSAStartup for the server, error code: " << WSAGetLastError();
130 m_socketFileDescriptor = socket(AF_INET, SOCK_DGRAM, 0);
132 if (m_socketFileDescriptor == INVALID_SOCKET)
134 if (m_socketFileDescriptor < 0)
139 memset(&m_serverAddress, 0,
sizeof(m_serverAddress));
140 std::stringstream ss;
142 struct addrinfo hints;
143 struct addrinfo *result = NULL;
144 struct addrinfo *ptr = NULL;
146 memset(&hints, 0,
sizeof(hints));
147 hints.ai_family = AF_INET;
148 hints.ai_socktype = SOCK_DGRAM;
149 hints.ai_protocol = IPPROTO_UDP;
151 DWORD dwRetval = getaddrinfo(hostname.c_str(), ss.str().c_str(), &hints, &result);
154 ss <<
"getaddrinfo failed with error: " << dwRetval;
158 for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
159 if (ptr->ai_family == AF_INET && ptr->ai_socktype == SOCK_DGRAM) {
160 m_serverAddress = *(
struct sockaddr_in *)ptr->ai_addr;
165 freeaddrinfo(result);
167 m_serverLength =
sizeof(m_serverAddress);
200 FD_SET(m_socketFileDescriptor, &s);
201 struct timeval timeout;
203 timeout.tv_sec = timeoutMs / 1000;
204 timeout.tv_usec = (timeoutMs % 1000) * 1000;
206 int retval = select((
int)m_socketFileDescriptor + 1, &s, NULL, NULL, timeoutMs > 0 ? &timeout : NULL);
209 std::cerr <<
"Error select!" << std::endl;
215 int length =
static_cast<int>(recvfrom(m_socketFileDescriptor, m_buf,
sizeof(m_buf), 0, (
struct sockaddr *)&m_serverAddress,
216 (socklen_t *)&m_serverLength));
218 return length < 0 ? -1 : 0;
221 msg = std::string(m_buf, length);
246 FD_SET(m_socketFileDescriptor, &s);
247 struct timeval timeout;
249 timeout.tv_sec = timeoutMs / 1000;
250 timeout.tv_usec = (timeoutMs % 1000) * 1000;
252 int retval = select((
int)m_socketFileDescriptor + 1, &s, NULL, NULL, timeoutMs > 0 ? &timeout : NULL);
255 std::cerr <<
"Error select!" << std::endl;
261 int length =
static_cast<int>(recvfrom(m_socketFileDescriptor, (
char *)msg, (
int)len, 0, (
struct sockaddr *)&m_serverAddress,
262 (socklen_t *)&m_serverLength));
264 return length < 0 ? -1 : 0;
305 if (msg.size() > VP_MAX_UDP_PAYLOAD) {
306 std::cerr <<
"Message is too long!" << std::endl;
311 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 312 return static_cast<int>(sendto(m_socketFileDescriptor, msg.c_str(), msg.size(), 0, (
struct sockaddr *)&m_serverAddress,
315 return sendto(m_socketFileDescriptor, msg.c_str(), (int)msg.size(), 0, (
struct sockaddr *)&m_serverAddress,
334 if (len > VP_MAX_UDP_PAYLOAD) {
335 std::cerr <<
"Message is too long!" << std::endl;
340 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 341 return static_cast<int>(sendto(m_socketFileDescriptor, msg, len, 0, (
struct sockaddr *)&m_serverAddress,
344 return sendto(m_socketFileDescriptor, (
char *)msg, (
int)len, 0, (
struct sockaddr *)&m_serverAddress,
349 #elif !defined(VISP_BUILD_SHARED_LIBS) 351 void dummy_vpUDPClient(){};
void init(const std::string &hostname, int port)
error that can be emited by ViSP classes.
int receive(std::string &msg, int timeoutMs=0)
Used to indicate that a parameter is not initialized.
int send(const std::string &msg)