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> 50 # define WSAGetLastError() strerror(errno) 52 # if defined(__MINGW32__) 56 # define _WIN32_WINNT _WIN32_WINNT_VISTA // 0x0600 63 # include <Ws2tcpip.h> 66 #include <visp3/core/vpUDPServer.h> 75 : m_clientAddress(), m_clientLength(0), m_serverAddress(), m_socketFileDescriptor(0)
91 : m_clientAddress(), m_clientLength(0), m_serverAddress(), m_socketFileDescriptor(0)
102 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 103 close(m_socketFileDescriptor);
105 closesocket(m_socketFileDescriptor);
110 void vpUDPServer::init(
const std::string &hostname,
int port)
113 if (WSAStartup(MAKEWORD(2, 2), &m_wsa) != 0) {
114 std::stringstream ss;
115 ss <<
"Failed WSAStartup for the server, error code: " << WSAGetLastError();
121 m_socketFileDescriptor = socket(AF_INET, SOCK_DGRAM, 0);
123 if (m_socketFileDescriptor == INVALID_SOCKET)
125 if (m_socketFileDescriptor < 0)
134 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 136 setsockopt(m_socketFileDescriptor, SOL_SOCKET, SO_REUSEADDR, (
const void *)&optval,
sizeof(
int));
138 const char optval = 1;
139 setsockopt(m_socketFileDescriptor, SOL_SOCKET, SO_REUSEADDR, (
const char *)&optval,
sizeof(
int));
143 memset(&m_serverAddress, 0,
sizeof(m_serverAddress));
144 if (hostname.empty()) {
145 m_serverAddress.sin_family = AF_INET;
146 m_serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
147 m_serverAddress.sin_port = htons((
unsigned short)port);
149 std::stringstream ss;
151 struct addrinfo hints;
152 struct addrinfo *result = NULL;
153 struct addrinfo *ptr = NULL;
155 memset(&hints, 0,
sizeof(hints));
156 hints.ai_family = AF_INET;
157 hints.ai_socktype = SOCK_DGRAM;
158 hints.ai_protocol = IPPROTO_UDP;
160 DWORD dwRetval = getaddrinfo(hostname.c_str(), ss.str().c_str(), &hints, &result);
163 ss <<
"getaddrinfo failed with error: " << dwRetval;
167 for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
168 if (ptr->ai_family == AF_INET && ptr->ai_socktype == SOCK_DGRAM) {
169 m_serverAddress = *(
struct sockaddr_in *)ptr->ai_addr;
174 freeaddrinfo(result);
178 if (bind(m_socketFileDescriptor, (
struct sockaddr *)&m_serverAddress,
sizeof(m_serverAddress)) < 0)
181 m_clientLength =
sizeof(m_clientAddress);
197 std::string hostInfo =
"";
198 return receive(msg, hostInfo, timeoutMs);
217 FD_SET(m_socketFileDescriptor, &s);
218 struct timeval timeout;
220 timeout.tv_sec = timeoutMs / 1000;
221 timeout.tv_usec = (timeoutMs % 1000) * 1000;
223 int retval = select((
int)m_socketFileDescriptor + 1, &s, NULL, NULL, timeoutMs > 0 ? &timeout : NULL);
226 std::cerr <<
"Error select!" << std::endl;
232 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 233 int length =
static_cast<int>(recvfrom(m_socketFileDescriptor, m_buf,
sizeof(m_buf), 0, (
struct sockaddr *)&m_clientAddress,
234 (socklen_t *)&m_clientLength));
237 recvfrom(m_socketFileDescriptor, m_buf,
sizeof(m_buf), 0, (
struct sockaddr *)&m_clientAddress, &m_clientLength);
240 return length < 0 ? -1 : 0;
243 msg = std::string(m_buf, length);
246 char hostname[NI_MAXHOST];
247 char servInfo[NI_MAXSERV];
248 DWORD dwRetval = getnameinfo((
struct sockaddr *)&m_clientAddress,
sizeof(
struct sockaddr), hostname, NI_MAXHOST,
249 servInfo, NI_MAXSERV, NI_NUMERICSERV);
251 std::string hostName =
"", hostIp =
"", hostPort =
"";
253 std::cerr <<
"getnameinfo failed with error: " << WSAGetLastError() << std::endl;
259 char result[INET_ADDRSTRLEN];
260 const char *ptr = inet_ntop(AF_INET, (
void *)&m_clientAddress.sin_addr, result,
sizeof(result));
262 std::cerr <<
"inet_ntop failed with error: " << WSAGetLastError() << std::endl;
267 std::stringstream ss;
268 ss << hostName <<
" " << hostIp <<
" " << hostPort;
290 if (msg.size() > VP_MAX_UDP_PAYLOAD) {
291 std::cerr <<
"Message is too long!" << std::endl;
296 memset(&m_clientAddress, 0,
sizeof(m_clientAddress));
297 std::stringstream ss;
299 struct addrinfo hints;
300 struct addrinfo *result = NULL;
301 struct addrinfo *ptr = NULL;
303 memset(&hints, 0,
sizeof(hints));
304 hints.ai_family = AF_INET;
305 hints.ai_socktype = SOCK_DGRAM;
306 hints.ai_protocol = IPPROTO_UDP;
308 DWORD dwRetval = getaddrinfo(hostname.c_str(), ss.str().c_str(), &hints, &result);
311 ss <<
"getaddrinfo failed with error: " << dwRetval;
315 for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
316 if (ptr->ai_family == AF_INET && ptr->ai_socktype == SOCK_DGRAM) {
317 m_clientAddress = *(
struct sockaddr_in *)ptr->ai_addr;
322 freeaddrinfo(result);
325 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 326 return static_cast<int>(sendto(m_socketFileDescriptor, msg.c_str(), msg.size(), 0, (
struct sockaddr *)&m_clientAddress,
329 return sendto(m_socketFileDescriptor, msg.c_str(), (int)msg.size(), 0, (
struct sockaddr *)&m_clientAddress,
334 #elif !defined(VISP_BUILD_SHARED_LIBS) 336 void dummy_vpUDPServer(){};
int send(const std::string &msg, const std::string &hostname, int port)
error that can be emited by ViSP classes.
int receive(std::string &msg, int timeoutMs=0)