-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek13_5_server.cpp
139 lines (107 loc) · 3.21 KB
/
week13_5_server.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include<cstdio>
#include<winsock.h>
#define MAXRECV 1024
#define MAXCLI 10
int main() {
WSADATA wsadata;
SOCKET serv_sd, new_socket, cli_sd[MAXCLI];
struct sockaddr_in serv{}, cli{};
int activity, cli_len, n, client_num = 0;
int ready_to_send[MAXCLI];
fd_set readfds, writefds; //set of socket descriptors
char str[MAXRECV];
TIMEVAL timeout{};
for (int i = 0; i < MAXCLI; i++) {
ready_to_send[i] = 0;
cli_sd[i] = 0;
}
WSAStartup(0x101, &wsadata); //呼叫 WSAStartup() 註冊 WinSock DLL 的使用
serv_sd = socket(AF_INET, SOCK_STREAM, 0);// 開啟 TCP socket
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = INADDR_ANY;
serv.sin_port = htons(5678);
bind(serv_sd, (LPSOCKADDR) &serv, sizeof(serv));
listen(serv_sd, 3);
while (true) {
printf("[1]clear the socket fd set. \n");
FD_ZERO(&readfds);
FD_ZERO(&writefds);
printf("[2]add server socket to fd set \n");
FD_SET(serv_sd, &readfds);
printf("[2]add client socket to fd set \n");
for (int i = 0; i < MAXCLI; i++) {
if (cli_sd[i] > 0) {
if (ready_to_send[i] == 0) {
FD_SET(cli_sd[i], &readfds);
} else {
FD_SET(cli_sd[i], &writefds);
}
}
}
// wait for an activity on any of the sockets, timeout is NULL , so wait indefinitely
printf("[3]call select() and waiting \n");
// set timeout
timeout.tv_sec = 5;
timeout.tv_usec = 0;
activity = select(0, &readfds, &writefds, NULL, &timeout);
printf("[4]wake up from select():%d\n", activity);
if (activity == SOCKET_ERROR) {
printf("select call failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
if (activity == 0) {
printf("select() is waiting for 5 sec.\n");
}
// check serv_sd->accept(), add new client into cli_sd[]
if (FD_ISSET(serv_sd, &readfds)) {
cli_len = sizeof(cli);
new_socket = accept(serv_sd, (struct sockaddr *) &cli, (int *) &cli_len);
printf("New connection: socket fd: %d, ip: %s, port: %d\n", new_socket, inet_ntoa(cli.sin_addr), ntohs(cli.sin_port));
for (int i = 0; i < MAXCLI; i++) {
if (cli_sd[i] == 0) {
cli_sd[i] = new_socket;
client_num++;
printf("The %d client socket is in cli_sd[%d]\n", client_num, i);
break;
}
}
}
// check each cli_sd client present in read sockets
for (int i = 0; i < MAXCLI; i++) {
if (FD_ISSET(cli_sd[i], &readfds)) {
n = recv(cli_sd[i], str, MAXRECV, 0);
if (n == SOCKET_ERROR) {
int error_code = WSAGetLastError();
if (error_code == WSAECONNRESET) {
// somebody disconnected, get his details and print
printf("Host disconnected unexpectedly\n");
closesocket(cli_sd[i]);
cli_sd[i] = 0;
client_num--;
} else {
printf("recv failed with error code: %d", error_code);
}
}
if (n == 0) {
// somebody disconnected, get his details and print
printf("Host disconnected\n");
closesocket(cli_sd[i]);
cli_sd[i] = 0;
client_num--;
}
if (n > 0) {
printf("recv from cli[%d]: %s\n", i, str);
ready_to_send[i] = 1;
}
}
if (FD_ISSET(cli_sd[i], &writefds)) {
send(cli_sd[i], str, int(strlen(str) + 1), 0);
ready_to_send[i] = 0;
}
}
}// while
closesocket(serv_sd);
WSACleanup();
system("pause");
return 0;
}