📄 一个多线程web服务器实例(c,linux,详细的web服务器原理) - 马牛不是人 - csdnblog.htm
字号:
style="COLOR: #000000">);<BR><BR> exit(</SPAN><SPAN
style="COLOR: #000000">1</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> }<BR><BR> <BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> opt </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> SO_REUSEADDR;<BR><BR> setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, </SPAN><SPAN
style="COLOR: #000000">&</SPAN><SPAN
style="COLOR: #000000">opt, </SPAN><SPAN
style="COLOR: #0000ff">sizeof</SPAN><SPAN
style="COLOR: #000000">(opt));<BR><BR> <BR><BR> bzero(</SPAN><SPAN
style="COLOR: #000000">&</SPAN><SPAN
style="COLOR: #000000">server,</SPAN><SPAN
style="COLOR: #0000ff">sizeof</SPAN><SPAN
style="COLOR: #000000">(server));<BR><BR> server.sin_family </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> AF_INET;<BR><BR> server.sin_port </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> htons(PORT);<BR><BR> server.sin_addr.s_addr </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> htonl(INADDR_ANY);<BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">bind....
</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">if</SPAN><SPAN
style="COLOR: #000000">(bind(listenfd,(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN><SPAN
style="COLOR: #000000"> sockaddr </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN
style="COLOR: #000000">&</SPAN><SPAN
style="COLOR: #000000">server,</SPAN><SPAN
style="COLOR: #0000ff">sizeof</SPAN><SPAN
style="COLOR: #000000">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN><SPAN
style="COLOR: #000000"> sockaddr)) </SPAN><SPAN
style="COLOR: #000000">==</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN
style="COLOR: #000000">) {<BR><BR> perror(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">bind error.</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> exit(</SPAN><SPAN
style="COLOR: #000000">1</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> }<BR><BR> <BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">listen....
</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">if</SPAN><SPAN
style="COLOR: #000000">(listen(listenfd,BACKLOG) </SPAN><SPAN
style="COLOR: #000000">==</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN
style="COLOR: #000000">) {<BR><BR> perror(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">listen() error </SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> exit(</SPAN><SPAN
style="COLOR: #000000">1</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> }<BR><BR><BR><BR> sin_size </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">sizeof</SPAN><SPAN
style="COLOR: #000000">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN><SPAN
style="COLOR: #000000"> sockaddr_in);<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">while</SPAN><SPAN
style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN
style="COLOR: #000000">)<BR><BR> {<BR><BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">accept() using main thread</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">accepting....
</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">if</SPAN><SPAN
style="COLOR: #000000">((connectfd </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> accept(listenfd,<BR><BR> (</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN><SPAN
style="COLOR: #000000"> sockaddr </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN
style="COLOR: #000000">&</SPAN><SPAN
style="COLOR: #000000">client,<BR><BR> (socklen_t</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN
style="COLOR: #000000">&</SPAN><SPAN
style="COLOR: #000000">sin_size)) </SPAN><SPAN
style="COLOR: #000000">==</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN
style="COLOR: #000000">) {<BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">accept() error </SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> }<BR><BR><BR><BR> arg </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">new</SPAN><SPAN
style="COLOR: #000000"> ARG;<BR><BR> arg</SPAN><SPAN
style="COLOR: #000000">-></SPAN><SPAN
style="COLOR: #000000">connfd </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> connectfd;<BR><BR> memcpy((</SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN
style="COLOR: #000000">&</SPAN><SPAN
style="COLOR: #000000">arg</SPAN><SPAN
style="COLOR: #000000">-></SPAN><SPAN
style="COLOR: #000000">client, </SPAN><SPAN
style="COLOR: #000000">&</SPAN><SPAN
style="COLOR: #000000">client, </SPAN><SPAN
style="COLOR: #0000ff">sizeof</SPAN><SPAN
style="COLOR: #000000">(client));<BR><BR> <BR><BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">invoke start_routine to handle this thread</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR>#ifdef DEBUG<BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">thread_creating....</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">);<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">#endif</SPAN><SPAN
style="COLOR: #000000"><BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">if</SPAN><SPAN
style="COLOR: #000000">(pthread_create(</SPAN><SPAN
style="COLOR: #000000">&</SPAN><SPAN
style="COLOR: #000000">thread, NULL, start_routine, (</SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">)arg)){<BR><BR> perror(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">pthread_create() error</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> exit(</SPAN><SPAN
style="COLOR: #000000">1</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> } <BR><BR> }<BR><BR> close(listenfd); <BR><BR>}<BR><BR><BR><BR><BR><BR></SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">handle the request of the client</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> process_cli(</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> connectfd, sockaddr_in client)<BR><BR>{<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> num;<BR><BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">char recvbuf[MAXDATASIZE], sendbuf[MAXDATASIZE], cli_name[MAXDATASIZE];</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000"> requestline[MAXDATASIZE], filepath[MAXDATASIZE], cmd[MAXDATASIZE],extname[MAXDATASIZE];<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> c;<BR><BR> FILE </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">fp;<BR><BR> FILE </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">cfp;<BR><BR> fp </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> fdopen(connectfd,</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">r</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">); <BR><BR> <BR><BR>#ifdef DEBUG<BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">the host is:%s </SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">,inet_ntoa(client.sin_addr) );<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">#endif</SPAN><SPAN
style="COLOR: #000000"><BR><BR> fgets(requestline,MAXDATASIZE,fp);<BR><BR>#ifdef DEBUG<BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">
THE REQUEST IS :%s </SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">,requestline);<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">#endif</SPAN><SPAN
style="COLOR: #000000"><BR><BR> strcpy(filepath,</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">./</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> sscanf(requestline,</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">%s%s</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">,cmd,filepath</SPAN><SPAN
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> strcpy(extname, getextname(filepath));<BR><BR>#ifdef DEBUG<BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">cmd:%s
filepath:%s extname:%s </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">,cmd,filepath,extname);<BR><BR> <BR><BR> printf(</SPAN><SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -