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: ▒▒▒▒▒▒▒▒е▒▒▒▒▒▒ֹ▒▒һ▒▒▒ѽ▒▒▒▒▒▒▒▒ӡ▒ .

相关推荐
BenD-_-29 分钟前
CVE-2026-31431 Copy Fail:Linux 内核本地提权漏洞风险与缓解
linux·网络·安全
无敌的黑星星40 分钟前
Java8 CompletableFuture 实战指南
linux·前端·python
仍然.1 小时前
网络编程(二)---TCP字节流套接字编程
网络·网络协议·tcp/ip
Championship.23.242 小时前
Linux Top 命令族深度解析与实战指南
java·linux·服务器·top·linux调试
南城猿2 小时前
保姆级 Ubuntu 部署 禅道
linux·运维·ubuntu
zhangrelay2 小时前
三分钟云课实践速通--模拟电子技术-模电--SimulIDE
linux·笔记·学习·ubuntu·lubuntu
木木_王2 小时前
嵌入式Linux学习 | 数据结构 (Day05) 栈与队列详解(原理 + C 语言实现 + 实战实验 + 易错点剖析)
linux·c语言·开发语言·数据结构·笔记·学习
Ether IC Verifier2 小时前
OSI网络七层协议详细介绍
服务器·网络·网络协议·计算机网络·php·dpu
Joseph Cooper3 小时前
Linux Power Management 子系统:从 suspend/resume 到 Runtime PM、PM QoS
linux·驱动开发·linux kernel·嵌入式linux·电源管理
wj3055853783 小时前
CC-Switch 在 WSL Ubuntu 中安装记录
linux·运维·ubuntu