Chinaunix首页 | 论坛 | 博客
  • 博客访问: 275734
  • 博文数量: 113
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1044
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-15 16:09
文章分类

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: C/C++

2015-08-22 22:46:49


1.安装libevent

我有同样的问题,就像其他答案建议我安装了“libevent”。这显然不是叫“libevent发展”(apt-get无法找到它)而做的:

 $ apt-cache search libevent

列出一堆可用的软件包。

 $ apt-get install libevent-dev (其他linux系统用yum -y install libevent-devel)
首先,安装libevent到任意目录下


wget ~provos/libevent-1.4.13-stable.tar.gz
tar –xzvf libevent-1.4.13-stable.tar.gz
cd libevent-1.4.13-stable
./configure --prefix=/home/mydir/libevent
make && make install

点击(此处)折叠或打开

  1. /*libevent server*/
  2. #include <sys/socket.h>
  3. #include <sys/types.h>
  4. #include <netinet/in.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <event.h>
  8. #include <stdlib.h>

  9. #define PORT 5188
  10. #define BACKLOG 5
  11. #define MEM_SIZE 1024

  12. struct event_base* base;
  13. struct sock_ev
  14. {
  15.     struct event* read_ev;
  16.     struct event* write_ev;
  17.     char* buffer;
  18. };

  19. void release_sock_event(struct sock_ev* ev)
  20. {
  21.     event_del(ev->read_ev);
  22.     free(ev->read_ev);
  23.     free(ev->write_ev);
  24.     free(ev->buffer);
  25.     free(ev);
  26. }

  27. void on_write(int sock, short event, void* arg)
  28. {
  29.     char* buffer = (char*)arg;
  30.     send(sock, buffer, strlen(buffer), 0);

  31.     free(buffer);
  32. }

  33. void on_read(int sock, short event, void* arg)
  34. {
  35.     struct event* write_ev;
  36.     int size;
  37.     struct sock_ev* ev = (struct sock_ev*)arg;
  38.     ev->buffer = (char*)malloc(MEM_SIZE);
  39.     bzero(ev->buffer, MEM_SIZE);
  40.     size = recv(sock, ev->buffer, MEM_SIZE, 0);
  41.     printf("receive data:%s, size:%d\n", ev->buffer, size);
  42.     if (size == 0)
  43.     {
  44.         release_sock_event(ev);
  45.         close(sock);
  46.         return;
  47.     }
  48.     event_set(ev->write_ev, sock, EV_WRITE, on_write, ev->buffer);
  49.     event_base_set(base, ev->write_ev);
  50.     event_add(ev->write_ev, NULL);
  51. }

  52. void on_accept(int sock, short event, void* arg)
  53. {
  54.     struct sockaddr_in cli_addr;
  55.     int newfd, sin_size;
  56.     struct sock_ev* ev = (struct sock_ev*)malloc(sizeof(struct sock_ev));
  57.     ev->read_ev = (struct event*)malloc(sizeof(struct event));
  58.     ev->write_ev = (struct event*)malloc(sizeof(struct event));
  59.     sin_size = sizeof(struct sockaddr_in);
  60.     newfd = accept(sock, (struct sockaddr*)&cli_addr, &sin_size);
  61.     event_set(ev->read_ev, newfd, EV_READ|EV_PERSIST, on_read, ev);
  62.     event_base_set(base, ev->read_ev);
  63.     event_add(ev->read_ev, NULL);
  64. }

  65. int main(int argc, char* argv[])
  66. {
  67.     struct sockaddr_in my_addr;
  68.     int sock;

  69.     sock = socket(AF_INET, SOCK_STREAM, 0);
  70.     int yes = 1;
  71.     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
  72.     memset(&my_addr, 0, sizeof(my_addr));
  73.     my_addr.sin_family = AF_INET;
  74.     my_addr.sin_port = htons(PORT);
  75.     my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  76.     bind(sock, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
  77.     listen(sock, BACKLOG);
  78.     /*libevent*/
  79.     struct event listen_ev;
  80.     base = event_base_new();
  81.     event_set(&listen_ev, sock, EV_READ|EV_PERSIST, on_accept, NULL);
  82.     event_base_set(base, &listen_ev);
  83.     event_add(&listen_ev, NULL);
  84.     event_base_dispatch(base);

  85.     return 0;
  86. }
$  gcc  libevent_server.c -o libserver -levent

点击(此处)折叠或打开

  1. /*libevent client*/
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <netdb.h>
  6. #include <sys/socket.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define cerror(str) do{perror(str); exit(EXIT_FAILURE); }while(0)
  10. int main()
  11. {
  12.     
  13.     int fd;
  14.     struct sockaddr_in sin;
  15.     
  16.     memset(&sin, 0, sizeof(sin));
  17.     sin.sin_family = AF_INET;
  18.     sin.sin_port = htons(5188);
  19.     sin.sin_addr.s_addr = inet_addr("127.0.0.1");
  20.   /* if(argc<3)
  21.     {
  22.         port = 8888;
  23.     }else
  24.     {
  25.         port = atoi(argv[2]);
  26.     }
  27.     
  28.     sin.sin_port = htons(port);
  29.     
  30.     if(argc<2)
  31.     {
  32.         strcpy(addr, argv[1]);
  33.         if (inet_pton(AF_INET, addr, &(sin.sin_addr) )<0)
  34.         {
  35.              struct hostent *psh;
  36.              psh=gethostbyname(addr);
  37.              if(psh!=NULL)
  38.                 inet_pton(AF_INET, psh->h_addr, &(sin.sin_addr) );
  39.              else
  40.                 cerror("inet_pton");
  41.         }
  42.     }
  43.      */
  44.     fd=socket(AF_INET, SOCK_STREAM, 0);
  45.     /* if( */connect(fd, (struct sockaddr*)&sin, sizeof(sin));/* <0 ) */
  46.      /* cerror("connect"); */
  47.     
  48.     char sendbuf[1024]={0};
  49.     char recvbuf[1024]={0};
  50.     while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL)
  51.     {
  52.         write(fd,sendbuf,strlen(sendbuf));
  53.         read(fd,recvbuf,sizeof(recvbuf));
  54.         fputs(recvbuf,stdout);
  55.         memset(sendbuf,0,sizeof(sendbuf));
  56.         memset(recvbuf,0,sizeof(recvbuf));
  57.     }
  58.     close(fd);
  59.     return 0;
  60. }
$  gcc libclient.c -o libclient
阅读(1717) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~