34 #include "DTrackNet.hpp" 41 #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) 42 #define OS_WIN // for MS Windows (2000, XP, Vista, 7, 8, 10) 44 #define OS_UNIX // for Unix (Linux, Irix) 50 #include <sys/socket.h> 52 #include <netinet/in.h> 53 #include <arpa/inet.h> 85 vreq = MAKEWORD(2, 0);
86 if (WSAStartup(vreq, &wsa) != 0)
108 unsigned int ip_name2ip(
const char* name )
111 struct addrinfo hints, *res;
113 memset( &hints, 0,
sizeof( hints ) );
114 hints.ai_family = AF_INET;
115 hints.ai_socktype = SOCK_STREAM;
118 err = getaddrinfo( name, NULL, &hints, &res );
119 if ( err != 0 || res == NULL )
123 struct sockaddr_in sin;
124 memcpy( &sin, res->ai_addr,
sizeof( sin ) );
125 ip = ntohl( ( (
struct in_addr )( sin.sin_addr ) ).s_addr );
139 UDP::UDP(
unsigned short port,
unsigned int multicastIp )
140 : m_isValid( false ), m_socket( NULL ), m_port( port ), m_multicastIp( 0 ), m_remoteIp( 0 )
143 struct sockaddr_in addr;
155 s->ossock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
163 s->ossock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
164 if (s->ossock == INVALID_SOCKET)
172 if ( multicastIp != 0 )
176 if ( setsockopt( m_socket->ossock, SOL_SOCKET, SO_REUSEADDR, (
char* )&flag_on,
sizeof( flag_on ) ) < 0 )
178 perror(
"setsockopt() failed1");
184 addr.sin_family = AF_INET;
185 addr.sin_port = htons( m_port );
186 addr.sin_addr.s_addr = htonl(INADDR_ANY);
187 addrlen =
sizeof(addr);
188 if ( bind( m_socket->ossock, (
struct sockaddr *)&addr, addrlen ) < 0 )
194 if ( getsockname( m_socket->ossock, (
struct sockaddr *)&addr, &addrlen ) )
197 m_port = ntohs( addr.sin_port );
200 if ( multicastIp != 0 )
203 struct ip_mreq ipmreq;
204 ipmreq.imr_multiaddr.s_addr = htonl( multicastIp );
205 ipmreq.imr_interface.s_addr = htonl(INADDR_ANY);
207 if ( setsockopt( m_socket->ossock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (
char* )&ipmreq,
sizeof( ipmreq ) ) < 0 )
209 perror(
"setsockopt() failed2");
212 m_multicastIp = multicastIp;
224 if ( m_socket == NULL )
return;
226 if ( m_multicastIp != 0 )
228 struct ip_mreq ipmreq;
229 ipmreq.imr_multiaddr.s_addr = htonl( m_multicastIp );
230 ipmreq.imr_interface.s_addr = htonl(INADDR_ANY);
232 if ( setsockopt( m_socket->ossock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (
char* )&ipmreq,
sizeof( ipmreq ) ) < 0 )
234 perror(
"setsockopt() failed3");
239 close( m_socket->ossock );
242 closesocket( m_socket->ossock );
286 FD_SET( m_socket->ossock, &
set );
287 tout.tv_sec = toutUs / 1000000;
288 tout.tv_usec = toutUs % 1000000;
290 err = select( FD_SETSIZE, &
set, NULL, NULL, &tout );
304 struct sockaddr_in addr;
311 addrlen =
sizeof(
struct sockaddr_in );
313 int nbytes =
static_cast< int >( recvfrom( m_socket->ossock, (
char* )buffer, maxLen, 0,
314 (
struct sockaddr* )&addr, &addrlen ) );
320 if ( addr.sin_family == AF_INET )
322 m_remoteIp = ntohl( addr.sin_addr.s_addr );
327 FD_SET( m_socket->ossock, &
set );
331 if (select(FD_SETSIZE, &
set, NULL, NULL, &tout) != 1)
334 if ( nbytes >= maxLen )
347 int UDP::send(
const void* buffer,
int len,
unsigned int ip,
unsigned short port,
int toutUs )
352 struct sockaddr_in addr;
355 addr.sin_family = AF_INET;
356 addr.sin_addr.s_addr = htonl( ip );
357 addr.sin_port = htons(port);
361 FD_SET( m_socket->ossock, &
set );
362 tout.tv_sec = toutUs / 1000000;
363 tout.tv_usec = toutUs % 1000000;
365 err = select( FD_SETSIZE, NULL, &
set, NULL, &tout );
377 int nbytes =
static_cast< int >( sendto( m_socket->ossock, (
const char* )buffer, len, 0, (
struct sockaddr* )&addr,
378 (
size_t )
sizeof(
struct sockaddr_in ) ) );
395 : m_isValid( false ), m_socket( NULL )
398 struct sockaddr_in addr;
404 s->ossock = socket(PF_INET, SOCK_STREAM, 0);
412 s->ossock = socket(PF_INET, SOCK_STREAM, 0);
413 if (s->ossock == INVALID_SOCKET)
422 addr.sin_family = AF_INET;
423 addr.sin_addr.s_addr = htonl(ip);
424 addr.sin_port = htons(port);
425 if ( connect( m_socket->ossock, (
struct sockaddr *)&addr, (
size_t )
sizeof( addr ) ) != 0 )
437 if ( m_socket == NULL )
return;
440 close( m_socket->ossock );
443 closesocket( m_socket->ossock );
469 FD_SET( m_socket->ossock, &
set );
470 tout.tv_sec = toutUs / 1000000;
471 tout.tv_usec = toutUs % 1000000;
473 err = select( FD_SETSIZE, &
set, NULL, NULL, &tout );
485 int nbytes =
static_cast< int >( recv( m_socket->ossock, (
char *)buffer, maxLen, 0 ) );
494 if (nbytes >= maxLen)
505 int TCP::send(
const void* buffer,
int len,
int toutUs )
513 FD_SET( m_socket->ossock, &
set );
514 tout.tv_sec = toutUs / 1000000;
515 tout.tv_usec = toutUs % 1000000;
517 err = select( FD_SETSIZE, NULL, &
set, NULL, &tout );
529 int nbytes =
static_cast< int >( sendto( m_socket->ossock, (
const char* )buffer, len, 0, NULL, 0 ) );
int send(const void *buffer, int len, unsigned int ip, unsigned short port, int toutUs)
Send UDP data.
unsigned short getPort()
Get UDP data port where data is received.
~TCP()
Deinitialize TCP socket.
int receive(void *buffer, int maxLen, int toutUs)
Receive TCP data.
int receive(void *buffer, int maxLen, int toutUs)
Receive UDP data.
unsigned int getRemoteIp()
Get IP address of sender of latest received data.
TCP(unsigned int ip, unsigned short port)
Initialize client TCP socket.
bool isValid()
Returns if UDP socket is open to receive data.
int send(const void *buffer, int len, int toutUs)
Send TCP data.
bool isValid()
Returns if TCP connection is active.
UDP(unsigned short port, unsigned int multicastIp=0)
Initialize UDP socket.
~UDP()
Deinitialize UDP socket.