C语言实现Linux下TCP Server测试工具

Linux TCP Server测试工具代码

实现了接受数据并输出文本和十六制字符串

c 复制代码
#include <stdio.h>
#include<string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
 
#define SERVER_PORT		8081
#define SERVER_IP		"0.0.0.0"
void ByteToHexStr(const unsigned char* source, char* dest, int sourceLen); 
int listen_fd = -1;
 
void signal_handler(int arg)
{
	printf("close listen_fd(signal = %d)\n", arg);
	close(listen_fd);
	exit(0);
}
 
int main(int argc,  char *argv[])
{
        int port=80;
        printf("argc:%d\r\n",argc);
        if (argc>1)
        {
            printf("raw data:%s",argv[1]);
            port=atoi(argv[1]);
            printf("port:%d\r\n",port);
        }
	int new_fd  = -1;
	struct sockaddr_in server;
	struct sockaddr_in client;
	socklen_t saddrlen = sizeof(server);
	socklen_t caddrlen = sizeof(client);
 
	signal(SIGINT, signal_handler);
 
	memset(&server, 0, sizeof(server));
	memset(&client, 0, sizeof(client));
 
	listen_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (listen_fd < 0)
	{
		printf("socket error!\n");
		return -1;
	}
        int opt = 1;
        setsockopt(listen_fd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof( opt ));
 
	server.sin_family = AF_INET;
	server.sin_port = htons(port);
	server.sin_addr.s_addr = inet_addr(SERVER_IP);
 
	if (bind(listen_fd, (struct sockaddr *)&server, saddrlen) < 0)
	{
		printf("bind error!\n");
		return -1;
	}
        printf("listen:%d\r\n",port); 
	if (listen(listen_fd, 5) < 0)
	{
		printf("listen error!\n");
		return -1;
	}
 
	char rbuf[256] = {0};
        char hexStr[1024]={0};
	int read_size = 0;
	while (1)
	{
		/*
		socket()创建的套接字默认是阻塞的,所以accept()在该套接字上进行监听时,
		如果没有客户端连接请求过来,accept()函数会一直阻塞等待;换句话说,程序
		就停在accept()函数这里,不会继续往下执行,直到有新的连接请求发送过来,唤醒accept()。
		*/
		new_fd = accept(listen_fd, (struct sockaddr *)&client, &caddrlen);
		if (new_fd < 0)
		{
			perror("accept");
			return -1;
		}
 
		printf("new client connected.IP:%s,port:%u\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
		while (1)
		{
                        memset(&rbuf,0,sizeof(rbuf));
			read_size = read(new_fd, rbuf, sizeof(rbuf));
			if (read_size < 0)
			{
				printf("read error!\n");
				continue;
			}
			else if (read_size == 0)
			{
				printf("client (%d) is closed!\n", new_fd);
				close(new_fd);
				break;
			}
 
			printf("recv:%s\n", rbuf);
                        memset(&hexStr, 0, sizeof(hexStr));
                        ByteToHexStr(rbuf,hexStr,read_size);
                        printf("recv HexStr:%s\n",hexStr);
		}
	}
 
	close(listen_fd);
 
	return 0;
}
void ByteToHexStr(const unsigned char* source, char* dest, int sourceLen)
{
    short i;
    unsigned char highByte, lowByte;
 
    for (i = 0; i < sourceLen; i++)
    {
        highByte = source[i] >> 4;
        lowByte = source[i] & 0x0f;
 
        highByte += 0x30;
 
        if (highByte > 0x39)
            dest[i * 2] = highByte + 0x07;
        else
            dest[i * 2] = highByte;
 
        lowByte += 0x30;
        if (lowByte > 0x39)
            dest[i * 2 + 1] = lowByte + 0x07;
        else
            dest[i * 2 + 1] = lowByte;
    }
    return;
}

编译

bash 复制代码
# 编译
 gcc tcpserver.c -o tcpserver
# 运行
./tcpserver 8081
相关推荐
Gaoithe19 分钟前
ubuntu 端口复用
linux·运维·ubuntu
德先生&赛先生1 小时前
Linux编程:1、文件编程
linux
程序猿小D1 小时前
第16节 Node.js 文件系统
linux·服务器·前端·node.js·编辑器·vim
多多*2 小时前
微服务网关SpringCloudGateway+SaToken鉴权
linux·开发语言·redis·python·sql·log4j·bootstrap
光芒Shine3 小时前
【物联网-TCP/IP】
网络·网络协议·tcp/ip
IT界小黑的对象4 小时前
virtualBox部署ubuntu22.04虚拟机 NAT+host only 宿主机ping不通虚拟机
linux·运维·服务器
SilentCodeY4 小时前
Ubuntu 系统通过防火墙管控 Docker 容器
linux·安全·ubuntu·系统防火墙
weixin_527550404 小时前
Linux 环境下高效视频切帧的实用指南
linux·运维·音视频
keson要进步4 小时前
CICD实战(二)-----gitlab的安装与配置
linux·运维·gitlab
藥瓿亭5 小时前
K8S认证|CKS题库+答案| 4. RBAC - RoleBinding
linux·运维·服务器·云原生·容器·kubernetes·cks