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

相关推荐
biter00882 小时前
Ubuntu 22.04 有线网络时好时坏?最终解决方案
linux·网络·ubuntu
zzzsde3 小时前
【Linux】基础开发工具(3):编译器
linux·运维·服务器
Macbethad3 小时前
基于WPF的Ethernet/IP主站程序技术方案
网络协议·tcp/ip·wpf
ganshenml3 小时前
【Web】证书(SSL/TLS)与域名之间的关系:完整、通俗、可落地的讲解
前端·网络协议·ssl
愚戏师4 小时前
Python3 多线程
linux·运维·服务器·python
n***33354 小时前
linux redis简单操作
linux·运维·redis
h***59334 小时前
使用Canal将MySQL数据同步到ES(Linux)
linux·mysql·elasticsearch
xu_yule5 小时前
网络和Linux网络-5(应用层)HTTP协议(方法+报头+状态码)
linux·网络·网络协议·http
报错小能手5 小时前
TCP/UDP面经整理
网络·tcp/ip·udp
lhyzws5 小时前
CENTOS上的网络安全工具(三十二) Portainer Kafka-Clickhouse部署(1)
linux·kafka·centos