-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
129 lines (118 loc) · 3.3 KB
/
test.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
127
128
129
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "xevent.h"
#include <sys/types.h>
#include <stdio.h>
// service of server
int callback_service(int fd, int filter){
LOG_I("serv-fd=%d, filter=%d(%s)", fd, filter, xfilterdesc(filter));
if (filter == xfilter_read) {
char buff[256] = { 0 };
if (recv(fd, buff, sizeof(buff) - 1, 0) > 0) {
LOG_I("Server Recv: %s", buff);
}
char reply[] = "hi from server";
if (send(fd, reply, sizeof(reply), 0) == sizeof(reply)){
LOG_I("Server Send: %s", reply);
}
}
else if (filter == xfilter_error){
LOG_E("receive fd error, filter %d", filter);
unregxevent(fd);
}
return 0;
}
// accept new connect and add reg connect event proc
int callback_accept(int srvfd, int filter) {
if (filter == xfilter_read){
int clifd = -1;
struct sockaddr_in cli;
unsigned int clilen = sizeof(cli);
if ((clifd = accept(srvfd, (sockaddr*)&cli, &clilen)) < 0){
LOG_E("accept error[%d], ignored!", errno);
return -1;
}
regxevent(clifd, xfilter_read, callback_service);
}
else if (filter == xfilter_error){
LOG_E("receive srvfd error, filter %d", filter);
unregxevent(srvfd);
}
return 0;
}
// client send message to server and receive reply
int callback_client(int fd, int filter){
LOG_I("client-fd=%d, filter=%d(%s)", fd, filter, xfilterdesc(filter));
if (filter == xfilter_read) {
char buff[256] = { 0 };
if (recv(fd, buff, sizeof(buff) - 1, 0) > 0) {
LOG_I("Client Recv: %s", buff);
}
sleep(2);
char reply[] = "hi from client";
if (send(fd, reply, sizeof(reply), 0) == sizeof(reply)){
LOG_I("Client Send: %s", reply);
}
}
else if (filter == xfilter_write) {
char buff[] = "first hi message from client";
if (send(fd, buff, sizeof(buff), 0) == sizeof(buff)) {
LOG_I("Client Send: %s", buff);
}
unregxevent(fd, xfilter_write);
}
else{
LOG_E("receive fd error, filter %d", filter);
unregxevent(fd);
}
return 0;
}
int main(int argc, char** argv)
{
// server and client socket
struct sockaddr_in serv = { 0 }, cli = {0};
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = htonl(INADDR_ANY);
serv.sin_port = htons(18080);
cli.sin_family= AF_INET;
cli.sin_addr.s_addr = inet_addr("127.0.0.1");
cli.sin_port = htons(18080);
// start server listen
int fd = -1, clifd = -1;
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
LOG_E("init socket failed!");
return -1;
}
if (bind(fd, (struct sockaddr*)&serv, sizeof(serv)) == -1) {
LOG_E("bind server failed!");
return -1;
}
if (listen(fd, 511) == -1) {
LOG_E("listen failed!");
return -1;
}
printf("listen port 18080\n");
// register server proc
initxevent();
regxevent(fd, xfilter_read, callback_accept);
// start client connect to server
clifd = socket(PF_INET, SOCK_STREAM, 0);
if (connect(clifd, (sockaddr*)(&cli), sizeof(cli)) == -1){
LOG_E("connect failed!");
return -1;
}
// register client proc
regxevent(clifd, xfilter_read, callback_client);
regxevent(clifd, xfilter_write, callback_client);
regxevent(clifd, xfilter_error, callback_client);
// dispatch message
while (1) {
dispatchxevent(3);
if (xeventnum() == 0) {
LOG_R("finish dispath");
break;
}
}
return 0;
}