-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_echo_multi_proc_client.c
110 lines (97 loc) · 2.29 KB
/
tcp_echo_multi_proc_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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// 回声客户端多进程版本
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <bits/sigaction.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <errno.h>
#include "error_handling.h"
#define BUF_SIZE 30
void read_childproc(int sig);
void read_routine(int sock, char *buf);
void write_routine(int sock, char *buf);
int main(int argc, char *argv[])
{
struct sockaddr_in serv_addr;
struct sigaction act;
int sock;
pid_t pid;
char message[BUF_SIZE];
if (argc != 3)
{
printf("Usage: %s <ip> <port>\n", argv[0]);
exit(1);
}
sigemptyset(&act.sa_mask);
act.sa_handler = read_childproc;
act.sa_flags = 0;
sigaction(SIGCHLD, &act, 0);
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
error_handling("socket() err: %s", strerror(errno));
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(atoi(argv[2]));
socklen_t addr_sz = sizeof(serv_addr);
if (connect(sock, (struct sockaddr *)&serv_addr, addr_sz) == -1)
{
error_handling("connect() err: %s", strerror(errno));
}
pid = fork();
if (pid == 0)
{
write_routine(sock, message);
}
else
{
read_routine(sock, message);
}
close(sock);
return 0;
}
void read_routine(int sock, char *buf)
{
while (1)
{
int read_len = read(sock, buf, BUF_SIZE);
if (read_len == 0)
{
return;
}
buf[read_len] = 0;
printf("Message from server: %s", buf);
}
}
void write_routine(int sock, char *buf)
{
while (1)
{
// fputs("Enter Q or q to exit: ", stdout);
fgets(buf, BUF_SIZE, stdin);
if (!strcmp(buf, "q\n") || !strcmp(buf, "Q\n"))
{
shutdown(sock, SHUT_WR); // 发送EOF
return;
}
write(sock, buf, strlen(buf));
}
}
void read_childproc(int sig)
{
if (sig == SIGCHLD)
{
int status;
pid_t id = waitpid(-1, &status, WNOHANG);
if (WIFEXITED(status))
{
printf("child proc %d exit, send: %d\n", id, WEXITSTATUS(status));
}
}
}