From f61ee4457bf794c16edb0644338ef636c3cef538 Mon Sep 17 00:00:00 2001 From: yanfeizhang Date: Sun, 6 Feb 2022 14:31:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E5=A4=8D=E7=94=A8=E6=B5=8B=E8=AF=95=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/network/test08/Makefile | 22 ++++++++++ tests/network/test08/client.c | 73 +++++++++++++++++++++++++++++++++ tests/network/test08/server.c | 77 +++++++++++++++++++++++++++++++++++ tests/network/test08/tool.sh | 20 +++++++++ 4 files changed, 192 insertions(+) create mode 100644 tests/network/test08/Makefile create mode 100644 tests/network/test08/client.c create mode 100644 tests/network/test08/server.c create mode 100644 tests/network/test08/tool.sh diff --git a/tests/network/test08/Makefile b/tests/network/test08/Makefile new file mode 100644 index 0000000..a843db1 --- /dev/null +++ b/tests/network/test08/Makefile @@ -0,0 +1,22 @@ +SERVER_BINARY_NAME := "test-server" +CLIENT_BINARY_NAME := "test-client" + +.PHONY: build-server +build-server: + gcc server.c -o $(SERVER_BINARY_NAME) + +.PHONY: run-server +run-server: + sh tool.sh runsrv ./$(SERVER_BINARY_NAME) + +.PHONY: stop-server +stop-server: + sh tool.sh stopsrv ./$(SERVER_BINARY_NAME) + +.PHONY: build-client +build-client: + gcc client.c -o $(CLIENT_BINARY_NAME) + +.PHONY: run-client +run-client: + ./$(CLIENT_BINARY_NAME) 0.0.0.0 6000 \ No newline at end of file diff --git a/tests/network/test08/client.c b/tests/network/test08/client.c new file mode 100644 index 0000000..aac58eb --- /dev/null +++ b/tests/network/test08/client.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include + +#define MAX_CONNECTION_NUM 100 + +int buildConnect(const char *sIp, int sPort) +{ + int skFd; + if((skFd = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + printf("\n Error : Could not create socket\n"); + return 0; + } + + struct sockaddr_in srvAddr; + srvAddr.sin_family = AF_INET; + srvAddr.sin_addr.s_addr = inet_addr(sIp); + srvAddr.sin_port = htons(sPort); + if(connect(skFd, (struct sockaddr *)&srvAddr, sizeof(srvAddr)) < 0) + { + printf("\n Error : Connect Failed \n"); + return 0; + } + + return skFd; +} + +int main(int argc, char *argv[]) +{ + int i = 0, sPort, fd; + char sIp[16]; + + if(argc != 3) + { + printf("\n Usage: %s \n", argv[0]); + return 1; + } + + //1. 从命令行获取并解析local ip、server ip以及端口 + strcpy(sIp, argv[1]); + sPort = atoi(argv[2]); + + //2. 开始建立连接 + int *sockets = (int *)malloc(sizeof(int) * MAX_CONNECTION_NUM); + for(i = 1; i <= MAX_CONNECTION_NUM; i++) + { + + fd = buildConnect(sIp, sPort); + if(fd > 0) + { + sockets[i-1] = fd; + printf("连接 %s:%d成功了 %d 条!\n", sIp, sPort, i); + } + else + { + return 1; + } + } + sleep(300); + + //3. 释放所有的连接 + printf("关闭所有的连接...\n"); + for(i = 0; i < MAX_CONNECTION_NUM; i++) + { + close(sockets[i]); + } + + return 0; +} diff --git a/tests/network/test08/server.c b/tests/network/test08/server.c new file mode 100644 index 0000000..9bbf94f --- /dev/null +++ b/tests/network/test08/server.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include + +#define MAX_CONNECTION_NUM 1100000 + +int main(int argc, char *argv[]) +{ + char ip[16]; + int lisFd, conFd, port; + struct sockaddr_in servAddr; + pid_t pid; + + + if(argc != 3) + { + printf("\n Usage: %s \n", argv[0]); + return 1; + } + + pid = getpid(); + + //1. 从命令行获取并解析server ip以及端口 + strcpy(ip, argv[1]); + port = atoi(argv[2]); + + //2. 启动服务 + //2.1 创建server socket + if((lisFd = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + printf("Server %d Error : Could not create socket!\n", pid); + return 0; + } + + //2.2 设置端口重用 + int val =1; + if (setsockopt(lisFd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val))<0) + { + printf("\n Server %d Error : setsockopt failed!\n", pid); + return 0; + } + + //2.3 bind绑定 + servAddr.sin_family = AF_INET; + servAddr.sin_addr.s_addr = inet_addr(ip); + servAddr.sin_port = htons(port); + if(bind(lisFd, (struct sockaddr*)&servAddr, sizeof(servAddr)) < 0) + { + printf("\n Server %d Error : Bind Failed! \n", pid); + return 0; + } + + //2.4 启动监听 + if((listen(lisFd, 1024)) < 0) + { + printf("\n Server %d Error : Listen Failed \n", pid); + return 0; + } + else + { + printf("\n Start server on %s:%d successed, pid is %d\n", ip, port, pid); + } + + //3. 接收连接 + int i = 0; + int *sockets = (int *)malloc(sizeof(int) * MAX_CONNECTION_NUM); + while(1) + { + conFd = accept(lisFd, (struct sockaddr*)NULL, NULL); + if(conFd > 0) + { + sockets[i++] = conFd; + printf("Server %s %d (%d) accept success:%d\n", ip, port, pid, i); + } + } +} \ No newline at end of file diff --git a/tests/network/test08/tool.sh b/tests/network/test08/tool.sh new file mode 100644 index 0000000..bf0420a --- /dev/null +++ b/tests/network/test08/tool.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +exec_runsrv(){ + for((i=1;i<=5;i++)); do + echo $SERVER 0.0.0.0 6000 & + $SERVER 0.0.0.0 6000 & + done + +} + +exec_stopsrv(){ + ps -ef | grep $SERVER | awk '{print $2}' | xargs kill -9 > /dev/null +} + +TYPE=$1 +case $TYPE in + "runsrv") SERVER=$2; exec_runsrv;; + "stopsrv") SERVER=$2; exec_stopsrv;; + *) echo "get unkown type $TYPE"; exit ;; +esac