In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use libevent to write a simple server", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use libevent to write a simple server" this article.
Sample code
# include # include void sock_read (int fd, short event, void * arg) {char buf [255]; int len; struct event * ev = arg; len = recv (fd, buf, sizeof (buf)-1,0); if (len =-1) {perror ("recv error/n") If (errno! = EAGAIN & & errno! = EINTR) {close (fd); free (ev);} return;} else if (len = = 0) {close (fd); fprintf (stderr, "Connection closed/n"); free (ev); return;} buf [len] ='/ 0' Fprintf (stdout, "Read:% Spura", buf); / * Reschedule this event * / event_add (ev, NULL);} void sock_accept (int fd, short event, void * arg) {struct event * ev = arg; struct sockaddr addr; socklen_t len = sizeof (addr) / / because this structure is to be used for a long time, rev must be allocated dynamically, otherwise it will be automatically released after leaving this function, resulting in segment fault struct event* rev = (struct event*) malloc (sizeof (* rev)); int s = accept (fd, & addr, & len); if (s = =-1) {perror ("accept error/n"); return } fprintf (stdout, "accept socket:% DBO", s); / * Initalize one event * / event_set (rev, s, EV_READ, sock_read, rev); / * Add it to the active events, without a timeout * / event_add (rev, NULL); / * Reschedule this event * / event_add (ev, NULL);} intmain (int argc, char * * argv) {struct event ev; int fd; struct sockaddr_in addr Fd = socket (AF_INET, SOCK_STREAM, 0); if (fd = =-1) {perror ("socket error/n"); exit (- 1);} bzero (& addr, sizeof (addr)); addr.sin_family = AF_INET; addr.sin_port = htons (10000); addr.sin_addr.s_addr = 0 If (bind (fd, (struct sockaddr*) & addr, sizeof (addr)) =-1) {perror ("bind error/n"); exit (- 1);} if (listen (fd, 5) = =-1) {perror ("listen error/n"); exit (- 1);} / * Initalize the event library * / event_init () / * Initalize one event * / event_set (& ev, fd, EV_READ, sock_accept, & ev); / * Add it to the active events, without a timeout * / event_add (& ev, NULL); event_dispatch (); return (0);} above is all the content of the article "how to write a simple server using libevent". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.