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

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: C/C++

2015-08-24 18:09:17


点击(此处)折叠或打开

  1. /*libevent server*/
  2. /*gcc  libserver.c -o libserver -levent*/
  3. #include <sys/socket.h>
  4. #include <sys/types.h>
  5. #include <netinet/in.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <event.h>
  9. #include <stdlib.h>

  10. #define PORT 5188
  11. #define BACKLOG 5
  12. #define BUFFSIZE 1024
  13. /* 初始化libevent */
  14. struct event_base* base;
  15. struct sock_ev
  16. {
  17.     struct event* read_ev;
  18.     struct event* write_ev;
  19.     char* buffer;
  20. };

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

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

  33.     free(buffer);
  34. }

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

  57. void on_accept(int sock, short event, void* arg)
  58. {
  59.     struct sockaddr_in cli_addr;
  60.     int newfd, sin_size;
  61.     struct sock_ev* ev = (struct sock_ev*)malloc(sizeof(struct sock_ev));
  62.     ev->read_ev = (struct event*)malloc(sizeof(struct event));
  63.     ev->write_ev = (struct event*)malloc(sizeof(struct event));
  64.     sin_size = sizeof(struct sockaddr_in);
  65.     /* accept */
  66.     newfd = accept(sock, (struct sockaddr*)&cli_addr, &sin_size);
  67.     printf("ip=%s port=%d\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
  68.     event_set(ev->read_ev, newfd, EV_READ|EV_PERSIST, on_read, ev);
  69.     event_base_set(base, ev->read_ev);
  70.     event_add(ev->read_ev, NULL);
  71. }

  72. int main(int argc, char* argv[])
  73. {
  74.     struct sockaddr_in my_addr;
  75.     int sock;

  76.     sock = socket(AF_INET, SOCK_STREAM, 0);
  77.     int yes = 1;
  78.     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
  79.     memset(&my_addr, 0, sizeof(my_addr));
  80.     my_addr.sin_family = AF_INET;
  81.     my_addr.sin_port = htons(PORT);
  82.     my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  83.     bind(sock, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
  84.     listen(sock, BACKLOG);
  85.     /*libevent*/
  86. /*     1 建立默认的 event_base event_base_new()函数声明在<event2/event.h>*/

  87.     base = event_base_new();
  88.     struct event listen_ev;
  89. /*     初始化事件event,设置回调函数和关注的事件 */
  90.     event_set(&listen_ev, sock, EV_READ|EV_PERSIST, on_accept, NULL);
  91.     /* 3)设置event从属的event_base */
  92.     event_base_set(base, &listen_ev);
  93. /*     4)是正式的添加事件的时候了 */
  94.     event_add(&listen_ev, NULL);
  95.     /* 5)程序进入无限循环,等待就绪事件并执行事件处理 */
  96.     event_base_dispatch(base);

  97.     return 0;
  98. }


点击(此处)折叠或打开

  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.     int fd;
  13.     struct sockaddr_in sin;
  14.     memset(&sin, 0, sizeof(sin));
  15.     sin.sin_family = AF_INET;
  16.     sin.sin_port = htons(5188);
  17.     sin.sin_addr.s_addr = inet_addr("127.0.0.1");
  18.     fd=socket(AF_INET, SOCK_STREAM, 0);
  19.     connect(fd, (struct sockaddr*)&sin, sizeof(sin));
  20.     char sendbuf[1024]={0};
  21.     char recvbuf[1024]={0};
  22.     while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL)
  23.     {
  24.         write(fd,sendbuf,strlen(sendbuf));
  25.         read(fd,recvbuf,sizeof(recvbuf));
  26.         fputs(recvbuf,stdout);
  27.         memset(sendbuf,0,sizeof(sendbuf));
  28.         memset(recvbuf,0,sizeof(recvbuf));
  29.     }
  30.     close(fd);
  31.     return 0;
  32. }

阅读(661) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~