ubuntu安装libevent2.0.20

1.是否安装

安装了的话应该是这样的:如果不是,那就装一下。

chen@chen-book1:~/libevent20$ ls -al /usr/lib |grep libevent

lrwxrwxrwx   1 root root           21 2013-06-07 10:43 libevent-2.0.so.5 -> libevent-2.0.so.5.1.4 -rw-r--r--   1 root root       281472 2011-07-08 08:28 libevent-2.0.so.5.1.1 -rwxr-xr-x   1 root root       814018 2013-06-07 10:43 libevent-2.0.so.5.1.4 -rw-r--r--   1 root root      1144276 2013-06-07 10:43 libevent.a lrwxrwxrwx   1 root root           26 2013-06-07 10:43 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.4 -rwxr-xr-x   1 root root       493956 2013-06-07 10:43 libevent_core-2.0.so.5.1.4 -rw-r--r--   1 root root       716442 2013-06-07 10:43 libevent_core.a -rwxr-xr-x   1 root root          983 2013-06-07 10:43 libevent_core.la lrwxrwxrwx   1 root root           26 2013-06-07 10:43 libevent_core.so -> libevent_core-2.0.so.5.1.4 lrwxrwxrwx   1 root root           27 2013-06-07 10:43 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.4 -rwxr-xr-x   1 root root       337896 2013-06-07 10:43 libevent_extra-2.0.so.5.1.4 -rw-r--r--   1 root root       427906 2013-06-07 10:43 libevent_extra.a -rwxr-xr-x   1 root root          990 2013-06-07 10:43 libevent_extra.la lrwxrwxrwx   1 root root           27 2013-06-07 10:43 libevent_extra.so -> libevent_extra-2.0.so.5.1.4 -rwxr-xr-x   1 root root          948 2013-06-07 10:43 libevent.la lrwxrwxrwx   1 root root           30 2013-06-07 10:43 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.4 -rwxr-xr-x   1 root root        19138 2013-06-07 10:43 libevent_pthreads-2.0.so.5.1.4 -rw-r--r--   1 root root        12226 2013-06-07 10:43 libevent_pthreads.a -rwxr-xr-x   1 root root         1011 2013-06-07 10:43 libevent_pthreads.la lrwxrwxrwx   1 root root           30 2013-06-07 10:43 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.4 lrwxrwxrwx   1 root root           21 2013-06-07 10:43 libevent.so -> libevent-2.0.so.5.1.4

2.下载

http://libevent.org/

3.安装

解压;

进入目录;

./configure –prefix=/usr         配置目录

make

sudo make install

4.验证

chen@chen-book1:~/libevent20$ ls -al /usr/lib |grep libevent

lrwxrwxrwx   1 root root           21 2013-06-07 10:43 libevent-2.0.so.5 -> libevent-2.0.so.5.1.4 -rw-r--r--   1 root root       281472 2011-07-08 08:28 libevent-2.0.so.5.1.1 -rwxr-xr-x   1 root root       814018 2013-06-07 10:43 libevent-2.0.so.5.1.4 -rw-r--r--   1 root root      1144276 2013-06-07 10:43 libevent.a lrwxrwxrwx   1 root root           26 2013-06-07 10:43 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.4 -rwxr-xr-x   1 root root       493956 2013-06-07 10:43 libevent_core-2.0.so.5.1.4 -rw-r--r--   1 root root       716442 2013-06-07 10:43 libevent_core.a -rwxr-xr-x   1 root root          983 2013-06-07 10:43 libevent_core.la lrwxrwxrwx   1 root root           26 2013-06-07 10:43 libevent_core.so -> libevent_core-2.0.so.5.1.4 lrwxrwxrwx   1 root root           27 2013-06-07 10:43 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.4 -rwxr-xr-x   1 root root       337896 2013-06-07 10:43 libevent_extra-2.0.so.5.1.4 -rw-r--r--   1 root root       427906 2013-06-07 10:43 libevent_extra.a -rwxr-xr-x   1 root root          990 2013-06-07 10:43 libevent_extra.la lrwxrwxrwx   1 root root           27 2013-06-07 10:43 libevent_extra.so -> libevent_extra-2.0.so.5.1.4 -rwxr-xr-x   1 root root          948 2013-06-07 10:43 libevent.la lrwxrwxrwx   1 root root           30 2013-06-07 10:43 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.4 -rwxr-xr-x   1 root root        19138 2013-06-07 10:43 libevent_pthreads-2.0.so.5.1.4 -rw-r--r--   1 root root        12226 2013-06-07 10:43 libevent_pthreads.a -rwxr-xr-x   1 root root         1011 2013-06-07 10:43 libevent_pthreads.la lrwxrwxrwx   1 root root           30 2013-06-07 10:43 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.4

lrwxrwxrwx   1 root root           21 2013-06-07 10:43 libevent.so -> libevent-2.0.so.5.1.4

 

5.测试

服务器端ser.c

 

  1. #include <sys/socket.h>  
  2. #include <sys/types.h>  
  3. #include <netinet/in.h>  
  4. #include <stdio.h>  
  5. #include <string.h>  
  6. #include <event.h>  
  7. #include <stdlib.h>  
  8.   
  9. #define PORT        25341  
  10. #define BACKLOG     5  
  11. #define MEM_SIZE    1024  
  12.   
  13. struct event_base* base;  
  14. struct sock_ev  
  15. {  
  16.     struct event* read_ev;  
  17.     struct event* write_ev;  
  18.     char* buffer;  
  19. };  
  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.   
  30. void on_write(int sock, short event, void* arg)  
  31. {  
  32.     char* buffer = (char*)arg;  
  33.     send(sock, buffer, strlen(buffer), 0);  
  34.   
  35.     free(buffer);  
  36. }  
  37.   
  38. void on_read(int sock, short event, void* arg)  
  39. {  
  40.     struct event* write_ev;  
  41.     int size;  
  42.     struct sock_ev* ev = (struct sock_ev*)arg;  
  43.     ev->buffer = (char*)malloc(MEM_SIZE);  
  44.     bzero(ev->buffer, MEM_SIZE);  
  45.     size = recv(sock, ev->buffer, MEM_SIZE, 0);  
  46.     printf("receive data:%s, size:%d\n", ev->buffer, size);  
  47.     if (size == 0)   
  48.     {  
  49.         release_sock_event(ev);  
  50.         close(sock);  
  51.         return;  
  52.     }  
  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.   
  58. void on_accept(int sock, short event, void* arg)  
  59. {  
  60.     struct sockaddr_in cli_addr;  
  61.     int newfd, sin_size;  
  62.     struct sock_ev* ev = (struct sock_ev*)malloc(sizeof(struct sock_ev));  
  63.     ev->read_ev = (struct event*)malloc(sizeof(struct event));  
  64.     ev->write_ev = (struct event*)malloc(sizeof(struct event));  
  65.     sin_size = sizeof(struct sockaddr_in);  
  66.     newfd = accept(sock, (struct sockaddr*)&cli_addr, &sin_size);  
  67.     event_set(ev->read_ev, newfd, EV_READ|EV_PERSIST, on_read, ev);  
  68.     event_base_set(base, ev->read_ev);  
  69.     event_add(ev->read_ev, NULL);  
  70. }  
  71.   
  72. int main(int argc, char* argv[])  
  73. {  
  74.     struct sockaddr_in my_addr;  
  75.     int sock;  
  76.   
  77.     sock = socket(AF_INET, SOCK_STREAM, 0);  
  78.     int yes = 1;  
  79.     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));  
  80.     memset(&my_addr, 0, sizeof(my_addr));  
  81.     my_addr.sin_family = AF_INET;  
  82.     my_addr.sin_port = htons(PORT);  
  83.     my_addr.sin_addr.s_addr = INADDR_ANY;  
  84.     bind(sock, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));  
  85.     listen(sock, BACKLOG);  
  86.   
  87.     struct event listen_ev;  
  88.     base = event_base_new();  
  89.     event_set(&listen_ev, sock, EV_READ|EV_PERSIST, on_accept, NULL);  
  90.     event_base_set(base, &listen_ev);  
  91.     event_add(&listen_ev, NULL);  
  92.     event_base_dispatch(base);  
  93.   
  94.     return 0;  
  95. }  
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <event.h>
#include <stdlib.h>

#define PORT        25341
#define BACKLOG     5
#define MEM_SIZE    1024

struct event_base* base;
struct sock_ev
{
    struct event* read_ev;
    struct event* write_ev;
    char* buffer;
};

void release_sock_event(struct sock_ev* ev)
{
    event_del(ev->read_ev);
    free(ev->read_ev);
    free(ev->write_ev);
    free(ev->buffer);
    free(ev);
}

void on_write(int sock, short event, void* arg)
{
    char* buffer = (char*)arg;
    send(sock, buffer, strlen(buffer), 0);

    free(buffer);
}

void on_read(int sock, short event, void* arg)
{
    struct event* write_ev;
    int size;
    struct sock_ev* ev = (struct sock_ev*)arg;
    ev->buffer = (char*)malloc(MEM_SIZE);
    bzero(ev->buffer, MEM_SIZE);
    size = recv(sock, ev->buffer, MEM_SIZE, 0);
    printf("receive data:%s, size:%d\n", ev->buffer, size);
    if (size == 0) 
    {
        release_sock_event(ev);
        close(sock);
        return;
    }
    event_set(ev->write_ev, sock, EV_WRITE, on_write, ev->buffer);
    event_base_set(base, ev->write_ev);
    event_add(ev->write_ev, NULL);
}

void on_accept(int sock, short event, void* arg)
{
    struct sockaddr_in cli_addr;
    int newfd, sin_size;
    struct sock_ev* ev = (struct sock_ev*)malloc(sizeof(struct sock_ev));
    ev->read_ev = (struct event*)malloc(sizeof(struct event));
    ev->write_ev = (struct event*)malloc(sizeof(struct event));
    sin_size = sizeof(struct sockaddr_in);
    newfd = accept(sock, (struct sockaddr*)&cli_addr, &sin_size);
    event_set(ev->read_ev, newfd, EV_READ|EV_PERSIST, on_read, ev);
    event_base_set(base, ev->read_ev);
    event_add(ev->read_ev, NULL);
}

int main(int argc, char* argv[])
{
    struct sockaddr_in my_addr;
    int sock;

    sock = socket(AF_INET, SOCK_STREAM, 0);
    int yes = 1;
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
    memset(&my_addr, 0, sizeof(my_addr));
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(PORT);
    my_addr.sin_addr.s_addr = INADDR_ANY;
    bind(sock, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
    listen(sock, BACKLOG);

    struct event listen_ev;
    base = event_base_new();
    event_set(&listen_ev, sock, EV_READ|EV_PERSIST, on_accept, NULL);
    event_base_set(base, &listen_ev);
    event_add(&listen_ev, NULL);
    event_base_dispatch(base);

    return 0;
}

客户端echo_client.c:

 

 

  1. #include <unistd.h>  
  2. #include <stdio.h>  
  3. #include <fcntl.h>  
  4. #include <netdb.h>  
  5. #include <sys/socket.h>  
  6. #include <stdlib.h>  
  7. #include <string.h>  
  8. #define cerror(str) do{perror(str); exit(EXIT_FAILURE); }while(0)  
  9.   
  10. static int port;   
  11. static char addr[256];  
  12. static int nlen;  
  13. static char msg[]="hello\n";  
  14. int main(int argc, char*argv[])  
  15. {  
  16.       
  17.     int fd;  
  18.     struct sockaddr_in sin;  
  19.       
  20.     memset(&sin, 0, sizeof(sin));  
  21.     sin.sin_family = AF_INET;  
  22.     
  23.     if(argc<3)  
  24.     {  
  25.         port = 8888;  
  26.     }else  
  27.     {  
  28.         port = atoi(argv[2]);  
  29.     }  
  30.       
  31.     sin.sin_port = htons(port);  
  32.       
  33.     if(argc<2)  
  34.     {  
  35.         strcpy(addr, argv[1]);  
  36.         if (inet_pton(AF_INET, addr, &(sin.sin_addr) )<0)  
  37.         {  
  38.              struct hostent *psh;    
  39.              psh=gethostbyname(addr);  
  40.              if(psh!=NULL)  
  41.                 inet_pton(AF_INET, psh->h_addr, &(sin.sin_addr) );  
  42.              else  
  43.                 cerror("inet_pton");  
  44.         }  
  45.     }  
  46.       
  47.     if((fd=socket(AF_INET, SOCK_STREAM, 0))<0)  
  48.         cerror("socket");  
  49.       
  50.     if(connect(fd, (struct sockaddr*)&sin, sizeof(sin))<0 )  
  51.         cerror("sonnect");  
  52.       
  53.     if( (nlen = write(fd,msg, strlen(msg)))<0 )  
  54.         cerror("write");  
  55.           
  56.     if( (nlen = read(fd,msg, strlen(msg)))<0 )  
  57.         cerror("read");  
  58.     msg[nlen]='\0';  
  59.     printf("msg: %s\n", msg);  
  60.     return 0;  
  61. }  
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#define cerror(str) do{perror(str); exit(EXIT_FAILURE); }while(0)

static int port; 
static char addr[256];
static int nlen;
static char msg[]="hello\n";
int main(int argc, char*argv[])
{
    
    int fd;
    struct sockaddr_in sin;
    
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
  
    if(argc<3)
    {
        port = 8888;
    }else
    {
        port = atoi(argv[2]);
    }
    
    sin.sin_port = htons(port);
    
    if(argc<2)
    {
        strcpy(addr, argv[1]);
        if (inet_pton(AF_INET, addr, &(sin.sin_addr) )<0)
        {
             struct hostent *psh;  
             psh=gethostbyname(addr);
             if(psh!=NULL)
                inet_pton(AF_INET, psh->h_addr, &(sin.sin_addr) );
             else
                cerror("inet_pton");
        }
    }
    
    if((fd=socket(AF_INET, SOCK_STREAM, 0))<0)
        cerror("socket");
    
    if(connect(fd, (struct sockaddr*)&sin, sizeof(sin))<0 )
        cerror("sonnect");
    
    if( (nlen = write(fd,msg, strlen(msg)))<0 )
        cerror("write");
        
    if( (nlen = read(fd,msg, strlen(msg)))<0 )
        cerror("read");
    msg[nlen]='\0';
    printf("msg: %s\n", msg);
    return 0;
}

编译

 

 

  1. chen@chen-book1:~/libevent20/sample$ gcc echo_client.c -o client  
  2. chen@chen-book1:~/libevent20/sample$ gcc ser.c -o ser -levent  
  3. chen@chen-book1:~/libevent20/sample$ ./ser &  
  4. [1] 6995  
  5. chen@chen-book1:~/libevent20/sample$ ./client localhost 25341  
  6. receive data:hello  
  7. , size:6  
  8. msg: hello  
  9.   
  10. receive data:, size:0  
  11. chen@chen-book1:~/libevent20/sample$ ./client localhost 25341  
  12. receive data:hello  
  13. , size:6  
  14. msg: hello  
chen@chen-book1:~/libevent20/sample$ gcc echo_client.c -o client
chen@chen-book1:~/libevent20/sample$ gcc ser.c -o ser -levent
chen@chen-book1:~/libevent20/sample$ ./ser &
[1] 6995
chen@chen-book1:~/libevent20/sample$ ./client localhost 25341
receive data:hello
, size:6
msg: hello

receive data:, size:0
chen@chen-book1:~/libevent20/sample$ ./client localhost 25341
receive data:hello
, size:6
msg: hello

代码和安装程序下载链接:

http://download.csdn.net/detail/fall221/5537153

posted @ 2014-02-13 10:30  roilat  阅读(1772)  评论(0编辑  收藏  举报