From f343b5a0a8b0ed252b2ba509c0420111dd330349 Mon Sep 17 00:00:00 2001 From: flykhan Date: Wed, 6 Sep 2023 18:06:03 +0800 Subject: [PATCH] =?UTF-8?q?tcp=E5=AE=A2=E6=88=B7=E7=AB=AF=EF=BC=8Ctcp?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=AB=AF=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day3/tcp_c1.c | 74 ++++++++++++++++++++++++++++++++++++++++++++ day3/tcp_s1.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 day3/tcp_c1.c create mode 100644 day3/tcp_s1.c diff --git a/day3/tcp_c1.c b/day3/tcp_c1.c new file mode 100644 index 0000000..4df5fe9 --- /dev/null +++ b/day3/tcp_c1.c @@ -0,0 +1,74 @@ +// tcp_s1 的客户端 +#include +#include +#include +#include +#include +#include // bzero +#include // atoi + +int main(int argc, char const *argv[]) +{ + if (argc < 3) + { + printf("usage: %s server_ip port\n", argv[0]); + return 1; + } + + // TCP 的 socket 套接字类型 SOCK_STREAM + int sock_fd = socket(AF_INET, SOCK_STREAM, 0); + if (sock_fd < 0) + { + perror("socket"); + return 1; + } + + // 连接服务端 + struct sockaddr_in server_addr; + bzero(&server_addr, sizeof(server_addr)); + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = inet_addr(argv[1]); + server_addr.sin_port = htons(atoi(argv[2])); + + // 连接 + if (connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) != 0) + { + perror("connect"); + close(sock_fd); + return 1; + } + + printf("connect ok\n"); + + while (1) + { + char buf[128] = ""; + scanf("%s", buf); // 从键盘取数据 + + int send_len = send(sock_fd, buf, strlen(buf), 0); + if (send_len == -1) + { + perror("send"); + break; + } + if (strncmp(buf, "bye", 3) == 0) + { + break; + } + if (strncmp(buf, "clear", 5) == 0) + { + system("clear"); + } + + memset(buf, 0, 128); + send_len = recv(sock_fd, buf, 128, 0); + if (send_len > 0) + { + printf("%s(%s): %s\n", argv[1], argv[2], buf); + } + } + + close(sock_fd); + + return 0; +} diff --git a/day3/tcp_s1.c b/day3/tcp_s1.c new file mode 100644 index 0000000..8008f50 --- /dev/null +++ b/day3/tcp_s1.c @@ -0,0 +1,86 @@ +// tcp_c1 的服务端 +#include +#include +#include +#include +#include +#include // bzero +#include // atoi + +int main(int argc, char const *argv[]) +{ + if (argc != 2) + { + printf("usage: %s port\n", argv[0]); + return 1; + } + + // TCP 的 socket 套接字类型 SOCK_STREAM + int sock_fd = socket(AF_INET, SOCK_STREAM, 0); + if (sock_fd < 0) + { + perror("socket"); + return 1; + } + + // 设置绑定:绑定服务器的IP和端口号 + struct sockaddr_in server_addr; + bzero(&server_addr, sizeof(server_addr)); + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + server_addr.sin_port = htons(atoi(argv[1])); + + bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)); + + // 创建监听队列 + if (listen(sock_fd, 50) == -1) + { + perror("listen"); + } + + // 接收客户端的连接 + 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); + + // 客户端 ip + char client_ip[16] = ""; + inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, client_ip, 16); + + printf("%s(%d) 连接成功\n", client_ip, ntohs(client_addr.sin_port)); + + while (1) + { + // 接受(读取客户端数据) + char buf[128] = ""; + ssize_t recv_len = read(client_fd, buf, 128); // 用 recv 也可以 + // ssize_t recv_len = recv(client_fd, buf, 128, 0); + + if (recv_len > 0) + { + printf("%s say: %s\n", client_ip, buf); + } + + if (strncmp(buf, "bye", 3) == 0) + { + break; + } + + memset(buf, 0, 128); + scanf("%s", buf); + // if (send(client_fd, buf, strlen(buf) + 1, 0) != -1) // 用 send 也可以 + if (write(client_fd, buf, strlen(buf) + 1) != -1) // 用 send 也可以 + { + printf("发送数据 OK\n"); + } + } + + close(client_fd); + close(sock_fd); + printf("----over----\n"); + + return 0; +}