From 4db309ba545e84b1089305537aaf436004d02aab Mon Sep 17 00:00:00 2001 From: flykhan Date: Thu, 7 Sep 2023 10:55:15 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E5=AE=9E=E7=8E=B0=E5=B9=B6?= =?UTF-8?q?=E5=8F=91tcp=E9=80=9A=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day4/echo2.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 day4/echo2.c diff --git a/day4/echo2.c b/day4/echo2.c new file mode 100644 index 0000000..00f7a9c --- /dev/null +++ b/day4/echo2.c @@ -0,0 +1,112 @@ +// 线程实现并发tcp通信 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// 定义结构体:用于描述连接的客户端信息 +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; +}