Comment on page
6.1. 簡單的 Stream Server
這個 server 所處理的事情就是透過 stream connection(串流連線)送出 "Hello, World!\n" 字串。你所需要做就是用一個視窗來測試執行 server,並用另一個視窗來 telnet 到 server:
$ telnet remotehostname 3490
這裡的 remotehostname 就是你執行 server 的主機名稱。
Server的程式碼如下 [20]:
1
/*
2
** server.c – 展示一個stream socket server
3
*/
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <unistd.h>
7
#include <errno.h>
8
#include <string.h>
9
#include <sys/types.h>
10
#include <sys/socket.h>
11
#include <netinet/in.h>
12
#include <netdb.h>
13
#include <arpa/inet.h>
14
#include <sys/wait.h>
15
#include <signal.h>
16
#define PORT "3490" // 提供給使用者連線的 port
17
#define BACKLOG 10 // 有多少個特定的連線佇列(pending connections queue)
18
19
void sigchld_handler(int s)
20
{
21
while(waitpid(-1, NULL, WNOHANG) > 0);
22
}
23
24
// 取得sockaddr,IPv4或IPv6:
25
void *get_in_addr(struct sockaddr *sa)
26
{
27
if (sa->sa_family == AF_INET) {
28
return &(((struct sockaddr_in*)sa)->sin_addr);
29
}
30
return &(((struct sockaddr_in6*)sa)->sin6_addr);
31
}
32
33
int main(void)
34
{
35
int sockfd, new_fd; // 在 sock_fd 進行 listen,new_fd 是新的連線
36
struct addrinfo hints, *servinfo, *p;
37
struct sockaddr_storage their_addr; // 連線者的位址資訊
38
socklen_t sin_size;
39
struct sigaction sa;
40
int yes=1;
41
char s[INET6_ADDRSTRLEN];
42
int rv;
43
44
memset(&hints, 0, sizeof hints);
45
hints.ai_family = AF_UNSPEC;
46
hints.ai_socktype = SOCK_STREAM;
47
hints.ai_flags = AI_PASSIVE; // 使用我的 IP
48
49
if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
50
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
51
return 1;
52
}
53
54
// 以迴圈找出全部的結果,並綁定(bind)到第一個能用的結果
55
for(p = servinfo; p != NULL; p = p->ai_next) {
56
if ((sockfd = socket(p->ai_family, p->ai_socktype,
57
p->ai_protocol)) == -1) {
58
perror("server: socket");
59
continue;
60
}
61
62
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
63
sizeof(int)) == -1) {
64
perror("setsockopt");
65
exit(1);
66
}
67
68
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
69
close(sockfd);
70
perror("server: bind");
71
continue;
72
}
73
74
break;
75
}
76
77
if (p == NULL) {
78
fprintf(stderr, "server: failed to bind\n");
79
return 2;
80
}
81
82
freeaddrinfo(servinfo); // 全部都用這個 structure
83
84
if (listen(sockfd, BACKLOG) == -1) {
85
perror("listen");
86
exit(1);
87
}
88
89
sa.sa_handler = sigchld_handler; // 收拾全部死掉的 processes
90
sigemptyset(&sa.sa_mask);
91
sa.sa_flags = SA_RESTART;
92
93
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
94
perror("sigaction");
95
exit(1);
96
}
97
98
printf("server: waiting for connections...\n");
99
100
while(1) { // 主要的 accept() 迴圈
101
sin_size = sizeof their_addr;
102
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
103
if (new_fd == -1) {
104
perror("accept");
105
continue;
106
}
107
108
inet_ntop(their_addr.ss_family,
109
get_in_addr((struct sockaddr *)&their_addr),
110
s, sizeof s);
111
printf("server: got connection from %s\n", s);
112
113
if (!fork()) { // 這個是 child process
114
close(sockfd); // child 不需要 listener
115
116
if (send(new_fd, "Hello, world!", 13, 0) == -1)
117
perror("send");
118
119
close(new_fd);
120
121
exit(0);
122
}
123
124
close(new_fd); // parent 不需要這個
125
}
126
127
return 0;
128
}
趁著你對這個例子還感到很好奇,我為了讓句子比較清楚(我個人覺得),所以將程式碼放在一個大的 main() 函式中,如果你覺得將它分成幾個小一點的函式會比較好的話,可以儘管去做。
「還有,sigaction() 這個東西對你而言應該是蠻陌生的。沒有關 係,這個程式碼是用來清理 zombie process(殭屍行程),當 parent process 所 fork() 出來的 child process 結束時,且 parent process 沒有取得 child process 的離開狀態時,就會出現 zombie process。如果你產生了許多 zombie,但卻無法清除他們時,你的系統管理員就會開始焦慮不安了。」
你可以利用下一節所列出的 client,來取得 server 的資料。
Last modified 1yr ago