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

相关推荐
我爱学习好爱好爱1 小时前
Ansible 常用模块详解:yum、service/systemd、copy实战
linux·服务器·ansible
papaofdoudou1 小时前
LINUX VFIO被IOMMUFD取代
linux·运维·服务器
平生不喜凡桃李2 小时前
浅谈 Linux 中 namespace 相关系统调用
java·linux·服务器
YMWM_3 小时前
【问题】thor上的cubLas
linux·python·thor
杨云龙UP4 小时前
mysqldump逻辑备份文件恢复总结:全库恢复、单库恢复,一篇讲明白
linux·运维·服务器·数据库·mysql·adb
舰长1154 小时前
linux系统服务器加固1、中风险 未设置登录失败处理功能和登录连接超时处理功能。2、中风险 未限制默认账户的访问权限。3、中风险 未实现管理用户的权限分离。
linux·运维·服务器
mounter6254 小时前
Linux 7.0 重磅更新:详解 nullfs 如何重塑根文件系统挂载与内核线程隔离
linux·运维·服务器·kernel
色空大师5 小时前
【网站搭建实操(一)环境部署】
java·linux·数据库·mysql·网站搭建
A.A呐6 小时前
【Linux第十三章】缓冲区
linux·服务器
想唱rap6 小时前
Linux线程
java·linux·运维·服务器·开发语言·mysql