From c22def151d5e1615f59df3883de223a6410416bd Mon Sep 17 00:00:00 2001 From: han muyi <2392902608@qq.com> Date: Thu, 16 Sep 2021 09:55:16 +0800 Subject: [PATCH 1/2] 9.15 --- homework/hanmuyi06181088/server | 1 + 1 file changed, 1 insertion(+) create mode 160000 homework/hanmuyi06181088/server diff --git a/homework/hanmuyi06181088/server b/homework/hanmuyi06181088/server new file mode 160000 index 0000000..56bfddd --- /dev/null +++ b/homework/hanmuyi06181088/server @@ -0,0 +1 @@ +Subproject commit 56bfddd069d13e963fb8b157ede70c433676b7c9 -- Gitee From 0154f3efd1aeeefbcd54a32290461a866d27a975 Mon Sep 17 00:00:00 2001 From: han muyi <2392902608@qq.com> Date: Thu, 16 Sep 2021 10:07:14 +0800 Subject: [PATCH 2/2] 9.15 --- homework/hanmuyi06181088/server | 1 - .../hanmuyi06181088/server/CMakeLists.txt | 5 ++ homework/hanmuyi06181088/server/main.c | 77 +++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) delete mode 160000 homework/hanmuyi06181088/server create mode 100644 homework/hanmuyi06181088/server/CMakeLists.txt create mode 100644 homework/hanmuyi06181088/server/main.c diff --git a/homework/hanmuyi06181088/server b/homework/hanmuyi06181088/server deleted file mode 160000 index 56bfddd..0000000 --- a/homework/hanmuyi06181088/server +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 56bfddd069d13e963fb8b157ede70c433676b7c9 diff --git a/homework/hanmuyi06181088/server/CMakeLists.txt b/homework/hanmuyi06181088/server/CMakeLists.txt new file mode 100644 index 0000000..af156f4 --- /dev/null +++ b/homework/hanmuyi06181088/server/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.5) + +project(server LANGUAGES C) + +add_executable(server main.c) diff --git a/homework/hanmuyi06181088/server/main.c b/homework/hanmuyi06181088/server/main.c new file mode 100644 index 0000000..4adab05 --- /dev/null +++ b/homework/hanmuyi06181088/server/main.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include + +int main() +{ + //创建监听套接字 + int listenfd = socket(PF_INET, SOCK_STREAM, 0); + + //本程序的IP地址和端口号 + struct sockaddr_in addr; + addr.sin_family = AF_INET; //IPv4地址(地址类型) + addr.sin_port = htons(80); //端口号(网络字节序),程序使用小于1024的端口号需要使用root权限 + addr.sin_addr.s_addr = htonl(INADDR_ANY); //绑定本机所有IP地址 + //addr.sin_addr.s_addr = inet_addr("192.168.1.175"); //指定IP地址进行监听 + + //将IP地址与监听套接字绑定 + int error = bind(listenfd, (struct sockaddr*)&addr, sizeof(addr)); + if (error) + { + //打印错误原因 + perror("bind"); + return 1; + } + + error = listen(listenfd, 3); + if (error) + { + perror("listen"); + return 2; + } + + //忽略SIGPIPE信号,防止发送数据时被OS终止 + signal(SIGPIPE, SIG_IGN); + + //循环服务器 + while(1) //等待下一个客户端连接 + { + struct sockaddr_in client_addr; + socklen_t addrlen = sizeof(client_addr); + //等待客户端连接 + int connectfd = accept(listenfd, (struct sockaddr*)&client_addr, &addrlen); + + printf("client is connected, ip: %s, port: %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); + + //使用标准IO函数与客户端通信,需要将连接套接字与标准IO文件流绑定 + FILE* fp = fdopen(connectfd, "r+"); + + char line[80]; + //循环接收客户端的输入 + while (1) + { + + while (fgets(line, sizeof(line), fp)) + { + if(line[0] == '\r') + { + break; + } + } + + fprintf(fp, "HTTP/1.1 200 OK\r\n"); + fprintf(fp, "Content-Type: test/plain;charset=utf-8\r\n"); + fprintf(fp, "Content-Length: 12\r\n"); + fprintf(fp, "\r\n"); + fprintf(fp, "Hello world!"); + } + + //断开和客户端的TCP连接 + fclose(fp); + } + return 0; +} -- Gitee