forked from sszllzss/webSocket_Libevent_ThreadPool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_main.c
82 lines (78 loc) · 2.46 KB
/
client_main.c
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
/*************************************************************************
* # > File Name: client_main.c
* # > Author: SSZL
* # > Mail: [email protected]
* # > Blog: sszlbg.cn
* # > Created Time: 2018-9-01 14:52:11
* # > Revise Time: 2018-10-01 15:32:14
* ************************************************************************/
#include "include/websocket_common.h"
char ip[] = "127.0.0.1";//"192.168.244.128"; // 本机IP
int port = 9999;
int main(void)
{
int ret, timeCount = 0;
int fd;
char buf[1024];
//
fd = webSocket_clientLinkToServer(ip, port,(char *) "/null");
if(fd <= 0)
{
printf("client link to server failed !\r\n");
return -1;
}
//
sleep(1);
//
ret = webSocket_send(fd, (unsigned char *)"Hello !", strlen("Hello !"), true, WCT_TXTDATA);
//
printf("\r\n\r\n========== client start ! ==========\r\n\r\n");
//
while(1)
{
memset(buf, 0, sizeof(buf));
ret = webSocket_recv(fd, (unsigned char *)buf, sizeof(buf));
if(ret > 0)
{
printf("client recv : len/%d %s\r\n", ret, buf);
if(strstr(buf, "Hello") != NULL)
ret = webSocket_send(fd, (unsigned char *)"I am Client_Test", strlen("I am Client_Test"), true, WCT_TXTDATA);
else if(strstr(buf, "Server_Test") != NULL)
ret = webSocket_send(fd, (unsigned char *)"I am carefree !", strlen("I am carefree !"), true, WCT_TXTDATA);
else
;
//
if(ret <= 0) // send返回负数, 连接已断开
{
close(fd);
break;
}
}
else // 检查错误, 是否连接已断开
{
if(errno == EAGAIN || errno == EINTR)
;
else
{
close(fd);
break;
}
}
//
delayms(10);
timeCount += 10;
//
if(timeCount >= 4000) ///////////////////////////////////////////////////////////////////////////// 每4s 客户端可以在这里定时骚扰一下服务器
{
timeCount = 0;
ret = webSocket_send(fd, (unsigned char *)"#%^#@@@DTG%^&&+_)+(*^%!HHI", strlen("#%^#@@@DTG%^&&+_)+(*^%!HHI"), true, WCT_TXTDATA);
if(ret <= 0)
{
close(fd);
break;
}
}
}
printf("client close !\r\n");
return 0;
}