Wol*_*sen 6 c sockets multithreading http
我正在尝试编写自己的http 1.1服务器,只是为了获得乐趣并了解有关HTTP,套接字和线程的更多信息.
我有一个良好的开端,我认为只提供静态页面(使用c,我希望暂时保留).我有一个我前一段时间写过的测试页面,并根据chrome提供了124个~50个文件,不使用线程或保持活动的套接字.
我发现很难让线程/保持活动工作.网上很少甚至没有资源(我可以在谷歌搜索中找到)详细解释保持连接的连接.如果有人可以推荐一本关于HTTP服务器编程的好书,我将不胜感激.
我之前通过制作一个简单的聊天程序做了一些线程和套接字编程,所以我至少有一些经验.
我遇到的问题是,当我尝试合并线程时,客户端浏览器会设置多个连接.在某个地方,服务器感到困惑,客户端只是坐在那里等待响应,服务器停止做任何事情.我发送了Connection:Keep-Alive标头,但这并没有改变任何东西,当我合并keep-alive它并创建一个循环来获取线程函数中的请求时,它会停止直到连接关闭.
我很感激,如果有人可以给我一些关于如何保持活动/线程为此工作的伪代码,以便客户端一次停止创建多个连接.
正在进行的简要说明:
main function
load in static pages to large array of fileinfo struct that hold the file data and length
create the socket
set it to listen to port 80
set it to listen for 10 connections at a time(i know this is low...)
start an endless loop
block while waiting for someone to connect
check if it's a localhost connection
shutdown the server
otherwise
start a thread(with pthread), sending it the socket variable
loop
Run Code Online (Sandbox Code Playgroud)
Thread Function
setsock opt for 3 sec timeout on send/recv and enable Keep-alive
start endless loop
read in request
if request timed out, break the loop
Validate Request function call
Create Reponse function call
Send response
if request contained Connection: close header break the loop
loop
close socket
return
Run Code Online (Sandbox Code Playgroud)
main function
load in static pages to large array of fileinfo struct that hold the file data and length
create the socket
set it to listen to port 80
set it to listen for 10 connections at a time(i know this is low...)
start an endless loop
block while waiting for someone to connect
check if it's a localhost connection
shutdown the server
otherwise
start a thread(with pthread), sending it the socket variable
loop
Run Code Online (Sandbox Code Playgroud)
Thread Function
setsock opt for 3 sec timeout on send/recv and enable Keep-alive
start endless loop
read in request
if request timed out, break the loop
Validate Request function call
Create Reponse function call
Send response
if request contained Connection: close header break the loop
loop
close socket
return
Run Code Online (Sandbox Code Playgroud)