forked from sszllzss/webSocket_Libevent_ThreadPool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ev_client_main.c
126 lines (122 loc) · 3.56 KB
/
ev_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
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
/*************************************************************************
# > File Name: ev_client_main.c
# > Author: SSZL
# > Mail: [email protected]
# > Blog: sszlbg.cn
# > Created Time: 2018-09-16 12:08:15
# > Revise Time: 2018-10-01 15:24:33
************************************************************************/
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<signal.h>
#include<event2/util.h>
#include<event2/dns.h>
#include<assert.h>
#include<sys/time.h>
#include"include/websocket_common.h"
#include"include/debug.h"
#define SERVER_PORT 8088
#define SERVERR_IP "lot.sszlsszl.com"
#define INADDR_LEN 50
static void bev_read_cd(struct bufferevent *bev, void *ctx)
{
ctx = (void*)ctx;
char buf[1024];
int ret = ev_webSocket_recv(bev, (unsigned char*)buf, sizeof(buf));
if(ret>0)
{
printf("client recv : len/%d %s\r\n", ret, buf);
if(strstr(buf, "Hello") != NULL)
ret = ev_webSocket_send(bev, (unsigned char *)"I am Client_Test", strlen("I am Client_Test"), true, WCT_TXTDATA);
else if(strstr(buf, "Server_Test") != NULL)
ret = ev_webSocket_send(bev, (unsigned char *)"I am carefree !", strlen("I am carefree !"), true, WCT_TXTDATA);
}
else
{
De_printf("断开连接!\r\n");
event_base_loopbreak(bufferevent_get_base(bev));
bufferevent_free(bev);
}
}
static void bev_event_cd(struct bufferevent *bev,short events, void *ctx)
{
ctx = (void*)ctx;
if(events & BEV_EVENT_EOF)
De_printf("断开连接!\r\n");
else if(events & BEV_EVENT_ERROR)
{
De_printf("Got an error on the connection:%s\r\n", strerror(errno));
}
event_base_loopbreak(bufferevent_get_base(bev));
bufferevent_free(bev);
}
static void sig_cb(evutil_socket_t sig, short events,void *ctx)
{
int ret;
if(sig==SIGALRM)
{
struct bufferevent *bev=(struct bufferevent *)ctx;
ret = ev_webSocket_send(bev,(unsigned char *) "#%^#@@@DTG%^&&+_)+(*^%!HHI", strlen("#%^#@@@DTG%^&&+_)+(*^%!HHI"), true, WCT_TXTDATA);
if(ret < 0)
{
De_printf("断开连接!\r\n");
event_base_loopbreak(bufferevent_get_base(bev));
bufferevent_free(bev);
}
}
}
int main(int argc, char *argv[])
{
char inAddr[INADDR_LEN];
int port = 0;
struct event_base *base=NULL;
struct bufferevent *bev=NULL;
struct event * sig=NULL;
struct itimerval timer_out;
if(argc==3)
{
if(strlen(argv[1])>INADDR_LEN)
{
printf("输入长度要少于%d\r\n", INADDR_LEN);
exit(1);
}
strcpy(inAddr, argv[1]);
port = atoi(argv[2]);
if(port==0)
{
printf("端口输入错误!!\r\n");
exit(1);
}
}
else
{
port = SERVER_PORT;
strcpy(inAddr, SERVERR_IP);
}
base = event_base_new();
assert(base);
bev = ev_webSocket_clientLinkToServer(base, inAddr, port,(char *)"/null",bev_read_cd,NULL,bev_event_cd,NULL);
if(!bev)
{
printf("连接服务器错误!\r\n");
exit(1);
}
sig = evsignal_new(base, SIGALRM, sig_cb, bev);
if(!sig || event_add(sig, NULL))
{
De_fprintf(stderr, "cold not create/add a signal event!");
}
else
{
memset(&timer_out, 0, sizeof(timer_out));
timer_out.it_interval.tv_sec = 2;
timer_out.it_value.tv_sec = 2;
setitimer(ITIMER_REAL, &timer_out, NULL);
}
event_base_dispatch(base);
return 0;
}