39 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 40 #include <arpa/inet.h> 45 #define WSAGetLastError() strerror(errno) 47 #if defined(__MINGW32__) 48 #define _WIN32_WINNT _WIN32_WINNT_VISTA // 0x0600 53 #include <visp3/core/vpUDPServer.h> 62 : m_clientAddress(), m_clientLength(0), m_serverAddress(), m_socketFileDescriptor(0)
78 : m_clientAddress(), m_clientLength(0), m_serverAddress(), m_socketFileDescriptor(0)
89 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 90 close(m_socketFileDescriptor);
92 closesocket(m_socketFileDescriptor);
97 void vpUDPServer::init(
const std::string &hostname,
const int port)
100 if (WSAStartup(MAKEWORD(2, 2), &m_wsa) != 0) {
101 std::stringstream ss;
102 ss <<
"Failed WSAStartup for the server, error code: " << WSAGetLastError();
108 m_socketFileDescriptor = socket(AF_INET, SOCK_DGRAM, 0);
110 if (m_socketFileDescriptor == INVALID_SOCKET)
112 if (m_socketFileDescriptor < 0)
121 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 123 setsockopt(m_socketFileDescriptor, SOL_SOCKET, SO_REUSEADDR, (
const void *)&optval,
sizeof(
int));
125 const char optval = 1;
126 setsockopt(m_socketFileDescriptor, SOL_SOCKET, SO_REUSEADDR, (
const char *)&optval,
sizeof(
int));
130 memset(&m_serverAddress, 0,
sizeof(m_serverAddress));
131 if (hostname.empty()) {
132 m_serverAddress.sin_family = AF_INET;
133 m_serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
134 m_serverAddress.sin_port = htons((
unsigned short)port);
136 std::stringstream ss;
138 struct addrinfo hints;
139 struct addrinfo *result = NULL;
140 struct addrinfo *ptr = NULL;
142 memset(&hints, 0,
sizeof(hints));
143 hints.ai_family = AF_INET;
144 hints.ai_socktype = SOCK_DGRAM;
145 hints.ai_protocol = IPPROTO_UDP;
147 DWORD dwRetval = getaddrinfo(hostname.c_str(), ss.str().c_str(), &hints, &result);
150 ss <<
"getaddrinfo failed with error: " << dwRetval;
154 for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
155 if (ptr->ai_family == AF_INET && ptr->ai_socktype == SOCK_DGRAM) {
156 m_serverAddress = *(
struct sockaddr_in *)ptr->ai_addr;
161 freeaddrinfo(result);
165 if (bind(m_socketFileDescriptor, (
struct sockaddr *)&m_serverAddress,
sizeof(m_serverAddress)) < 0)
168 m_clientLength =
sizeof(m_clientAddress);
184 std::string hostInfo =
"";
185 return receive(msg, hostInfo, timeoutMs);
204 FD_SET(m_socketFileDescriptor, &s);
205 struct timeval timeout;
207 timeout.tv_sec = timeoutMs / 1000;
208 timeout.tv_usec = (timeoutMs % 1000) * 1000;
210 int retval = select((
int)m_socketFileDescriptor + 1, &s, NULL, NULL, timeoutMs > 0 ? &timeout : NULL);
213 std::cerr <<
"Error select!" << std::endl;
219 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 220 int length = recvfrom(m_socketFileDescriptor, m_buf,
sizeof(m_buf), 0, (
struct sockaddr *)&m_clientAddress,
221 (socklen_t *)&m_clientLength);
224 recvfrom(m_socketFileDescriptor, m_buf,
sizeof(m_buf), 0, (
struct sockaddr *)&m_clientAddress, &m_clientLength);
227 return length < 0 ? -1 : 0;
230 msg = std::string(m_buf, length);
233 char hostname[NI_MAXHOST];
234 char servInfo[NI_MAXSERV];
235 DWORD dwRetval = getnameinfo((
struct sockaddr *)&m_clientAddress,
sizeof(
struct sockaddr), hostname, NI_MAXHOST,
236 servInfo, NI_MAXSERV, NI_NUMERICSERV);
238 std::string hostName =
"", hostIp =
"", hostPort =
"";
240 std::cerr <<
"getnameinfo failed with error: " << WSAGetLastError() << std::endl;
246 char result[INET_ADDRSTRLEN];
247 const char *ptr = inet_ntop(AF_INET, (
void *)&m_clientAddress.sin_addr, result,
sizeof(result));
249 std::cerr <<
"inet_ntop failed with error: " << WSAGetLastError() << std::endl;
254 std::stringstream ss;
255 ss << hostName <<
" " << hostIp <<
" " << hostPort;
277 if (msg.size() > VP_MAX_UDP_PAYLOAD) {
278 std::cerr <<
"Message is too long!" << std::endl;
283 memset(&m_clientAddress, 0,
sizeof(m_clientAddress));
284 std::stringstream ss;
286 struct addrinfo hints;
287 struct addrinfo *result = NULL;
288 struct addrinfo *ptr = NULL;
290 memset(&hints, 0,
sizeof(hints));
291 hints.ai_family = AF_INET;
292 hints.ai_socktype = SOCK_DGRAM;
293 hints.ai_protocol = IPPROTO_UDP;
295 DWORD dwRetval = getaddrinfo(hostname.c_str(), ss.str().c_str(), &hints, &result);
298 ss <<
"getaddrinfo failed with error: " << dwRetval;
302 for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
303 if (ptr->ai_family == AF_INET && ptr->ai_socktype == SOCK_DGRAM) {
304 m_clientAddress = *(
struct sockaddr_in *)ptr->ai_addr;
309 freeaddrinfo(result);
312 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 313 return sendto(m_socketFileDescriptor, msg.c_str(), msg.size(), 0, (
struct sockaddr *)&m_clientAddress,
316 return sendto(m_socketFileDescriptor, msg.c_str(), (int)msg.size(), 0, (
struct sockaddr *)&m_clientAddress,
error that can be emited by ViSP classes.
vpUDPServer(const int port)
int send(const std::string &msg, const std::string &hostname, const int port)
int receive(std::string &msg, const int timeoutMs=0)