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

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: C/C++

2015-08-24 22:41:07

gcc  libserver.c -o libserver -levent

点击(此处)折叠或打开

  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. #include<time.h>
  10. #define PORT 5188
  11. #define BACKLOG 5
  12. #define BUFFSIZE 1024
  13. /* 初始化libevent */
  14. struct event_base* base;
  15. /*sock_ev struct*/
  16. struct sock_ev
  17. {
  18.     struct event* read_ev;
  19.     struct event* write_ev;
  20.     unsigned char* buffer;
  21. };
  22. /* release event */
  23. void release_sock_event(struct sock_ev* ev)
  24. {
  25.     event_del(ev->read_ev);
  26.     free(ev->read_ev);
  27.     free(ev->write_ev);
  28.     free(ev->buffer);
  29.     free(ev);
  30. }

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

  35.     free(buffer);
  36. }
  37. void register_package(unsigned char *buffer){
  38.     int i;
  39.     printf("Receive Register Package\n");
  40.     printf("ID=%x%x%x%x\n",buffer[3],buffer[2],buffer[1],buffer[0]);
  41.     printf("PHONE_NUMBER=");
  42.     for(i=4;i<15;i++){
  43.         printf("%c",buffer[i]); //PHONE_NUMBER打印
  44.     }
  45.     printf("\n");
  46.     printf("IP address: %d.%d.%d.%d",buffer[16],buffer[17],buffer[18],buffer[19]);
  47.     printf("\n");
  48. }
  49. void on_read(int sock, short event, void* arg)
  50. {
  51.     struct event* write_ev;
  52.     int size;
  53.     struct sock_ev* ev = (struct sock_ev*)arg;
  54.     ev->buffer = (char*)malloc(BUFFSIZE);
  55.     bzero(ev->buffer, BUFFSIZE);
  56.     size = recv(sock, ev->buffer, BUFFSIZE, 0);
  57.     register_package(ev->buffer);
  58.     if (size == 0)
  59.     {
  60.         release_sock_event(ev);
  61.         close(sock);
  62.         return;
  63.     }
  64.      bzero(ev->buffer, BUFFSIZE);
  65.     strcat(ev->buffer,"libevent i know you\n");
  66.     event_set(ev->write_ev, sock, EV_WRITE, on_write, ev->buffer);
  67.     event_base_set(base, ev->write_ev);
  68.     event_add(ev->write_ev, NULL);
  69. }

  70. void on_accept(int sock, short event, void* arg)
  71. {
  72.     struct sockaddr_in cli_addr;
  73.     int newfd, sin_size;
  74.     struct sock_ev* ev = (struct sock_ev*)malloc(sizeof(struct sock_ev));
  75.     ev->read_ev = (struct event*)malloc(sizeof(struct event));
  76.     ev->write_ev = (struct event*)malloc(sizeof(struct event));
  77.     sin_size = sizeof(struct sockaddr_in);
  78.     /* accept */
  79.     newfd = accept(sock, (struct sockaddr*)&cli_addr, &sin_size);
  80.     printf("ip=%s port=%d\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
  81.     event_set(ev->read_ev, newfd, EV_READ|EV_PERSIST, on_read, ev);
  82.     event_base_set(base, ev->read_ev);
  83.     event_add(ev->read_ev, NULL);
  84. }

  85. int main(int argc, char* argv[])
  86. {
  87.     struct sockaddr_in my_addr;
  88.     int sock;

  89.     sock = socket(AF_INET, SOCK_STREAM, 0);
  90.     int yes = 1;
  91.     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
  92.     memset(&my_addr, 0, sizeof(my_addr));
  93.     my_addr.sin_family = AF_INET;
  94.     my_addr.sin_port = htons(PORT);
  95.     my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  96.     bind(sock, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
  97.     listen(sock, BACKLOG);
  98.     /*libevent*/
  99. /*     1 建立默认的 event_base event_base_new()函数声明在<event2/event.h>*/

  100.     base = event_base_new();
  101.     struct event listen_ev;
  102. /*     初始化事件event,设置回调函数和关注的事件 */
  103. /*void event_set(struct event *ev, int fd, short event, void (*cb)(int, short, void *), void *arg)*/
  104.     event_set(&listen_ev, sock, EV_READ|EV_PERSIST, on_accept, NULL);
  105.     /* 3)设置event从属的event_base */
  106.     /* event_base_set(base, &ev); */
  107. /* 这一步相当于指明event要注册到哪个event_base实例上; */
  108.     event_base_set(base, &listen_ev);

  109. /*     4)是正式的添加事件的时候了 */
  110.     event_add(&listen_ev, NULL);
  111.     /*event_add(&listen_ev, timeout);*/
  112. /*     基本信息都已设置完成,只要简单的调用event_add()函数即可完成,其中timeout是定时值; */
  113.     /* 5)程序进入无限循环,等待就绪事件并执行事件处理 */
  114.     event_base_dispatch(base);

  115.     return 0;
  116. }

gcc libclient.c -o libclient

点击(此处)折叠或打开

  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.     while(1){
  21.     char sendbuf[2048]={0x78,0x56,0x34,0x12,0x31,0x33,0x30,0x36,0x36,0x36,0x36,0x38,0x38,0x38,0x36,0x38,0xc0,0xa8,0x01,0x42};
  22.     char recvbuf[1024]={0};
  23.     write(fd,sendbuf,strlen(sendbuf));
  24.     sleep(5);
  25.     read(fd,recvbuf,sizeof(recvbuf));
  26.     fputs(recvbuf,stdout);
  27.     memset(sendbuf,0,sizeof(sendbuf));
  28.     memset(recvbuf,0,sizeof(recvbuf));
  29.     sleep(2);
  30.     
  31.     }
  32.     close(fd);
  33.     return 0;
  34. }

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