没有合适的资源?快使用搜索试试~ 我知道了~
join 方法:阻塞线程 , 直到该线程执行完毕 因此 ,可以对join加一个超时操作 , join([timeout]),超过设置时间,就不再阻塞线程 jion加上还有一个后果就是, 子线程和主线程绑定在一起 , 直到子线程运行完毕,才开始执行子线程。 代码 有join: 在CODE上查看代码片派生到我的代码片 #-*- coding: UTF-8 -*- import threading from time import sleep def fun(): 在CODE上查看代码片派生到我的代码片 <span style=white-space:pre
资源推荐
资源评论





















/*!Copyright (c) 2025-2026 TP-LINK Technologies CO.,LTD.
*All rights reserved.
*
*\file single_web.c
*\brief 单线程阻塞式 WebServer
*
*\author Fu Yu <[email protected]>
*\version 1.0.0
*\date 2025-08-07
*
*\history \arg 1.0.0 2025-08-07, Fu Yu, Create file.
*/
#ifndef SINGLE_WEB_H_
#define SINGLE_WEB_H_
/**************************************************************************************************/
/* INCLUDE_FILES */
/**************************************************************************************************/
#define _GNU_SOURCE
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/stat.h>
/**************************************************************************************************/
/* DEFINES */
/**************************************************************************************************/
#define PORT 8080 /* 监听端口 */
#define BACKLOG 10 /* 等待连接队列长度 */
#define ROOT "web" /* 前端根目录 */
#define BUF_SIZE 8192 /* 读写缓冲区大小 */
/**************************************************************************************************/
/* TYPES */
/**************************************************************************************************/
/**************************************************************************************************/
/* VARIBLES */
/**************************************************************************************************/
/**************************************************************************************************/
/* PUBLIC_FUNCTIONS */
/**************************************************************************************************/
/**************************************************************************************************/
/* LOCAL_FUNCTIONS */
/**************************************************************************************************/
#endif
/* 根据文件扩展名返回 HTTP Content-Type */
static const char *mime(const char *ext)
{
if (!ext) return "application/octet-stream";
if (!strcasecmp(ext, "html") || !strcasecmp(ext, "htm")) return "text/html";
if (!strcasecmp(ext, "css")) return "text/css";
if (!strcasecmp(ext, "js")) return "application/javascript";
if (!strcasecmp(ext, "png")) return "image/png";
if (!strcasecmp(ext, "jpg") || !strcasecmp(ext, "jpeg")) return "image/jpeg";
return "text/plain";
}
int main(int argc, char *argv[])
{
int port = argc > 1 ? atoi(argv[1]) : PORT; /* 支持命令行传端口 */
/* 1. 创建 TCP 套接字 */
int listenfd = socket(AF_INET, SOCK_STREAM, 0); /* IPv4 面向字节流的TCP 协议自动选择TCP*/
if (listenfd < 0)
{
perror("socket");
exit(EXIT_FAILURE);
}
/* 设置地址重用 */
int opt = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
/* 2. 绑定本地地址(IP+端口) */
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET; /* IPv4 */
addr.sin_port = htons(port); /* 端口号转网络字节序 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* 监听所有网卡 */
/* bind() 把“地址+端口”贴到套接字上 */
if (bind(listenfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
{
perror("bind");
exit(EXIT_FAILURE);
}
/* listen() 把主动套接字变成“被动监听”套接字,允许接受连接 */
if (listen(listenfd, BACKLOG) == -1)
{
perror("listen");
exit(EXIT_FAILURE);
}
printf("单线程 WebServer 已启动 http://localhost:%d\n", port);
/* 3. 单线程主循环:一次只处理一个连接 */
while (1)
{
struct sockaddr_in cli; /* 客户端地址 */
socklen_t len = sizeof(cli);
int connfd = accept(listenfd, (struct sockaddr *)&cli, &len); /* 阻塞等待 */
if (connfd < 0) /* accept 出错 */
{
if (errno == EINTR)
{
continue; /* 被信号中断 */
}
perror("accept");
continue;
}
/* 4. 读取请求首行 */
char buf[BUF_SIZE] = {0};
ssize_t n = recv(connfd, buf, sizeof(buf) - 1, 0);
if (n <= 0) /* 客户端断开 */
{
close(connfd);
continue;
}
buf[n] = 0; /* 字符串结束 */
char method[8], path[256], proto[16];
sscanf(buf, "%7s %255s %15s", method, path, proto); /* 解析 GET /xxx HTTP/1.1 */
/* 5. 默认首页 / -> /Index.html */
if (strcmp(path, "/") == 0)
{
strcpy(path, "/Index.html");
}
/* 6. 简单防目录穿越 */
if (strstr(path, ".."))
{
const char *rsp = "HTTP/1.1 400 Bad Request\r\n\r\n";
send(connfd, rsp, strlen(rsp), 0);
close(connfd);
continue;
}
/* 7. 构造磁盘绝对路径并打印 */
char file_path[512];
snprintf(file_path, sizeof(file_path), "%s%s", ROOT, path);
fprintf(stderr, "[DEBUG] path=%s file_path=%s\n", path, file_path);
/* 8. 打开文件,不存在则 404 */
int fd = open(file_path, O_RDONLY);
if (fd < 0)
{
const char *rsp = "HTTP/1.1 404 Not Found\r\n\r\n<h1>404</h1>";
send(connfd, rsp, strlen(rsp), 0);
close(connfd);
continue;
}
/* 9. 获取文件大小与Content-Type */
struct stat st;
fstat(fd, &st);
const char *ext = strrchr(file_path, '.');
const char *ct = mime(ext ? ext + 1 : NULL);
/* 10. 发送 HTTP 响应头 */
char header[512];
int hd_len = snprintf(header, sizeof(header), /* HTTP响应头 */
"HTTP/1.1 200 OK\r\n" /* 状态行 */
"Content-Type: %s\r\n" /* 文件类型 */
"Content-Length: %ld\r\n" /* 正文长度 */
"Connection: close\r\n\r\n",
ct, st.st_size);
send(connfd, header, hd_len, 0);/* 写入套接字 */
/* 11. 发送文件内容 */
char file_buf[BUF_SIZE];
ssize_t bytes;
while ((bytes = read(fd, file_buf, sizeof(file_buf))) > 0)
{
send(connfd, file_buf, bytes, 0);
}
/* 12. 关闭文件和连接,准备下一个循环 */
close(fd);
close(connfd);
}
return 0;
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Contact</title>
<link rel="stylesheet" type="text/css" href="css/style_mobile.css">
<script language="javascript" type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script language="javascript" type="text/javascript" src="js/action.js"></script>
</head>
<body>
Contact
<form action="/https/download.csdn.net/submit" method="POST">
Name*
<input class="input" type="text" name="name" id="name" >
Email*
<input class="input" type="text" name="email" id="email">
Message*
<textarea class="msginput" type="text" name="message" id="message"></textarea>
<button type="submit" class="button">SUBMIT</button>
</form>
All illustrations on this site are from the very talented
illustrator and designer David Lanham. Make sure to
check out his work at DavidLanham.com.
David Lanham
I create beatufiul illustrations and designs.About me.
</body>
</html>
帮我修改c文件实现post功能
Contact
<form action="/https/download.csdn.net/submit" method="POST">
<input class="input" type="text" name="name" id="name" >
<input class="input" type="text" name="email" id="email">
<textarea class="msginput" type="text" name="message" id="message"></textarea>
<button type="submit" class="button">SUBMIT</button>
</form>
Name*
Email*
Message*
All illustrations on this site are from the very talented
illustrator and designer David Lanham. Make sure to
check out his work at DavidLanham.com.
David Lanham
I create beatufiul illustrations and designs.About me.
浏览:2
资源评论


weixin_38513794
- 粉丝: 1
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 网络环境下教育教学的探索与实施.docx
- Ahdqyln计算机专业大学本科方案设计书(网络).doc
- 数据库课程设计(实例-).doc
- 单片机万年历电子钟方案设计书报告含电路图和源程序.doc
- 2010年9月全国计算机等级测验二级笔试试卷C语言程序设计.docx
- workerman-PHP资源
- 计算机软件应用与发展分析.docx
- 麻村砂石加工系统安全渡汛措施.doc
- 论网络环境中的图书馆藏书发展.docx
- 一级分类食品饮料、家居用品、个人用品、IT与电子商务、耐用品.doc
- 工作任务8-网站宣传与推广.ppt
- 基于大学城空间的动态网页课程信息化教学设计.docx
- ATS单片机的数字温度测量及显示系统设计方案.doc
- 贫困地区的教育信息化发展障碍及对策.docx
- 论大数据对高校教育的推动作用.docx
- Freescale单片机电池管理系统设计方案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
