使用libevent写的一个简单服务器的代码

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <event.h>

void sock_read(int fd, short event, void *arg)
{
    char buf[255];
    int len;
    struct event *ev = arg;

    len = recv(fd, buf, sizeof(buf)-1, 0);

    if (len == -1)
    {
        perror("recv error/n");

        if (errno != EAGAIN && errno != EINTR)
        {
            close(fd);
            free(ev);
        }

        return;
    }
    else if (len == 0)
    {
        close(fd);
        fprintf(stderr, "Connection closed/n");
        free(ev);
        return;
    }

    buf[len] = '/0';

    fprintf(stdout, "Read: %s/n", buf);

    /* Reschedule this event */
    event_add(ev, NULL);
}

void sock_accept(int fd, short event, void *arg)
{
    struct event *ev = arg;
    struct sockaddr addr;
    socklen_t len = sizeof(addr);

    //由于此结构要长期使用,所以rev必须动态分配,否则离开此函数后会自动释放,导致segment fault
    struct event* rev = (struct event*)malloc(sizeof(*rev));
    
    int s = accept(fd, &addr, &len);
    if (s == -1)
    {
        perror("accept error/n");
        return;
    }

    fprintf(stdout, "accept socket: %d/n", s);

    /* Initalize one event */
    event_set(rev, s, EV_READ, sock_read, rev);

    /* Add it to the active events, without a timeout */
    event_add(rev, NULL);

    /* Reschedule this event */
    event_add(ev, NULL);
}

int
main (int argc, char **argv)
{
    struct event ev;
    int fd;
    struct sockaddr_in addr;    
 
    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd == -1)
    {
        perror("socket error/n");
        exit(-1);
    }

    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(10000);
    addr.sin_addr.s_addr = 0;
    if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
    {
        perror("bind error/n");
        exit(-1);
    }

    if (listen(fd, 5) == -1)
    {
        perror("listen error/n");    
        exit(-1);
    }

    /* Initalize the event library */
    event_init();

    /* Initalize one event */
    event_set(&ev, fd, EV_READ, sock_accept, &ev);

    /* Add it to the active events, without a timeout */
    event_add(&ev, NULL);
    
    event_dispatch();

    return (0);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!要创建一个简单的 TCP 服务器,你可以使用 libevent 库来处理网络事件。下面是一个使用 libevent 创建 TCP 服务器的基本示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <event2/event.h> #include <event2/listener.h> void handle_client(struct bufferevent *bev, void *ctx) { char msg[1024]; size_t len = bufferevent_read(bev, msg, sizeof(msg)); msg[len] = '\0'; printf("Received message: %s\n", msg); bufferevent_write(bev, "Server received your message\n", 29); } void accept_conn_cb(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *address, int socklen, void *ctx) { struct event_base *base = evconnlistener_get_base(listener); struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); bufferevent_setcb(bev, handle_client, NULL, NULL, NULL); bufferevent_enable(bev, EV_READ | EV_WRITE); } void accept_error_cb(struct evconnlistener *listener, void *ctx) { struct event_base *base = evconnlistener_get_base(listener); int err = EVUTIL_SOCKET_ERROR(); fprintf(stderr, "Error %d: %s\n", err, evutil_socket_error_to_string(err)); event_base_loopexit(base, NULL); } int main() { struct event_base *base = event_base_new(); struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_port = htons(8080); struct evconnlistener *listener = evconnlistener_new_bind(base, accept_conn_cb, NULL, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, -1, (struct sockaddr*)&sin, sizeof(sin)); if (!listener) { perror("Failed to create listener"); return 1; } evconnlistener_set_error_cb(listener, accept_error_cb); event_base_dispatch(base); evconnlistener_free(listener); event_base_free(base); return 0; } ``` 这个示例代码会在本地的 8080 端口上创建一个 TCP 服务器。当有客户端连接时,服务器会接收客户端发送的消息,并将其打印出来。然后服务器会回复一个简单的消息给客户端。 你可以根据自己的需求修改和扩展这个示例代码。希望对你有帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值