forked from rqycpp/socket
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcli.c
48 lines (41 loc) · 1.11 KB
/
tcli.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
//客户端程序:监听指定端口,收到服务器广播的时间数据报后打印
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFLEN 255
int main(int argc, char const *argv[]) {
struct sockaddr_in localaddr;
int sockfd, n;
char msg[BUFLEN + 1];
if(argc != 2){
printf("Usage:%s<port>\n", argv[0]);
exit(0);
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0){
fprintf(stderr, "socket creating error in tcli.c\n");
exit(1);
}
memset(&localaddr, 0, sizeof(struct sockaddr_in));
localaddr.sin_port = htons(atoi(argv[1]));
localaddr.sin_addr.s_addr = htonl(INADDR_ANY);
int opt = SO_REUSEADDR;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if(bind(sockfd, (struct sockaddr *)&localaddr, sizeof(struct sockaddr_in)) < 0){
fprintf(stderr, "bind error in tcli.c\n");
exit(2);
}
n = read(sockfd, msg, BUFLEN);
if(n == -1){
fprintf(stderr, "read error in tcli.c\n");
exit(3);
}
else{
msg[n] = '\0';
printf("%s\n", msg);
}
return 0;
}