-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_echo_cli.c
More file actions
71 lines (61 loc) · 1.76 KB
/
Copy pathtcp_echo_cli.c
File metadata and controls
71 lines (61 loc) · 1.76 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#define MAX_CMD_STR 100
void echo_rqt(int sockfd) {
char buf[MAX_CMD_STR + 1];
while (fgets(buf, MAX_CMD_STR, stdin)) {
if(strncmp(buf, "exit", 4) == 0){
return;
}
int len_h = strnlen(buf, 100);
int len_n = htonl(len_h);
char* temp = NULL;
if ((temp = strstr(buf, "\n"))) *temp = '\0';
write(sockfd, &len_n, sizeof(len_n));
write(sockfd, buf, len_h);
read(sockfd, &len_n, sizeof(len_n));
int res = 0;
while(res != len_h){
read(sockfd, buf + res, 1);
res ++;
}
printf("[echo_rep] %s\n", buf);
}
}
int main(int argc, char *argv[]){
int connfd;
struct sockaddr_in srv_addr;
if ((connfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Create socket failed.\n");
exit(1);
}
if(argc != 3){
printf("Usage:%s <IP> <PORT>\n", argv[0]);
return 0;
}
bzero(&srv_addr, sizeof(srv_addr));
srv_addr.sin_family = AF_INET;
if(inet_pton(AF_INET, argv[1], &srv_addr.sin_addr) == 0){
perror("Server IP Address Error:\n");
exit(1);
}
srv_addr.sin_port = htons(atoi(argv[2]));
int res = connect(connfd, (struct sockaddr *)&srv_addr, sizeof(struct sockaddr));
if(res == 0){
printf("[cli] server[%s:%s] is connected!\n", argv[1], argv[2]);
echo_rqt(connfd);
}
else if(res == -1){
printf("[cli] connect error! errno is %d\n", errno);
}
close(connfd);
printf("[cli] connfd is closed!\n");
printf("[cli] client is exiting!\n");
return 0;
}