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
相关推荐
东华果汁哥16 分钟前
【linux 免密登录】快速设置kafka01、kafka02、kafka03 三台机器免密登录
linux·运维·服务器
咖喱鱼蛋38 分钟前
Ubuntu安装Electron环境
linux·ubuntu·electron
ac.char42 分钟前
在 Ubuntu 系统上安装 npm 环境以及 nvm(Node Version Manager)
linux·ubuntu·npm
肖永威1 小时前
CentOS环境上离线安装python3及相关包
linux·运维·机器学习·centos
tian2kong1 小时前
Centos 7 修改YUM镜像源地址为阿里云镜像地址
linux·阿里云·centos
布鲁格若门1 小时前
CentOS 7 桌面版安装 cuda 12.4
linux·运维·centos·cuda
C-cat.1 小时前
Linux|进程程序替换
linux·服务器·microsoft
dessler1 小时前
云计算&虚拟化-kvm-扩缩容cpu
linux·运维·云计算
怀澈1221 小时前
高性能服务器模型之Reactor(单线程版本)
linux·服务器·网络·c++
DC_BLOG1 小时前
Linux-Apache静态资源
linux·运维·apache