欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

        1. <bdo id="vie6q"><code id="vie6q"><cite id="vie6q"></cite></code></bdo>
        2. Windows下實現(xiàn)簡單的libevent服務(wù)器

           更新時間:2016年10月20日 10:37:10   作者:有夢想不彷徨  
          這篇文章主要介紹了Windows下實現(xiàn)簡單的libevent服務(wù)器的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

          最近再學(xué)習(xí)Libevent由于自己使用的是windows系統(tǒng),遺憾的是有關(guān)在vs下可以參考的程序少之又少。在參考了許多的博客文章后。自己摸索寫了一個簡單的Libevent Server程序。并且在網(wǎng)上找了一個簡單的客戶端程序,測試該代碼成功。今天在此做一個記錄。

          Libevent的確是一個非常好用的東西,還在繼續(xù)學(xué)習(xí)中,后續(xù)還要在windows下實現(xiàn)Libevent的多線程使用。今天先把自己搞出來的東西貼上來,僅供學(xué)習(xí)參考。在vs2015上編譯通過。

          默認情況下是單線程的(可以配置成多線程,如果有需要的話),每個線程有且只有一event base,對應(yīng)一個struct event_base結(jié)構(gòu)體(以及附于其上的事件管理器),用來schedule托管給它的一系列event,可以和操作系統(tǒng)的進程管理類比,當(dāng)然,要更簡單一點。當(dāng)一個事件發(fā)生后,event_base會在合適的時間(不一定是立即)去調(diào)用綁定在這個事件上的函數(shù)(傳入一些預(yù)定義的參數(shù),以及在綁定時指定的一個參數(shù)),直到這個函數(shù)執(zhí)行完,再返回schedule其他事件。

           //創(chuàng)建一個event_base
          struct event_base *base = event_base_new();
          assert(base != NULL); 
          
          

          event_base內(nèi)部有一個循環(huán),循環(huán)阻塞在epoll / kqueue等系統(tǒng)調(diào)用上,直到有一個 / 一些事件發(fā)生,然后去處理這些事件。當(dāng)然,這些事件要被綁定在這個event_base上。每個事件對應(yīng)一個struct event,可以是監(jiān)聽一個fd或者POSIX信號量之類(這里只講fd了,其他的看manual吧)。struct event使用event_new來創(chuàng)建和綁定,使用event_add來啟用:

           //創(chuàng)建并綁定一個event
          struct event *listen_event;
          //參數(shù):event_base, 監(jiān)聽的fd,事件類型及屬性,綁定的回調(diào)函數(shù),給回調(diào)函數(shù)的參數(shù)
           
          listen_event = event_new(base, listener, EV_READ | EV_PERSIST, callback_func, (void*)base);
          //參數(shù):event,超時時間(struct timeval *類型的,NULL表示無超時設(shè)置)
          event_add(listen_event, NULL);
          

          注:libevent支持的事件及屬性包括(使用bitfield實現(xiàn),所以要用 | 來讓它們合體)

          (a) EV_TIMEOUT: 超時
          (b) EV_READ : 只要網(wǎng)絡(luò)緩沖中還有數(shù)據(jù),回調(diào)函數(shù)就會被觸發(fā)
          (c) EV_WRITE : 只要塞給網(wǎng)絡(luò)緩沖的數(shù)據(jù)被寫完,回調(diào)函數(shù)就會被觸發(fā)
          (d) EV_SIGNAL : POSIX信號量,參考manual吧
          (e) EV_PERSIST : 不指定這個屬性的話,回調(diào)函數(shù)被觸發(fā)后事件會被刪除
          (f) EV_ET : Edge - Trigger邊緣觸發(fā),參考EPOLL_ET
          然后需要啟動event_base的循環(huán),這樣才能開始處理發(fā)生的事件。循環(huán)的啟動event base dispatch,循環(huán)將一直持續(xù),直到不再有需要關(guān)注的事件,或者是遇到event_loopbreak() / event_loopexit()函數(shù)。
          //啟動事件循環(huán)
          event_base_dispatch(base);

          接下來關(guān)注下綁定到event的回調(diào)函數(shù)callback_func:傳遞給它的是一個socket fd、一個event類型及屬性bit_field、以及傳遞給event_new的最后一個參數(shù)(去上面幾行回顧一下,把event_base給傳進來了,實際上更多地是分配一個結(jié)構(gòu)體,把相關(guān)的數(shù)據(jù)都撂進去,然后丟給event_new,在這里就能取得到了)。其原型是:
          typedef void(*event_callback_fn)(evutil_socket_t sockfd, short event_type, void *arg) 

          對于一個服務(wù)器而言,上面的流程大概是這樣組合的:
          1. listener = socket(),bind(),listen(),設(shè)置nonblocking(POSIX系統(tǒng)中可使用fcntl設(shè)置,windows不需要設(shè)置,
              實際上libevent提供了統(tǒng)一的包裝evutil_make_socket_nonblocking)
          2. 創(chuàng)建一個event_base
          3. 創(chuàng)建一個event,將該socket托管給event_base,指定要監(jiān)聽的事件類型,并綁定上相應(yīng)的回調(diào)函數(shù)(及需要給它的參數(shù))
              。對于listener socket來說,只需要監(jiān)聽EV_READ | EV_PERSIST
          4. 啟用該事件
          5. 進入事件循環(huán)
          -------------- -
          6. (異步)當(dāng)有client發(fā)起請求的時候,調(diào)用該回調(diào)函數(shù),進行處理。
          /*接下來關(guān)注下綁定到event的回調(diào)函數(shù)callback_func:傳遞給它的是一個socket fd、一個event類型及屬性bit_field、以及傳遞給event_new的最后一個參數(shù)(去上面幾行回顧一下,把event_base給傳進來了,實際上更多地是分配一個結(jié)構(gòu)體,把相關(guān)的數(shù)據(jù)都撂進去,然后丟給event_new,在這里就能取得到了)。*/ 

          服務(wù)器端代碼:Server.cpp

            #include <WinSock2.h>
            #include <stdlib.h>
            #include <stdio.h>
            #include <string.h>
            #include <errno.h>
            #include <event2/event.h>
            #include <event2/bufferevent.h>
            #include<iostream>
            #include<cassert>
            #pragma comment (lib,"ws2_32.lib")
            #include<ws2tcpip.h>
            #define LISTEN_PORT 9999
            #define LIATEN_BACKLOG 32
           using namespace std;
           /*********************************************************************************
           *                   函數(shù)聲明
           **********************************************************************************/
           //accept回掉函數(shù)
           void do_accept_cb(evutil_socket_t listener, short event, void *arg);
           //read 回調(diào)函數(shù)
           void read_cb(struct bufferevent *bev, void *arg);
           //error回調(diào)函數(shù)
           void error_cb(struct bufferevent *bev, short event, void *arg);
           //write 回調(diào)函數(shù)
           void write_cb(struct bufferevent *bev, void *arg);
           /*********************************************************************************
           *                   函數(shù)體
           **********************************************************************************/
           //accept回掉函數(shù)
           void do_accept_cb(evutil_socket_t listener, short event, void *arg)
           {
             //傳入的event_base指針
             struct event_base *base = (struct event_base*)arg;
             //socket描述符
             evutil_socket_t fd;
             //聲明地址
             struct sockaddr_in sin;
             //地址長度聲明
             socklen_t slen = sizeof(sin);
             //接收客戶端
             fd = accept(listener, (struct sockaddr *)&sin, &slen);
             if (fd < 0)
             {
               perror("error accept");
               return;
             }
             printf("ACCEPT: fd = %u\n", fd);
             ////注冊一個bufferevent_socket_new事件
             struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
             ////設(shè)置回掉函數(shù)
             bufferevent_setcb(bev, read_cb, NULL, error_cb, arg);
             ////設(shè)置該事件的屬性
             bufferevent_enable(bev, EV_READ | EV_WRITE | EV_PERSIST);
           }
           ////read 回調(diào)函數(shù)
           void read_cb(struct bufferevent *bev, void *arg)
           {
           #define MAX_LINE 256
             char line[MAX_LINE + 1];
             int n;
             //通過傳入?yún)?shù)bev找到socket fd
             evutil_socket_t fd = bufferevent_getfd(bev);
             //
             while (n = bufferevent_read(bev, line, MAX_LINE))
             {
               line[n] = '\0';
               printf("fd=%u, read line: %s\n", fd, line);
               //將獲取的數(shù)據(jù)返回給客戶端
               bufferevent_write(bev, line, n);
             }
           }
           ////error回調(diào)函數(shù)
           void error_cb(struct bufferevent *bev, short event, void *arg)
           {
             //通過傳入?yún)?shù)bev找到socket fd
             evutil_socket_t fd = bufferevent_getfd(bev);
             //cout << "fd = " << fd << endl;
             if (event & BEV_EVENT_TIMEOUT)
             {
               printf("Timed out\n"); //if bufferevent_set_timeouts() called
             }
             else if (event & BEV_EVENT_EOF)
             {
               printf("connection closed\n");
             }
             else if (event & BEV_EVENT_ERROR)
             {
               printf("some other error\n");
             }
             bufferevent_free(bev);
           }
           ////write 回調(diào)函數(shù)
           void write_cb(struct bufferevent *bev, void *arg)
           {
             char str[50];
             //通過傳入?yún)?shù)bev找到socket fd
             evutil_socket_t fd = bufferevent_getfd(bev);
             //cin >> str;
             printf("輸入數(shù)據(jù)!");
             scanf_s("%d", &str);
             bufferevent_write(bev, &str, sizeof(str));
           }
           
           int main()
           {
             int ret;
             evutil_socket_t listener;
             WSADATA Ws;
             //Init Windows Socket
             if (WSAStartup(MAKEWORD(2, 2), &Ws) != 0)
             {
               return -1;
             }
             listener = socket(AF_INET, SOCK_STREAM, 0);
             assert(listener > 0);
             evutil_make_listen_socket_reuseable(listener);
             struct sockaddr_in sin;
             sin.sin_family = AF_INET;
             sin.sin_addr.s_addr = 0;
             sin.sin_port = htons(LISTEN_PORT);
             if (bind(listener, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
               perror("bind");
               return 1;
             }
             if (listen(listener, 1000) < 0) {
               perror("listen");
               return 1;
             }
            printf("Listening...\n");
             evutil_make_socket_nonblocking(listener);
             struct event_base *base = event_base_new();
             assert(base != NULL);
             struct event *listen_event;
             listen_event = event_new(base, listener, EV_READ | EV_PERSIST, do_accept_cb, (void*)base);
             event_add(listen_event, NULL);
             event_base_dispatch(base);
             printf("The End.");
             return 0;
           }

          客戶端代碼:Client.cpp

          /******* 客戶端程序 client.c ************/
          #define _WINSOCK_DEPRECATED_NO_WARNINGS
          #define _CRT_SECURE_NO_WARNINGS
          #include <stdlib.h> 
          #include <stdio.h> 
          #include <errno.h> 
          #include <string.h>    
          #include<winsock2.h>
          #include<ws2tcpip.h>
          #include<iostream>
           
           #pragma comment (lib,"ws2_32.lib")
           int main(int argc, char *argv[])
           {
             WSADATA Ws;
             //Init Windows Socket
             if (WSAStartup(MAKEWORD(2, 2), &Ws) != 0)
             {
               return 0;
             }
             int sockfd;
             char buffer[1024];
             struct sockaddr_in server_addr;
             struct hostent *host;
             int portnumber, nbytes;
           
             if ((host = gethostbyname("127.0.0.1")) == NULL)
             {
               fprintf(stderr, "Gethostname error\n");
               exit(1);
             }
           
             if ((portnumber = atoi("9999"))<0)
             {
               fprintf(stderr, "Usage:%s hostname portnumber\a\n", argv[0]);
               exit(1);
             }
           
             /* 客戶程序開始建立 sockfd描述符 */
             if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
             {
               fprintf(stderr, "Socket Error:%s\a\n", strerror(errno));
               exit(1);
             }
           
             /* 客戶程序填充服務(wù)端的資料    */
             memset(&server_addr,0, sizeof(server_addr));
             server_addr.sin_family = AF_INET;
             server_addr.sin_port = htons(portnumber);
             server_addr.sin_addr = *((struct in_addr *)host->h_addr);
           
             /* 客戶程序發(fā)起連接請求     */
             if (connect(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1)
            {
               fprintf(stderr, "Connect Error:%s\a\n", strerror(errno));
               exit(1);
             }
           
             while (true)
             {
               char MESSAGE[] = "hello server..\n";
               //bufferevent_write(buf_ev,MESSAGE,strlen(MESSAGE)); 
               // 
               if (-1 == (::send(sockfd, MESSAGE, strlen(MESSAGE), 0)))
               {
                 printf("the net has a error occured..");
                 break;
               }
           
               if ((nbytes = recv(sockfd, buffer, 1024,0)) == -1)
              {
                 fprintf(stderr, "read error:%s\n", strerror(errno));
                 exit(1);
               }
           
               buffer[nbytes] = '\0';
               printf("I have received:%s\n", buffer);
               memset(buffer, 0, 1024);
           
               Sleep(2);
           
             }
             /* 結(jié)束通訊   */
             closesocket(sockfd);
             exit(0);
           
             return 0;
           }
          

          以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

          相關(guān)文章

          最新評論