55 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66 */
77
8+ #ifdef _WIN32
9+ #define _WINSOCK_DEPRECATED_NO_WARNINGS
10+ #include <winsock2.h>
11+ typedef int socklen_t ;
12+ typedef SSIZE_T ssize_t ;
13+ #else
814#include <arpa/inet.h>
15+ #include <sys/socket.h>
16+ #include <unistd.h>
17+ #endif
18+
919#include <stdio.h>
1020#include <stdlib.h>
1121#include <string.h>
12- #include <sys/socket.h>
13- #include <unistd.h>
1422
1523#include <umf/ipc.h>
1624#include <umf/memory_pool.h>
2230#define SIZE_SHM 1024
2331
2432int producer_connect_to_consumer (int port ) {
25- struct sockaddr_in consumer_addr ;
33+ #ifdef _WIN32
34+ WSADATA wsaData ;
35+ SOCKET producer_socket ;
36+ #else
2637 int producer_socket = -1 ;
38+ #endif
39+
40+ struct sockaddr_in consumer_addr ;
41+
42+ #ifdef _WIN32
43+ // initialize Winsock
44+ if (WSAStartup (MAKEWORD (2 , 2 ), & wsaData ) != 0 ) {
45+ fprintf (stderr , "Failed. Error Code : %d" , WSAGetLastError ());
46+ return -1 ;
47+ }
48+ #endif
2749
2850 // create a producer socket
2951 producer_socket = socket (AF_INET , SOCK_STREAM , 0 );
3052 if (producer_socket < 0 ) {
3153 fprintf (stderr , "[producer] ERROR: Unable to create socket\n" );
32- return -1 ;
54+ goto err_WSA_cleanup ;
3355 }
3456
3557 fprintf (stderr , "[producer] Socket created\n" );
3658
3759 // set IP address and port the same as for the consumer
3860 consumer_addr .sin_family = AF_INET ;
39- consumer_addr .sin_port = htons (port );
61+ consumer_addr .sin_port = htons (( u_short ) port );
4062 consumer_addr .sin_addr .s_addr = inet_addr (INET_ADDR );
4163
4264 // send connection request to the consumer
@@ -49,10 +71,19 @@ int producer_connect_to_consumer(int port) {
4971
5072 fprintf (stderr , "[producer] Connected to the consumer\n" );
5173
52- return producer_socket ; // success
74+ return ( int ) producer_socket ; // success
5375
5476err_close_producer_socket_connect :
77+ #ifdef _WIN32
78+ closesocket (producer_socket );
79+ #else
5580 close (producer_socket );
81+ #endif
82+
83+ err_WSA_cleanup :
84+ #ifdef _WIN32
85+ WSACleanup ();
86+ #endif
5687
5788 return -1 ;
5889}
@@ -131,8 +162,8 @@ int main(int argc, char *argv[]) {
131162 }
132163
133164 // send a size of the IPC_handle to the consumer
134- ssize_t len =
135- send ( producer_socket , & IPC_handle_size , sizeof (IPC_handle_size ), 0 );
165+ ssize_t len = ( ssize_t ) send ( producer_socket , ( const char * ) & IPC_handle_size ,
166+ sizeof (IPC_handle_size ), 0 );
136167 if (len < 0 ) {
137168 fprintf (stderr , "[producer] ERROR: unable to send the message\n" );
138169 goto err_close_producer_socket ;
@@ -147,7 +178,7 @@ int main(int argc, char *argv[]) {
147178 memset (recv_buffer , 0 , sizeof (recv_buffer ));
148179
149180 // receive the consumer's confirmation
150- len = recv (producer_socket , recv_buffer , sizeof (recv_buffer ), 0 );
181+ len = ( ssize_t ) recv (producer_socket , recv_buffer , sizeof (recv_buffer ), 0 );
151182 if (len < 0 ) {
152183 fprintf (stderr , "[producer] ERROR: error while receiving the "
153184 "confirmation from the consumer\n" );
@@ -169,7 +200,8 @@ int main(int argc, char *argv[]) {
169200 }
170201
171202 // send the IPC_handle of IPC_handle_size to the consumer
172- len = send (producer_socket , IPC_handle , IPC_handle_size , 0 );
203+ len = (ssize_t )send (producer_socket , (const char * )IPC_handle ,
204+ (int )IPC_handle_size , 0 );
173205 if (len < 0 ) {
174206 fprintf (stderr , "[producer] ERROR: unable to send the message\n" );
175207 goto err_close_producer_socket ;
@@ -221,7 +253,11 @@ int main(int argc, char *argv[]) {
221253 }
222254
223255err_close_producer_socket :
256+ #ifdef _WIN32
257+ closesocket (producer_socket );
258+ #else
224259 close (producer_socket );
260+ #endif
225261
226262err_PutIPCHandle :
227263 umf_result = umfPutIPCHandle (IPC_handle );
0 commit comments