-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathGetServerDaytimeClient_IPv6.c
50 lines (43 loc) · 1.09 KB
/
GetServerDaytimeClient_IPv6.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#define VEASON_IP ""
#define VEASON_PORT (20013)
#define MAXLINE 2048
int main(int argc, char const *argv[])
{
int fd = -1;
if ((fd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
perror("socket fd create fail!");
exit(1);
}
struct sockaddr_in6 svraddr;
bzero(&svraddr, sizeof(svraddr));
svraddr.sin6_family = AF_INET6;
svraddr.sin6_port = htons(VEASON_PORT);
if (inet_pton(AF_INET6, VEASON_IP, &svraddr.sin6_addr) < 0) {
perror("ipv6 format error!");
exit(1);
}
if (connect(fd, (struct sockaddr *)&svraddr, sizeof(svraddr)) < 0) {
perror("connect error!");
exit(1);
}
char buf[MAXLINE];
ssize_t nread = 0;
while ((nread = read(fd, buf, MAXLINE)) > 0) {
buf[nread] = 0;
if (fputs(buf, stdout) == EOF) {
fprintf(stderr, "fputs error!");
exit(1);
}
}
if (nread < 0) {
perror("read error!");
exit(1);
}
close(fd);
return 0;
}