-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_non_block_apply3_server.cpp
74 lines (56 loc) · 1.33 KB
/
tcp_non_block_apply3_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
#include <cstdio>
#include <winsock.h>
#define MaxClientNum 5
int main() {
SOCKET sd, clnt_sd[MaxClientNum];
WSADATA wsadata;
struct sockaddr_in serv{}, clnt{};
int n;
int current_client_num;
bool exit = false;
char str[100] = "I love NP!";
WSAStartup(0x101, &wsadata);
sd = socket(AF_INET, SOCK_STREAM, 0);
serv.sin_family = AF_INET;
serv.sin_port = htons(1234);
serv.sin_addr.s_addr = inet_addr("127.0.0.1");
bind(sd, (struct sockaddr *) &serv, sizeof(serv));
listen(sd, 5);
//enable non-blocking mode when iMode=1
u_long iMode = 1;
ioctlsocket(sd, FIONBIO, &iMode);
int clnt_len = sizeof(clnt);
printf("Server waits.\n");
for (int i = 0; i < MaxClientNum; i++) {
clnt_sd[i] = -1;
}
current_client_num = 0;
while (true) {
n = accept(sd, (struct sockaddr *) &clnt, &clnt_len);
if (n != -1) {
clnt_sd[current_client_num] = n;
printf("Client[%d] is connected.\n", current_client_num);
current_client_num++;
}
for (int i = 0; i < MaxClientNum; i++) {
if (clnt_sd[i] != -1) {
send(clnt_sd[i], str, strlen(str) + 1, 0);
printf("Send to clnt_sd[%d]: %s\n", i, str);
if (strcmp(str, "exit") == 0) {
exit = true;
break;
}
}
}
if(exit) {
break;
}
Sleep(1000);
}
closesocket(sd);
for (unsigned i : clnt_sd) {
closesocket(i);
}
WSACleanup();
//system("pause");
}