an easy implementation of a multi-process tcp server and a multi-thread tcp client

Overview

一个TCP多进程服务器-多线程客户端的简单实现。

客户端类似Apache ab的测试功能,能够通过向某一个ip端口发送指定并发量和总数量的tcp短连接;服务端处理tcp短连接,每来一条消息就打印一条log。

使用cmake编译,建议在vscode里编译,或者命令行

# 终端进入目录
mkdir build
cd build
cmake ../
make

服务器

1. 基本思路

Linux系统下创建进程的开销相比创建线程并不会高太多,在多核设备上可以发挥并行优势,避免线程切换的开销,著名的nginx服务器也采用了多进程异步IO的架构。这里使用主进程监听端口并打开要存储的文件句柄,子进程accept处理任务(写log)的架构,子进程通过write写入文件。

2. 优化手段

  • 默认选取当前系统的可用核心数量作为子进程数量,也可以通过命令行指定子进程数量

  • 将listen函数的backlog设置较大,这里选取了1024

  • 尝试了在每个子进程内用epoll提高处理并发请求的能力,但最终在QPS和P90性能上没有体现出明显的提升,考虑原因可能是因为没有较好的处理惊群效应,目前处理惊群的办法是让没有accept到socket的进程忽略错误返回值,等待下一次监听socket就绪,这样造成了一定的资源浪费。

3. 运行方式

使用cmake进行编译

usage: ./build/multiprocess_server [options] ... 
options:
  -p, --port       port (int [=8081])
  -w, --worker     message to be sent (int [=16])
  -l, --logpath    path of logfile (string [=tcp_log.txt])
  -?, --help       print this message

客户端

1. 基本思路

使用多线程进行客户端并行发送,所有的线程共同维护全局互斥量:待发送消息总数,每次发送前将该变量-1,若发送失败则+1。发送成功后统计该次发送时间,待发送消息总数为0后统计QPS等性能参数。

2. 优化手段

  • 使用atomic变量代替互斥锁提升效率,理论上比加锁的资源消耗会少很多,但测试发现提升不明显

  • 使用全局vector作为存储响应时间的容器,发送前提前分配空间,使用当前待发送消息数作为vector索引,减少运行时扩容的消耗。

3. 运行方式

用法

usage: ./build/multithread_client --conttr                 currency=unsigned int --number=long long [options] ... 
options:
  -c, --concurrency    number of concurrency (unsigned int)
  -n, --number         total number of tcp messages (long long)
  -s, --string         message to be sent (string [=hello, tcp!])
  -p, --port           port (int [=8081])
  -a, --address        server address (string [=127.0.0.1])
  -?, --help           print this message

You might also like...
Triton Python and C++ client libraries and example, and client examples for go, java and scala.

Triton Client Libraries and Examples To simplify communication with Triton, the Triton project provides several client libraries and examples of how t

Provide translation, currency conversion, and voting services. First using telnet you create a connection to a TCP socket, then the server connects to 3 UDP sockets hosted on other servers to do tasks.

to run micro servers g++ translator.cpp -o translator ./translator port 1 g++ voting.cpp -o voting ./voting port 2 g++ currency_converter.cpp -o c

Un petit client/serveur TCP pour transférer d'image en C

MIMAGE Project Guide de démarage Prérequis Être sur un environement Linux et avoir le compilateur GCC et makefile d'installé. Installation Pour build

High performant TCP server for rtl-sdr
High performant TCP server for rtl-sdr

About Key features Share available RF bandwidth between several independent clients: Total bandwidth can be 2016000 samples/sec at 436,600,000 hz One

A barebones TCP server framework in POSIX C

TCP Server Framework A barebones TCP Server Framework in POSIX C; removes boilerplate code and uses loop paradigm. How To Use copy files into project

IRC server based on TCP/IP protocol to rfc1459 standard

IRC-server В далеком 1998 году, в сети DALnet, на одном популярном тогда русскоязычном канале, на вопрос новичка «А что такое IRC?», один человек отве

Boilerplate of a TCP Server written in C++ using the epoll API
Boilerplate of a TCP Server written in C++ using the epoll API

tcp-server-boilerplate Boilerplate of a TCP Server written in C++ using the epoll API. Requirements cmake (if you want to use the provided build.sh sc

A very simple, fast, multithreaded, platform independent HTTP and HTTPS server and client library implemented using C++11 and Boost.Asio.

A very simple, fast, multithreaded, platform independent HTTP and HTTPS server and client library implemented using C++11 and Boost.Asio. Created to be an easy way to make REST resources available from C++ applications.

Simple server and client using python socket and declarative programming

Socket-programming Simple server and client using python socket and declarative programming How to use? open cmd and navigate to the location of the s

Owner
adin
adin
Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution

CppServer Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and

Ivan Shynkarenka 931 Nov 19, 2022
Built a client-server application using TCP and UDP sockets, in which the clients can subscribe/unsubscribe to various topics.

Built a client-server application using TCP and UDP sockets, in which the clients can subscribe/unsubscribe to various topics.

null 1 Jun 22, 2022
Ole Christian Eidheim 741 Nov 2, 2022
Simple Tcp Chat Client / Server written in C++

What this TcpXat TcpXat Is A Simple Tcp Chat Setup TcpXat sudo apt install make;make client;make serv;echo "done by FuryM3m0ry"; How To Run TcpXat Ter

sami 1 Dec 28, 2021
Windows named pipe server that forwards connections to given TCP server

PipeTcp An asynchronous Windows named pipe server that forwards connections to given TCP server. Pre-built binaries can be found in Releases. Invocati

Jinoh Kang 5 Nov 3, 2022
Webdav-client-cpp - C++ WebDAV Client provides easy and convenient to work with WebDAV-servers.

WebDAV Client Package WebDAV Client provides easy and convenient to work with WebDAV-servers: Yandex.Disk Dropbox Google Drive Box 4shared ownCloud ..

Cloud Polis 102 Oct 1, 2022
Mongoose Embedded Web Server Library - a multi-protocol embedded networking library with TCP/UDP, HTTP, WebSocket, MQTT built-in protocols, async DNS resolver, and non-blocking API.

Mongoose - Embedded Web Server / Embedded Networking Library Mongoose is a networking library for C/C++. It implements event-driven non-blocking APIs

Cesanta Software 8.9k Nov 21, 2022
mvfst is a client and server implementation of IETF QUIC protocol in C++ by Facebook.

mvfst (Pronounced move fast) is a client and server implementation of IETF QUIC protocol in C++ by Facebook.

Meta Incubator 1.2k Nov 25, 2022
LAppS - Lua Application Server for micro-services with default communication over WebSockets. The fastest and most vertically scalable WebSockets server implementation ever. Low latency C++ <-> Lua stack roundtrip.

LAppS - Lua Application Server This is an attempt to provide very easy to use Lua Application Server working over WebSockets protocol (RFC 6455). LApp

null 48 Oct 13, 2022
C and C++ client for QuestDB Input Line Protocol over TCP

c-questdb-client QuestDB - InfluxDB Line Protocol - Ingestion Client Library for C and C++ This library makes it easy to insert data into QuestDB. Thi

QuestDB 25 Nov 18, 2022