From 1b4be013ebe02f072b0ad2713ef952ec833c856c Mon Sep 17 00:00:00 2001 From: flykhan Date: Mon, 11 Sep 2023 09:48:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF-=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E6=B6=88=E6=81=AF=E4=BC=A0=E9=80=92=EF=BC=9A=E6=B5=8B?= =?UTF-8?q?=E8=AF=951?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day5/weak_task/da.c | 57 +++++++++++++++++++++++++++++++++++ day5/weak_task/db.c | 73 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 day5/weak_task/da.c create mode 100644 day5/weak_task/db.c diff --git a/day5/weak_task/da.c b/day5/weak_task/da.c new file mode 100644 index 0000000..e551056 --- /dev/null +++ b/day5/weak_task/da.c @@ -0,0 +1,57 @@ +// a->客户端 +#include +#include +#include +#include +#include +#include + +int main(int argc, char const *argv[]) +{ + int sock_fd = socket(AF_INET, SOCK_DGRAM, 0); + if (sock_fd < 0) + { + perror("socket"); + return -1; + } + + struct sockaddr_in addr; + bzero(&addr, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(8000); + addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // 指定服务器地址 + + while (1) + { + // 创建发送的数据 + int n = 5; + printf("请输入内容:\n"); + scanf("%d", &n); + + // 发送 udp 数据 + ssize_t sendnum = sendto(sock_fd, &n, sizeof(n), 0, (struct sockaddr *)&addr, sizeof(addr)); // 将 buf 数据发送给服务器 + if (sendnum > 0) + { + perror("发送成功\n"); + } + + socklen_t addrlen = sizeof(addr); + + char recvbuf[128] = ""; + + ssize_t recvlen = recvfrom(sock_fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&addr, &addrlen); // 从服务器获取数据到 buf + if (recvlen < 0) + { + perror("recvfrom"); + return 1; + } + + printf("从服务器获取内容为 %s\n", recvbuf); + if (strncmp(recvbuf, "bye", 3) == 0) + break; + } + + close(sock_fd); + + return 0; +} diff --git a/day5/weak_task/db.c b/day5/weak_task/db.c new file mode 100644 index 0000000..d021ede --- /dev/null +++ b/day5/weak_task/db.c @@ -0,0 +1,73 @@ +// b->服务端 +#include +#include +#include +#include +#include +#include + +int main(int argc, char const *argv[]) +{ + int sock_fd = socket(AF_INET, SOCK_DGRAM, 0); + if (sock_fd < 0) + { + perror("socket"); + return -1; + } + + struct sockaddr_in addr; + bzero(&addr, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(8000); + addr.sin_addr.s_addr = htonl(INADDR_ANY); + + if (bind(sock_fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) + { + perror("bind"); + } + else + { + printf("bind success\n"); + + struct sockaddr_in src_addr; + int n = 0; // 接收数的空间 + socklen_t socklen = sizeof(src_addr); + while (1) + { + bzero(&src_addr, sizeof(src_addr)); + + // 接收数据,如果数据未到达,则会阻塞,直到数据到达为止 + ssize_t len = recvfrom(sock_fd, &n, sizeof(n), 0, (struct sockaddr *)&src_addr, &socklen); + if (len < 0) + { + perror("recvfrom"); + break; + } + + char srcIP[INET_ADDRSTRLEN]; // 16 + int port = ntohs(src_addr.sin_port); // 将网络字节序转化为本地字节序 + inet_ntop(AF_INET, &src_addr.sin_addr.s_addr, srcIP, INET_ADDRSTRLEN); // 将网络数字转换为点分十进制 ip + + printf("从 %s:%d 位置接收到的数字为(%lu Bytes): %d\n", srcIP, port, len, n); + + + char sendbuf[128] = ""; + + if (n == 1) + strcpy(sendbuf, "disen666"); + else if (n == 2) + strcpy(sendbuf, "jack good"); + else if (n == 3) + strcpy(sendbuf, "disen888"); + else if (n == 4) + strcpy(sendbuf, "bye"); + + sendto(sock_fd, &sendbuf, strlen(sendbuf), 0, (struct sockaddr *)&src_addr, socklen); + printf("发送回去内容为 %s\n", sendbuf); + } + } + + close(sock_fd); + + return 0; +}