-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathUDPDaytimeClientUseGetAddrInfo.c
75 lines (62 loc) · 1.91 KB
/
UDPDaytimeClientUseGetAddrInfo.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include "../../tool/AddrTool.h"
int main(int argc, char const *argv[])
{
if (argc != 3) {
printf("usage : %s <ip/hostname> <port/service>\n", argv[0]);
exit(1);
}
setbuf(stdout, NULL);
const char *hostname = argv[1];
const char *service = argv[2];
struct addrinfo hints, *res = NULL, *ressave = NULL;
bzero(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
int error = 0;
if ((error = getaddrinfo(hostname, service, &hints, &res)) != 0) {
printf("getaddrinfo error! %s %s : %s\n", hostname, service, gai_strerror(error));
exit(1);
}
ressave = res;
int fd = -1;
do {
if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
perror("socket fd create fail!");
continue;
}
break;
} while ((res = res->ai_next) != NULL);
if (sendto(fd, "", 1, 0, res->ai_addr, res->ai_addrlen) < 0) {
perror("sendto error!");
exit(1);
}
struct sockaddr_in svraddr;
bzero(&svraddr, sizeof(svraddr));
socklen_t svrlen = sizeof(svraddr);
char buf[1024];
ssize_t nrecv = 0;
if ((nrecv = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&svraddr, &svrlen)) < 0) {
perror("recvfrom error!");
exit(1);
}
char *addrInfo = getAddrInfo(&svraddr);
struct sockaddr_in *ptr = (struct sockaddr_in *)res->ai_addr;
if (svrlen != res->ai_addrlen ||
svraddr.sin_port != ptr->sin_port ||
svraddr.sin_addr.s_addr != ptr->sin_addr.s_addr) {
printf("recvfrom %zd bytes from %s (ignore)\n", nrecv, addrInfo);
free(addrInfo);
exit(0);
}
buf[nrecv] = 0;
printf("%s\n", buf);
return 0;
}