libevent TCP echo

uppercase.c

cpp 复制代码
/*
 * sudo apt-get install libevent-dev  # libevent-devel for yum
 * Server:
 * cc -g uppercase.c -levent
 * ./a.out
 * Client:
 * $ ncat 192.168.0.107 9995
 * hello, world
 * HELLO, WORLD
 */
#include <stdlib.h> /* free */
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <signal.h>
#ifndef _WIN32
#include <netinet/in.h>
# ifdef _XOPEN_SOURCE_EXTENDED
#  include <arpa/inet.h>
# endif
#include <sys/socket.h>
#endif

#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/listener.h>
#include <event2/util.h>
#include <event2/event.h>

#define PORT 9995
#define BUFFER_SIZE 1024

static void listener_cb(struct evconnlistener *, evutil_socket_t,
    struct sockaddr *, int socklen, void *);
static void conn_readcb(struct bufferevent *, void *);
static void conn_eventcb(struct bufferevent *, short, void *);
static void signal_cb(evutil_socket_t, short, void *);

int main(int argc, char **argv) {
    struct event_base *base;
    struct evconnlistener *listener;
    struct event *signal_event;
    struct sockaddr_in sin = {0};

#ifdef _WIN32
    WSADATA wsa_data;
    WSAStartup(0x0201, &wsa_data);
#endif

    base = event_base_new();
    if (!base) {
        fprintf(stderr, "Could not initialize libevent!\n");
        return 1;
    }

    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);

    listener = evconnlistener_new_bind(base, listener_cb, (void *)base,
        LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, -1,
        (struct sockaddr*)&sin,
        sizeof(sin));

    if (!listener) {
        fprintf(stderr, "Could not create a listener!\n");
        return 1;
    }

    signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);

    if (!signal_event || event_add(signal_event, NULL) < 0) {
        fprintf(stderr, "Could not create/add a signal event!\n");
        return 1;
    }

    event_base_dispatch(base);

    evconnlistener_free(listener);
    event_free(signal_event);
    event_base_free(base);

    printf("Server shut down.\n");
    return 0;
}

static void listener_cb(struct evconnlistener *listener, evutil_socket_t fd,
    struct sockaddr *sa, int socklen, void *user_data) {
    struct event_base *base = user_data;
    struct bufferevent *bev;

    bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
    if (!bev) {
        fprintf(stderr, "Error constructing bufferevent!\n");
        event_base_loopbreak(base);
        return;
    }
    bufferevent_setcb(bev, conn_readcb, NULL, conn_eventcb, NULL);
    bufferevent_enable(bev, EV_READ | EV_WRITE);
}

static void conn_readcb(struct bufferevent *bev, void *user_data) {
    struct evbuffer *input = bufferevent_get_input(bev);
    struct evbuffer *output = bufferevent_get_output(bev);
    char *line;
    size_t n, i;

    while ((line = evbuffer_readln(input, &n, EVBUFFER_EOL_LF)) != NULL) {
		if (strncmp(line, "exit", 4)==0 || (strncmp(line, "quit", 4)==0)) {
			free(line);
			bufferevent_free(bev);
			return;
		}
        for (i = 0; i < n; i++) {
            line[i] = toupper((unsigned char)line[i]);
        }
        evbuffer_add(output, line, n);
        evbuffer_add(output, "\n", 1);  /* Add newline for proper response */
        free(line);
    }
}

static void conn_eventcb(struct bufferevent *bev, short events, void *user_data) {
    if (events & BEV_EVENT_EOF) {
        printf("Connection closed.\n");
    } else if (events & BEV_EVENT_ERROR) {
        printf("Got an error on the connection: %s\n", strerror(errno));
    }
    bufferevent_free(bev);
}

static void signal_cb(evutil_socket_t sig, short events, void *user_data) {
    struct event_base *base = user_data;
    struct timeval delay = { 0, 10 };

    printf("Caught an interrupt signal; exiting cleanly in 10ms.\n");
    event_base_loopexit(base, &delay);
}

mzh@raspberrypi:~/workspace $ cc -g uppercase.c -levent

mzh@raspberrypi:~/workspace $ ./a.out

mzh@DESKTOP-GITL67P MINGW64 ~

$ ncat 192.168.0.107 9995

libnsock ssl_init_helper(): OpenSSL legacy provider failed to load.

hello,world

HELLO,WORLD

zjrcu96592

ZJRCU96592

quit

Ncat: ▒▒▒▒▒▒▒▒е▒▒▒▒▒▒ֹ▒▒һ▒▒▒ѽ▒▒▒▒▒▒▒▒ӡ▒ .

相关推荐
IMPYLH40 分钟前
Linux 的 wc 命令
linux·运维·服务器·前端·bash
zxy6444924731 小时前
Centos7.9编译安装PHP7.4
linux·运维·服务器
无限进步_1 小时前
【Linux】从冯诺依曼到操作系统:理解计算机运行的基本脉络
linux·运维·服务器
happybasic1 小时前
Python库升级标准流程~
linux·前端·python
Rabbit_QL1 小时前
【ln -s】Linux 软链接在大模型部署中的应用
linux·运维·服务器
IP搭子来一个2 小时前
舆情监控系统怎么接入代理 IP?多平台多账号采集的配置全流程
网络协议·tcp/ip·数据分析
坤昱2 小时前
cfs调度类深入解刨——核心结构细节分析
linux·cfs调度·eevdf调度·linux调度·linux技术
枳实-叶2 小时前
【Linux驱动开发】第12天:Linux设备树核心:树形结构+节点+属性 完整全解
linux·运维·驱动开发
Yeats_Liao2 小时前
物联网接入层技术剖析(三):epoll在JVM中的映射
java·linux·jvm·人工智能·物联网
小贾要学习2 小时前
【Linux】基于自定义TCP协议的日期计算器
linux·网络·c++·网络协议·tcp/ip