-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho.c
110 lines (89 loc) · 2.52 KB
/
echo.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
/*
* g++ -g -o echo echo.c -levent
*/
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
//#include <event2/event.h>
#include <event.h>
void accept_cb(int fd, short events, void *arg);
void read_cb(int fd, short events, void *arg);
void write_cb(int fd, short events, void *arg);
void accept_cb(int fd, short events, void *arg)
{
struct event *ev = (struct event*)arg;
struct sockaddr_in cli_addr;
memset(&cli_addr, 0, sizeof(cli_addr));
socklen_t cli_addr_len = sizeof(cli_addr);
int client = accept(fd, (struct sockaddr*)&cli_addr, &cli_addr_len);
printf("accept new connection [%s:%d]\n", inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
struct event *new_ev = (struct event*)malloc(sizeof(struct event));
event_assign(new_ev, ev->ev_base, client, EV_READ | EV_PERSIST, read_cb, new_ev);
event_add(new_ev, NULL);
}
void read_cb(int fd, short events, void *arg)
{
struct event *ev = (struct event*)arg;
char buf[1024];
int iLen = recv(fd, buf, 1024, 0);
if (iLen == 0)
{
printf("peer shutdown, close it\n");
event_del(ev);
free(ev);
close(fd);
}
else if (iLen == -1)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
printf("No data to read\n");
}
printf("Unknow error occur, errno[%d]\n", errno);
event_del(ev);
free(ev);
close(fd);
}
else
{
printf("recv %d bytes data, content[%s]\n", iLen, buf);
event_del(ev);
event_assign(ev, ev->ev_base, fd, EV_WRITE | EV_PERSIST, write_cb, ev);
event_add(ev, NULL);
}
}
void write_cb(int fd, short events, void *arg)
{
struct event *ev = (struct event*)arg;
char buf[] = "recv data succ!\n";
int iLen = send(fd, buf, strlen(buf), 0);
printf("send %d bytes data, contents[%s]\n", iLen, buf);
event_del(ev);
event_assign(ev, ev->ev_base, fd, EV_READ | EV_PERSIST, read_cb, ev);
event_add(ev, NULL);
}
int main(int argc, char **argv)
{
int server = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(8080);
bind(server, (struct sockaddr *)&addr, sizeof(addr));
listen(server, 5);
struct event_base *ev_base = event_base_new();
struct event listen_event;
struct event *ev = (struct event*)malloc(sizeof(struct event));
event_assign(ev, ev_base, server, EV_READ | EV_PERSIST, accept_cb, ev);
event_add(ev, NULL);
event_base_dispatch(ev_base);
delete ev;
event_base_free(ev_base);
return 0;
}