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