-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_non_block_apply2_client.cpp
73 lines (54 loc) · 1.22 KB
/
tcp_non_block_apply2_client.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
// simple TCP chat client(1)
// v2: two client chat through server
#include <cstdio>
#include <ctime>
#include <winsock.h>
int main() {
SOCKET sd;
WSADATA wsadata;
struct sockaddr_in serv{};
int i, n;
char str[100] = "I love NP!";
char str_name[100];
clock_t t1, t2;
int time_interval;
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");
connect(sd, (struct sockaddr *) &serv, sizeof(serv));
printf("Connect to server.\n");
//enable non-blocking mode when iMode=1
u_long iMode = 1;
ioctlsocket(sd, FIONBIO, &iMode);
printf("Client name:");
gets(str_name);
printf("Time Interval(sec):");
scanf("%d", &time_interval);
Sleep(5000);
t2 = 0;
long time;
while (true) {
t1 = clock();
time = (t1 - t2) / CLOCKS_PER_SEC;
if (time > time_interval) {
strcpy(str, str_name);
printf("[Client]: %s\n", str);
send(sd, str, strlen(str) + 1, 0);
t2 = clock();
}
memset(str, 0, 100);
n = recv(sd, str, 100, 0);
if (n > 0) {
str[n] = '\0';
printf("[Other]:%s\n", str);
}
if (strcmp(str, "exit") == 0) {
break;
}
Sleep(100);
}
closesocket(sd);
WSACleanup();
}