@@ -27,10 +27,14 @@ THE SOFTWARE.
2727#ifndef UDP_SOCKET_H
2828#define UDP_SOCKET_H
2929
30+ // Platform detection - include appropriate implementation
31+ #ifdef _WIN32
32+
33+ // Windows implementation
3034#include < WinSock2.h>
3135#include < Ws2tcpip.h>
3236
33- #pragma message("Note: including lib: Ws2_32.lib")
37+ #pragma message("Note: including lib: Ws2_32.lib")
3438#pragma comment(lib, "Ws2_32.lib")
3539
3640#include < string>
@@ -49,7 +53,7 @@ class wsa_session
4953 WSACleanup () ;
5054 }
5155
52- private :
56+ private :
5357
5458 WSAData data_ ;
5559} ;
@@ -59,7 +63,7 @@ class udp_socket
5963{
6064 public :
6165
62- udp_socket ( void )
66+ udp_socket ( void )
6367 {
6468 // UDP socket
6569 socket_ = socket ( AF_INET , SOCK_DGRAM , IPPROTO_UDP ) ;
@@ -69,7 +73,7 @@ class udp_socket
6973 ioctlsocket ( socket_ , FIONBIO , &arg ) ;
7074 }
7175
72- ~udp_socket ( void )
76+ ~udp_socket ( void )
7377 {
7478 closesocket ( socket_ ) ;
7579 }
@@ -80,7 +84,7 @@ class udp_socket
8084 add.sin_family = AF_INET ;
8185 InetPton ( AF_INET , address.c_str () , &add.sin_addr .s_addr ) ;
8286 add.sin_port = htons ( port ) ;
83-
87+
8488 if ( sendto ( socket_ , message.c_str () , (int )message.length () , 0 , reinterpret_cast <SOCKADDR *>( &add ) , sizeof ( add ) ) > 0 )
8589 return true ;
8690
@@ -117,52 +121,59 @@ class udp_socket
117121 return false ;
118122 }
119123
120- bool enable_send_message_multicast ( void )
124+ bool enable_send_message_multicast ( void )
121125 {
122126 struct in_addr add ;
123127 add.s_addr = INADDR_ANY ;
124128
125- int result = setsockopt ( socket_ ,
126- IPPROTO_IP ,
127- IP_MULTICAST_IF ,
128- (const char *)&add ,
129+ int result = setsockopt ( socket_ ,
130+ IPPROTO_IP ,
131+ IP_MULTICAST_IF ,
132+ (const char *)&add ,
129133 sizeof ( add ) ) ;
130- return ( result != SOCKET_ERROR ) ;
134+ return ( result != SOCKET_ERROR ) ;
131135 }
132136
133- bool join_multicast_group ( const ::std::string & ip_group )
137+ bool join_multicast_group ( const ::std::string & ip_group )
134138 {
135- struct ip_mreq imr;
136-
139+ struct ip_mreq imr;
140+
137141 InetPton ( AF_INET , ip_group.c_str () , &imr.imr_multiaddr .s_addr ) ;
138142 imr.imr_interface .s_addr = INADDR_ANY ;
139143
140- int result = setsockopt ( socket_ ,
141- IPPROTO_IP ,
142- IP_ADD_MEMBERSHIP ,
143- (char *) &imr ,
144+ int result = setsockopt ( socket_ ,
145+ IPPROTO_IP ,
146+ IP_ADD_MEMBERSHIP ,
147+ (char *) &imr ,
144148 sizeof (struct ip_mreq ) ) ;
145- return ( result != SOCKET_ERROR ) ;
149+ return ( result != SOCKET_ERROR ) ;
146150 }
147151
148- bool leave_multicast_group ( const ::std::string & ip_group )
152+ bool leave_multicast_group ( const ::std::string & ip_group )
149153 {
150154 struct ip_mreq imr;
151-
155+
152156 InetPton ( AF_INET , ip_group.c_str () , &imr.imr_multiaddr .s_addr ) ;
153157 imr.imr_interface .s_addr = INADDR_ANY ;
154158
155- int result = setsockopt ( socket_ ,
156- IPPROTO_IP ,
159+ int result = setsockopt ( socket_ ,
160+ IPPROTO_IP ,
157161 IP_DROP_MEMBERSHIP ,
158- (char *) &imr ,
162+ (char *) &imr ,
159163 sizeof (struct ip_mreq ) ) ;
160- return ( result != SOCKET_ERROR ) ;
164+ return ( result != SOCKET_ERROR ) ;
161165 }
162166
163167 private :
164168
165- SOCKET socket_ ;
169+ SOCKET socket_ ;
166170} ;
167171
172+ #else
173+
174+ // POSIX implementation (macOS, Linux, etc.)
175+ #include " udp_socket_posix.h"
176+
177+ #endif
178+
168179#endif
0 commit comments