Skip to content

Commit 122e561

Browse files
richardsiveraSTblazraST
authored andcommitted
fixed pc_to_pc example for windows platform
1 parent 2b53713 commit 122e561

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

examples/pc_to_pc/main.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#ifdef _WIN32
55
#include <winsock2.h>
6+
#include <ws2tcpip.h>
67
#else
78
#include <netinet/in.h>
89
#include <sys/socket.h>
@@ -24,9 +25,18 @@ uint32_t add_int_handler(uint32_t a, uint32_t b)
2425
int main(int argc, char *argv[])
2526
{
2627
#ifdef _WIN32
27-
WSADATA wsaData;
28-
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
29-
exit(1);
28+
WSADATA wsaData;
29+
30+
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
31+
fprintf(stderr, "WSAStartup failed.\n");
32+
exit(1);
33+
}
34+
35+
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
36+
fprintf(stderr, "Versiion 2.2 of Winsock is not available.\n");
37+
WSACleanup();
38+
exit(2);
39+
}
3040
#endif
3141

3242
struct sockaddr_in sockaddr;
@@ -46,16 +56,29 @@ int main(int argc, char *argv[])
4656
int connection = accept(sock, (struct sockaddr *)&client_sockaddr_in, &len);
4757

4858
for (;;) {
49-
50-
if (read(connection, rx_buf, 1) < 1)
51-
break;
52-
if (read(connection, rx_buf + 1, 3 + 4 * rx_buf[0]) < 1)
53-
break;
59+
60+
#ifdef _WIN32
61+
if (recv(connection, rx_buf, 1, 0) < 1)
62+
#else
63+
if (read(connection, rx_buf, 1) < 1)
64+
#endif
65+
break;
66+
67+
#ifdef _WIN32
68+
if (recv(connection, rx_buf + 1, 3 + 4 * rx_buf[0], 0) < 1)
69+
#else
70+
if (read(connection, rx_buf + 1, 3 + 4 * rx_buf[0]) < 1)
71+
#endif
72+
break;
5473

5574
int16_t msg_size = compost_msg_process(tx_buf, sizeof(tx_buf), rx_buf, 4 + 4 * rx_buf[0]);
5675

5776
if (msg_size > 0) {
58-
write(connection, tx_buf, msg_size);
77+
#ifdef _WIN32
78+
send(connection, tx_buf, msg_size, 0);
79+
#else
80+
write(connection, tx_buf, msg_size);
81+
#endif
5982
} else if (msg_size == 0) {
6083
// No response to send
6184
} else {
@@ -78,4 +101,4 @@ int main(int argc, char *argv[])
78101
#endif
79102

80103
return 0;
81-
}
104+
}

0 commit comments

Comments
 (0)