1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <assert.h>
#if EMSCRIPTEN
#include <emscripten.h>
#endif
#define EXPECTED_BYTES 5
#define BUFLEN 16
#define HOST_ADDR "10.0.0.1"
int result = 0;
int sock;
char buf[16] = "emscripten";
struct sockaddr_in si_host;
struct iovec iov[1];
struct msghdr hdr;
int done = 0;
void iter() {
int n;
n = sendmsg(sock, &hdr, 0);
if(0 < n) {
done = 1;
fprintf(stderr, "sent %d bytes: %s", n, (char*)hdr.msg_iov[0].iov_base);
shutdown(sock, SHUT_RDWR);
close(sock);
exit(EXIT_SUCCESS);
emscripten_cancel_main_loop();
} else if(EWOULDBLOCK != errno) {
perror("sendmsg failed");
exit(EXIT_FAILURE);
emscripten_cancel_main_loop();
}
}
int main(void)
{
memset(&si_host, 0, sizeof(struct sockaddr_in));
si_host.sin_family = AF_INET;
si_host.sin_port = htons(8991);
if(0 == inet_pton(AF_INET, HOST_ADDR, &si_host.sin_addr)) {
perror("inet_aton failed");
exit(EXIT_FAILURE);
}
if(-1 == (sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP))) {
perror("cannot create socket");
exit(EXIT_FAILURE);
}
iov[0].iov_base = buf;
iov[0].iov_len = sizeof(buf);
memset (&hdr, 0, sizeof (struct msghdr));
hdr.msg_name = &si_host;
hdr.msg_namelen = sizeof(struct sockaddr_in);
hdr.msg_iov = iov;
hdr.msg_iovlen = 1;
#if EMSCRIPTEN
emscripten_set_main_loop(iter, 0, 0);
#else
while (!done) iter();
#endif
return EXIT_SUCCESS;
}
|