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
相关推荐
飞奔的瓜牛5 小时前
Nginx 实现TCP与UDP转发:新版与老版本配置详解
tcp/ip·nginx·udp
Eyfcom10 小时前
〖物流 SaaS 实战 02〗SaaS 租户的计费与订阅模型:从“一锤子买卖”到“按需订阅”
linux·服务器·数据库·快递系统·物流saas实战
慧都小项10 小时前
MyEclipse Inline Diff 实用指南:逐处核对 AI 修改再提交
git·ai编程·myeclipse·java 代码审查·ai 代码修改
忆挽篱笙歌12 小时前
systemV进程通信
linux·运维·服务器
德育处主任13 小时前
让 Claude Code 在你睡觉时接着干活:tmux、权限策略与验收清单
git·ai编程·命令行
无奈笑天下17 小时前
银河麒麟桌面操作系统上磁盘无法自动挂载问题处理
linux·运维·服务器
木头左18 小时前
【Linux运维】服务器磁盘清理与Docker日志治理实战:一次巡检释放70G空间
linux·docker·磁盘清理·日志治理
星恒随风19 小时前
MySQL 从零入门(七):索引原理与 B+ 树详解——从磁盘 I/O 到 InnoDB 聚簇索引
linux·服务器·数据库·笔记·mysql
多弗朗皮卡丘1 天前
网络基础:数据链路层与其他协议或技术
linux·网络·tcp/ip