qfedu-network-advanced-level/day4/echo2.c

113 lines
2.7 KiB
C

// 线程实现并发tcp通信
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <pthread.h>
// 定义结构体:用于描述连接的客户端信息
typedef struct client_s
{
unsigned char ip[INET_ADDRSTRLEN]; // 16
int fd;
} Client;
void *client_task(void *arg)
{
Client *client = (Client *)arg;
while (1)
{
char buf[128] = "";
ssize_t len = recv(client->fd, buf, 128, 0);
if (len > 0)
{
int i = 0;
while (buf[i])
{
buf[i] = toupper(buf[i]);
i++;
}
send(client->fd, buf, len, 0);
if (strncmp(buf, "BYE", 3) == 0)
{
break;
}
}
}
close(client->fd);
// pthread_exit(NULL); // 结束线程
printf("%s 关闭连接\n", client->ip);
free(client); // 回收空间
}
int main(int argc, char const *argv[])
{
// 1. 创建 socket
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd < 0)
{
perror("socket");
return 1;
}
// 2. bind
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[1]));
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int flag = bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (flag != 0)
{
perror("bind");
close(sock_fd);
return 1;
}
// 3. 创建监听队列
listen(sock_fd, 100);
printf("-----ECHO 服务器已开启端口号为:%s-----\n", argv[1]);
// 4. 开始接收客户端的连接(并发接收多个客户端)
while (1)
{
struct sockaddr_in client_addr;
bzero(&client_addr, sizeof(client_addr));
socklen_t client_addr_len = sizeof(client_addr);
int client_fd = accept(sock_fd, (struct sockaddr *)&client_addr, &client_addr_len);
char clinet_ip[INET_ADDRSTRLEN] = "";
inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, clinet_ip, INET_ADDRSTRLEN);
printf("%s 连接成功\n", clinet_ip);
// 创建线程实现并发通信
Client *client = malloc(sizeof(Client)); // 创建堆空间
strcpy(client->ip, clinet_ip);
client->fd = client_fd;
pthread_t tid;
pthread_create(&tid, NULL, client_task, client);
pthread_detach(tid); // 分离线程
// pthread_join(tid,NULL);
}
// 主进程的范畴
close(sock_fd);
return 0;
}