⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 一个多线程web服务器实例(c,linux,详细的web服务器原理) - 马牛不是人 - csdnblog.htm

📁 一个多线程web服务器实例(C,Linux,详细的web服务器原理)
💻 HTM
📖 第 1 页 / 共 5 页
字号:
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">string&nbsp;comparing 
      :::::::::::::start::::::::::::::: </SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;<BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">#endif</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">if</SPAN><SPAN 
      style="COLOR: #000000">(strcmp(cmd,</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">GET</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">)&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">==</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">0</SPAN><SPAN 
      style="COLOR: #000000">){<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">the&nbsp;command&nbsp;is&nbsp;get</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR>#ifdef&nbsp;DEBUG<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">cmd(%s)==GET 
      </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">,cmd);<BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">#endif</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">is&nbsp;this&nbsp;a&nbsp;file&nbsp;or&nbsp;dir&nbsp;or&nbsp;notexist?</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">if</SPAN><SPAN 
      style="COLOR: #000000">(fileordirExist(filepath)){<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">is&nbsp;a&nbsp;file&nbsp;or&nbsp;dir&nbsp;or&nbsp;none<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">is&nbsp;this&nbsp;a&nbsp;dir&nbsp;</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">if</SPAN><SPAN 
      style="COLOR: #000000">(IsDIR(filepath)){<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">is&nbsp;a&nbsp;dir</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR>#ifdef&nbsp;DEBUG<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">%s&nbsp;is&nbsp;a&nbsp;DIR </SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">,filepath);<BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">#endif</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">if</SPAN><SPAN 
      style="COLOR: #000000">(&nbsp;fileordirExist(&nbsp;strcat(filepath,</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">index.htm</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">)&nbsp;)){<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendobj(connectfd,</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">index.htm</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN><SPAN 
      style="COLOR: #0000ff">else</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">if</SPAN><SPAN 
      style="COLOR: #000000">(fileordirExist(strcat(filepath,</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">index.html</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">))){<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendobj(connectfd,</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">index.htm</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN><SPAN 
      style="COLOR: #0000ff">else</SPAN><SPAN 
      style="COLOR: #000000">{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg404(connectfd);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN><SPAN 
      style="COLOR: #0000ff">else</SPAN><SPAN 
      style="COLOR: #000000">{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">is&nbsp;a&nbsp;file</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR>#ifdef&nbsp;DEBUG<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">%s&nbsp;is&nbsp;a&nbsp;file</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">,filepath);<BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">#endif</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendobj(connectfd,filepath);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN><SPAN 
      style="COLOR: #0000ff">else</SPAN><SPAN 
      style="COLOR: #000000">{<BR><BR>#ifdef&nbsp;DEBUG<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">404 
      </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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg404(connectfd);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN><SPAN 
      style="COLOR: #0000ff">else</SPAN><SPAN 
      style="COLOR: #000000">{<BR><BR>#ifdef&nbsp;DEBUG<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">cmd(%s)!=GET 
      </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">,cmd);<BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">#endif</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>#ifdef&nbsp;DEBUG<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">:::::::::::::end::::::::::::::: </SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;<BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">#endif</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;close(connectfd);<BR><BR>}<BR><BR></SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">send&nbsp;the&nbsp;404&nbsp;error&nbsp;message&nbsp;to&nbsp;the&nbsp;client</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR></SPAN><SPAN 
      style="COLOR: #0000ff">void</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;msg404(</SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;connectfd)<BR><BR>{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">char</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;msg;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;msg&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">=</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">HTTP/1.0&nbsp;404&nbsp;Not&nbsp;Found 
      Content-Type:&nbsp;text/plain 
      404&nbsp;not&nbsp;found&nbsp;by&nbsp;Manio</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;send(connectfd,msg,strlen(msg),</SPAN><SPAN 
      style="COLOR: #000000">0</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>}<BR><BR></SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">is&nbsp;the&nbsp;filepath&nbsp;a&nbsp;file&nbsp;&nbsp;or&nbsp;directory</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR></SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;fileordirExist(</SPAN><SPAN 
      style="COLOR: #0000ff">char</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;fpath)<BR><BR>{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">struct</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;stat&nbsp;filestat;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">return</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;(&nbsp;&nbsp;stat(fpath,</SPAN><SPAN 
      style="COLOR: #000000">&amp;</SPAN><SPAN 
      style="COLOR: #000000">filestat)&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">!=</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>}<BR><BR><BR><BR></SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">&nbsp;is&nbsp;the&nbsp;filepath&nbsp;a&nbsp;directory</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR></SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;IsDIR(</SPAN><SPAN 
      style="COLOR: #0000ff">char</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;fpath)<BR><BR>{<BR><BR>#ifdef&nbsp;DEBUG<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">IN&nbsp;IsDIR 
      </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>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">struct</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;stat&nbsp;filestat;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">return</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;(&nbsp;stat(fpath,</SPAN><SPAN 
      style="COLOR: #000000">&amp;</SPAN><SPAN 
      style="COLOR: #000000">filestat)&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">!=</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">&amp;&amp;</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;S_ISDIR(filestat.st_mode));<BR><BR>}<BR><BR><BR><BR></SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">send&nbsp;the&nbsp;data&nbsp;of&nbsp;the&nbsp;file&nbsp;which&nbsp;the&nbsp;client&nbsp;want</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR></SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;sendobj(</SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;connectfd,</SPAN><SPAN 
      style="COLOR: #0000ff">char</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;serverfilepath)<BR><BR>{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;FILE</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;sfp,</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="C

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -