mirror of
https://github.com/riba2534/TCP-IP-NetworkNote.git
synced 2026-06-30 01:46:15 +08:00
通过多智能体工作流对 19 章笔记(README.md)与 96 个 .c 示例代码做深度 审查与对抗性验证,修复 317 处确认问题,涵盖: 技术正确性: - 修复缓冲区溢出:echo_mpserv.c / echo_storeserv.c 等的 read(buf, BUFSIZ) 改为 BUF_SIZE(buf 仅 30 字节,BUFSIZ 远大于此) - 修复 open() 缺少 mode 参数:low_open.c / fd_seri.c / desto.c 等 O_CREAT 调用补 0644(原导致 low_read 链路失败) - 修复 feof 循环 off-by-one:news_sender.c / echo_stdserv.c 改用 fgets 返回值判断 - 修复线程竞态:chat_server.c / webserv_linux.c 的 &clnt_sock 栈地址 传子线程改为 malloc 分配 + free - 修复索引混淆:char_EPLTserv.c 错用 clnt_sock 查找改为 ep_events[i].data.fd - 修复格式化符:thread4.c 的 sizeof 用 %d 改为 %zu - 修正习题答案:ch01 fd 序号、ch13 MSG_OOB 加粗项、ch09 Nagle 等 文档规范: - 统一术语:IPv4/IPv6、接收(receive)/连接(connection) - 修正错别字:occured→occurred、cooffee→coffee、Usgae→Usage、 eerror→error、proess→process 等 - 修复病句、补全习题答案解释 - GitHub 绝对 URL 改为相对路径,统一项目引用规范 - 同步根 README.md(前言 + 19 章合并) 另:重命名 ch10/remove_zomebie.c → remove_zombie.c(修正拼写) 所有 .c 文件经 gcc 编译验证通过(ch17 epoll 文件因 macOS 无 sys/epoll.h 跳过,已人工复核)。
159 lines
4.1 KiB
C
159 lines
4.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <arpa/inet.h>
|
|
#include <sys/socket.h>
|
|
#include <pthread.h>
|
|
|
|
#define BUF_SIZE 1024
|
|
#define SMALL_BUF 100
|
|
|
|
void *request_handler(void *arg);
|
|
void send_data(FILE *fp, char *ct, char *file_name);
|
|
char *content_type(char *file);
|
|
void send_error(FILE *fp);
|
|
void error_handling(char *message);
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int serv_sock;
|
|
struct sockaddr_in serv_adr, clnt_adr;
|
|
int clnt_adr_size;
|
|
pthread_t t_id;
|
|
if (argc != 2)
|
|
{
|
|
printf("Usage : %s <port>\n", argv[0]);
|
|
exit(1);
|
|
}
|
|
|
|
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
|
|
memset(&serv_adr, 0, sizeof(serv_adr));
|
|
serv_adr.sin_family = AF_INET;
|
|
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
serv_adr.sin_port = htons(atoi(argv[1]));
|
|
if (bind(serv_sock, (struct sockaddr *)&serv_adr, sizeof(serv_adr)) == -1)
|
|
error_handling("bind() error");
|
|
if (listen(serv_sock, 20) == -1)
|
|
error_handling("listen() error");
|
|
|
|
while (1)
|
|
{
|
|
clnt_adr_size = sizeof(clnt_adr);
|
|
int *clnt_sock_p = malloc(sizeof(int));
|
|
*clnt_sock_p = accept(serv_sock, (struct sockaddr *)&clnt_adr, &clnt_adr_size);
|
|
printf("Connection Request : %s:%d\n",
|
|
inet_ntoa(clnt_adr.sin_addr), ntohs(clnt_adr.sin_port));
|
|
pthread_create(&t_id, NULL, request_handler, clnt_sock_p);
|
|
pthread_detach(t_id);
|
|
}
|
|
close(serv_sock);
|
|
return 0;
|
|
}
|
|
|
|
void *request_handler(void *arg)
|
|
{
|
|
int clnt_sock = *((int *)arg);
|
|
free(arg);
|
|
char req_line[SMALL_BUF];
|
|
FILE *clnt_read;
|
|
FILE *clnt_write;
|
|
|
|
char method[10];
|
|
char ct[15];
|
|
char file_name[30];
|
|
|
|
clnt_read = fdopen(clnt_sock, "r");
|
|
clnt_write = fdopen(dup(clnt_sock), "w");
|
|
fgets(req_line, SMALL_BUF, clnt_read);
|
|
if (strstr(req_line, "HTTP/") == NULL)
|
|
{
|
|
send_error(clnt_write);
|
|
fclose(clnt_read);
|
|
fclose(clnt_write);
|
|
return NULL;
|
|
}
|
|
strcpy(method, strtok(req_line, " /"));
|
|
strcpy(file_name, strtok(NULL, " /"));
|
|
strcpy(ct, content_type(file_name));
|
|
if (strcmp(method, "GET") != 0)
|
|
{
|
|
send_error(clnt_write);
|
|
fclose(clnt_read);
|
|
fclose(clnt_write);
|
|
return NULL;
|
|
}
|
|
fclose(clnt_read);
|
|
send_data(clnt_write, ct, file_name);
|
|
}
|
|
void send_data(FILE *fp, char *ct, char *file_name)
|
|
{
|
|
char protocol[] = "HTTP/1.0 200 OK\r\n";
|
|
char server[] = "Server:Linux Web Server \r\n";
|
|
char cnt_len[] = "Content-length:2048\r\n";
|
|
char cnt_type[SMALL_BUF];
|
|
char buf[BUF_SIZE];
|
|
FILE *send_file;
|
|
|
|
sprintf(cnt_type, "Content-type:%s\r\n\r\n", ct);
|
|
send_file = fopen(file_name, "r");
|
|
if (send_file == NULL)
|
|
{
|
|
send_error(fp);
|
|
fclose(fp);
|
|
return;
|
|
}
|
|
|
|
//传输头信息
|
|
fputs(protocol, fp);
|
|
fputs(server, fp);
|
|
fputs(cnt_len, fp);
|
|
fputs(cnt_type, fp);
|
|
|
|
//传输响应体数据
|
|
while (fgets(buf, BUF_SIZE, send_file) != NULL)
|
|
{
|
|
fputs(buf, fp);
|
|
fflush(fp);
|
|
}
|
|
fflush(fp);
|
|
fclose(fp);
|
|
}
|
|
char *content_type(char *file)
|
|
{
|
|
char extension[SMALL_BUF];
|
|
char file_name[SMALL_BUF];
|
|
char *ext;
|
|
strcpy(file_name, file);
|
|
strtok(file_name, ".");
|
|
ext = strtok(NULL, ".");
|
|
if (ext == NULL)
|
|
return "text/plain";
|
|
strcpy(extension, ext);
|
|
|
|
if (!strcmp(extension, "html") || !strcmp(extension, "htm"))
|
|
return "text/html";
|
|
else
|
|
return "text/plain";
|
|
}
|
|
void send_error(FILE *fp)
|
|
{
|
|
char protocol[] = "HTTP/1.0 400 Bad Request\r\n";
|
|
char server[] = "Server:Linux Web Server \r\n";
|
|
char cnt_len[] = "Content-length:2048\r\n";
|
|
char cnt_type[] = "Content-type:text/html\r\n\r\n";
|
|
char content[] = "<html><head><title>NETWORK</title></head>"
|
|
"<body><font size=+5><br>发生错误! 查看请求文件名和请求方式!"
|
|
"</font></body></html>";
|
|
fputs(protocol, fp);
|
|
fputs(server, fp);
|
|
fputs(cnt_len, fp);
|
|
fputs(cnt_type, fp);
|
|
fflush(fp);
|
|
}
|
|
void error_handling(char *message)
|
|
{
|
|
fputs(message, stderr);
|
|
fputc('\n', stderr);
|
|
exit(1);
|
|
} |