libevent 简单客户端和服务器
server.cpp
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 |
#include <stdio.h> #include <string.h> #include <iostream> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <unistd.h> #include <event.h> using namespace std; // 事件base struct event_base* base; // 读事件回调函数 void onRead(int iCliFd, short iEvent, void *arg) { int iLen; char buf[1500]; iLen = recv(iCliFd, buf, 1500, 0); if (iLen <= 0) { cout << "Client Close" << endl; // 连接结束(=0)或连接错误(<0),将事件删除并释放内存空间 struct event *pEvRead = (struct event*) arg; event_del(pEvRead); delete pEvRead; close(iCliFd); return; } buf[iLen] = 0; cout << "Client Info:" << buf << endl; struct bufferevent* buf_ev; buf_ev = bufferevent_new(iCliFd, NULL, NULL, NULL, NULL); buf_ev->wm_read.high = 4096; char MESSAGE[] = "welcome to server.."; bufferevent_write(buf_ev, MESSAGE, strlen(MESSAGE)); } // 连接请求事件回调函数 void onAccept(int iSvrFd, short iEvent, void *arg) { int iCliFd; struct sockaddr_in sCliAddr; socklen_t iSinSize = sizeof(sCliAddr); iCliFd = accept(iSvrFd, (struct sockaddr*) &sCliAddr, &iSinSize); // 连接注册为新事件 (EV_PERSIST为事件触发后不默认删除) struct event *pEvRead = new event; event_set(pEvRead, iCliFd, EV_READ | EV_PERSIST, onRead, pEvRead); event_base_set(base, pEvRead); event_add(pEvRead, NULL); struct bufferevent* buf_ev; buf_ev = bufferevent_new(iCliFd, NULL, NULL, NULL, NULL); buf_ev->wm_read.high = 4096; char MESSAGE[] = "welcome to server.."; bufferevent_write(buf_ev, MESSAGE, strlen(MESSAGE)); cout << "a client connect:" << iCliFd << endl; } int main() { int iSvrFd; struct sockaddr_in sSvrAddr; memset(&sSvrAddr, 0, sizeof(sSvrAddr)); sSvrAddr.sin_family = AF_INET; sSvrAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); sSvrAddr.sin_port = htons(8888); // 创建tcpSocket(iSvrFd),监听本机8888端口 iSvrFd = socket(AF_INET, SOCK_STREAM, 0); bind(iSvrFd, (struct sockaddr*) &sSvrAddr, sizeof(sSvrAddr)); listen(iSvrFd, 10); // 初始化base base = (struct event_base*) event_init(); struct event evListen; // 设置事件 event_set(&evListen, iSvrFd, EV_READ | EV_PERSIST, onAccept, NULL); // 设置为base事件 event_base_set(base, &evListen); // 添加事件 event_add(&evListen, NULL); // 事件循环 event_base_dispatch(base); return 0; } |
client.cpp
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 |
/******* 客户端程序 client.c ************/ #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <netdb.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/types.h> #include <arpa/inet.h> #include<sys/time.h> #include<event.h> int main(int argc, char *argv[]) { 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("8888")) < 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); } /* 客户程序填充服务端的资料 */ bzero(&server_addr, 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); /* 客户程序发起连接请求 */ 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 = read(sockfd, buffer, 1024)) == -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); } /* 结束通讯 */ close(sockfd); exit(0); } |
编译命令:
g++ -g server.cpp -o server -levent
g++ -g client.cpp -o client
发表评论
要发表评论,您必须先登录。