Network Communications libevent learning

Server-side
to achieve network communications, and will definitely use the socket and other functions, these functions should be no problem. libevent default is single-threaded, may be configured in a plurality of threads, each thread has a event_base, corresponding to a struct event _base structure and an event manager, a series of events hosted dispatch to it. So when an event occurs, create a event_base, and then create an event, this event will bind, and then added to event_base, start the cycle event_base start processing events. Probably process is as follows:

A parameter analysis;
2. Create a socket connection;
3. * Base = struct event_base event_base_new (); create a event_base;
event_base_new () function allocates and returns a new event_base with default settings. Detecting the environment variable function returns a pointer to the event_base. If an error occurs, NULL is returned. When you select a variety of methods, functions will choose the fastest way to OS support.
4struct event * listen_event; create a listening event;
5event_new (Base, listenfd, EV_READ | EV_PERSIST, accept_cb, Base); use event_new () function will monitor the event binding;
Parameters: event_base monitor objects need to monitor events, events after the callback function passed to the callback function parameters. libevent supported events and properties include (achieved using a bitfield)
EV_TIMEOUT: Timeout;
EV_READ: As long as there are network buffering data, the callback function will be triggered;
EV_WRITE: as long as the data is written upon their network buffer, the callback function will triggered;
ev_signal: the POSIX semaphore;
EV_PERSIST: this property is not specified, the callback function is triggered after the event will be deleted;
EV_ET: the trigger edge-edge-triggered
6event_add (listen_event, NULL); will listen to event_base the event binding;
7event_base_dispatch (base); start the cycle begins processing events;
Typedef 8 callback function when an event occurs void (* event_callback_fn) (evutil_socket_t sockfd , short event_type, void * arg)
passed callback_func is a listening event type fd, and event_new in the last parameter.
Here is the code:

int main(int argc,char **argv)
{
int listenfd;
int ch;
int port;

struct option opt[]={
{"port",required_argument,NULL,'p'},
{"help",no_argument,NULL,'h'},
{NULL,0,NULL,0}
};

while( (ch=getopt_long(argc,argv,"p:h",opt,NULL))!=-1 )
{
switch(ch)
{
case 'p':
port=atoi(optarg);
break;
case 'h':
print_help(argv[0]);
return 0;
}
}
printf("port:%d\n",port);

if( !port )
{
print_help(argv[0]);
return 0;
}


listenfd=socket_init(NULL,port);

if( listenfd<0 )
{
printf("socket_init failure!\n");
return -1;
}
printf("socket_init successfully!\n");

/ * * Create a event_base /
struct event_base * Base = event_base_new ();
the Assert (Base = NULL!); // set event_base not null

/ * create and bind a * Event /
struct Event * listen_event;

listen_event=event_new(base,listenfd,EV_READ | EV_PERSIST,accept_cb,base);

/ * Add an event listener * /
event_add (listen_event, NULL);

/ * Start Cycle to begin processing events * /
event_base_dispatch (Base);

return 0;
}

/*回调函数accept_cb*/
void accept_cb(int fd, short events, void* arg)
{
struct sockaddr_in cliaddr;
evutil_socket_t clifd;
socklen_t len=sizeof(cliaddr);

clifd=accept(fd,(struct sockaddr*)&cliaddr,&len);
if( clifd<0 )
{
printf("accept client %d failure!\n",clifd);
close(clifd);
}

evutil_make_socket_nonblocking (clifd); // set the non-blocking mode
the printf ( "% D Accept Client successfully \ n-!", clifd);
struct event_base * Base = (struct event_base *) Arg;
/ * create a dynamic event structure, and as will be passed to the callback parameter * /
struct * Event cli_event = event_new (NULL, -1, 0, NULL, NULL);
event_assign (cli_event, Base, clifd, EV_READ | EV_PERSIST, read_cb, (void *) cli_event);

event_add(cli_event,NULL);
}

/ * accept * callback read_cb /
void read_cb (int FD, Short Events, void * Arg)
{
int RV;
char buf [1024];
struct * Event EV = (struct Event *) Arg;

rv=read(fd,buf,sizeof(buf));
if( rv<=0 )
{
printf("read message failure!\n");
event_free(ev);
close(fd);
return ;
}

printf("read message successfully:%s\n",buf);

char reply_buf[1024] = "I have received the msg: ";
strcat(reply_buf + strlen(reply_buf), buf);
write(fd,reply_buf,sizeof(reply_buf));
}
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
client
the client with the server side substantially similar, to achieve the socket connector, and then create event_base, create and bind listening to events, an event listener is added, start the cycle begins processing events. code show as below:

int main(int argc, char** argv)
{
int ch;
int sockfd;
char *server_ip=NULL;
int server_port=0;

struct option opt[]={
{"server_ip",required_argument,NULL,'i'},
{"server_port",required_argument,NULL,'p'},
{"help",no_argument,NULL,'h'},
{NULL,0,NULL,0}
};


while( (ch=getopt_long(argc,argv,"i:p:h",opt,NULL))!=-1 )
{
switch(ch)
{
case 'i':
server_ip=optarg;
break;
case 'p':
server_port=atoi(optarg);
break;
case 'h':
print_help(argv[0]);
return 0;
}
}

if(!server_port)
{
print_help(argv[0]);
return 0;
}

sockfd=socket_connect(server_ip,server_port);
if( sockfd<0 )
{
printf("connect to server failure!\n");
return -1;
}
printf("connect to server successfully!\n");
struct event_base* base = event_base_new();

struct event *sockevent = event_new(base, sockfd, EV_READ | EV_PERSIST, read_cb, NULL);
event_add(sockevent, NULL);

// Listen terminal input event
struct event * ev_input = event_new (base , STDIN_FILENO, EV_READ | EV_PERSIST, input_cb, (void *) & sockfd);


event_add(ev_input, NULL);

event_base_dispatch(base);
printf("finished \n");
return 0;
}
/*读回调函数*/
void read_cb(int fd, short events, void *arg)
{
char buf[1024];
int rv;

rv=read(fd,buf,sizeof(buf));
if( rv<=0 )
{
printf("read data from server %dfailure!\n",fd);
exit(1);
}

printf("read %d data from server:%s\n",rv,buf);

}
/ * Input information callback function * /
void input_cb (int FD, Short Events, void * Arg)
{
char buf [1024];
int RV;

Read = RV (FD, buf, the sizeof (buf));
IF (RV <= 0)
{
the printf ( "Read failure \ n-!");
Exit (. 1);
}
// send the message to the terminal server
int sockfd = * ((int *) Arg);
Write (sockfd, buf, RV);
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
41 is
42 is
43 is
44 is
45
46 is
47
48
49
50
51 is
52 is
53 is
54 is
55
56 is
57 is
58
59
60
61 is
62 is
63 is
64
65
66
67
68
69
70
71 is
72
73 is
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
The following are the results:
client:

Service-Terminal:

--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/10935231.html