-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_non_block_apply2_server.cpp
69 lines (52 loc) · 1.34 KB
/
tcp_non_block_apply2_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
#include <cstdio>
#include <winsock.h>
int main() {
SOCKET sd, clnt_sd1, clnt_sd2;
WSADATA wsadata;
struct sockaddr_in serv{}, clnt1{}, clnt2{};
int i, n;
char str[100];
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);
int clnt_len1 = sizeof(clnt1);
int clnt_len2 = sizeof(clnt2);
printf("Server waits.\n");
clnt_sd1 = accept(sd, (struct sockaddr *) &clnt1, &clnt_len1);
printf("One client connects.\n");
clnt_sd2 = accept(sd, (struct sockaddr *) &clnt2, &clnt_len2);
printf("Two clients connect.\n");
//enable non-blocking mode when iMode=1
u_long iMode = 1;
ioctlsocket(clnt_sd1, FIONBIO, &iMode);
ioctlsocket(clnt_sd2, FIONBIO, &iMode);
while (true) {
n = recv(clnt_sd1, str, 100, 0);
if (n > 0) {
str[n] = '\0';
printf("(1): %s\n", str);
send(clnt_sd2, str, strlen(str) + 1, 0);
}
if (strcmp(str, "exit") == 0) {
break;
}
n = recv(clnt_sd2, str, 100, 0);
if (n > 0) {
str[n] = '\0';
printf("(2): %s\n", str);
send(clnt_sd1, str, strlen(str) + 1, 0);
}
if (strcmp(str, "exit") == 0) {
break;
}
Sleep(100);
}
closesocket(sd);
closesocket(clnt_sd1);
closesocket(clnt_sd2);
WSACleanup();
}