forked from xxyzz/ostep-hw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDP-client.c
35 lines (29 loc) · 813 Bytes
/
UDP-client.c
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
#include "UDP-lib.h"
int
main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr, "Usage: %s host\n", argv[0]);
exit(EXIT_FAILURE);
}
int sfd = UDP_Open(argv[1], "10000", 0);
ssize_t nread;
char buf[BUFFER_SIZE] = "first hello";
for(size_t i = 0; i < 2; i++) {
printf("Send: %s\n", buf);
if (UDP_Write(sfd, buf, strlen(buf), NULL, 0) != strlen(buf)) {
fprintf(stderr, "partial/failed write\n");
exit(EXIT_FAILURE);
}
nread = UDP_Read(sfd, buf, NULL, 0);
if (nread == -1) {
perror("read");
exit(EXIT_FAILURE);
}
printf("Received %zd bytes: %s\n", nread, buf);
strncpy(buf, "hello", 6);
}
close(sfd);
closeSem();
exit(EXIT_SUCCESS);
}