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

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

📁 一个多线程web服务器实例(C,Linux,详细的web服务器原理)
💻 HTM
📖 第 1 页 / 共 5 页
字号:
      href="<A 
      href='http://blog.csdn.net/manio/archive/2006/10/14/"<a'>http://blog.csdn.net/manio/archive/2006/10/14/"&lt;a</A>"&gt;退出&lt;/a&gt;&lt;/font&gt;&lt;/div&gt;&lt;img 
      src="/intl/zh-CN_ALL/images/logo.gif" width=286 height=110 
      alt="Google"&gt;&lt;br&gt;&lt;br&gt;<BR>&lt;form action=/search 
      name=f&gt;&lt;script&gt;&lt;!--<BR>function qs(el) {if (window.RegExp 
      &amp;&amp; window.encodeURIComponent) {var ue=el.href;var 
      qe=encodeURIComponent(document.f.q.value);if(ue.indexOf("q=")!=-1){el.href=ue.replace(new 
      RegExp("q=[^&amp;$]*"),"q="+qe);}else{el.href=ue+"&amp;q="+qe;}}return 
      1;}<BR>// --&gt;<BR>..........</P></DIV>
      <P>第一个空行上面的就是“说明”了,下面是html代码。有了说明,浏览器就知道这是什么了,拿到这段数据后,就把这些html标签解释成各种各样的元素,在浏览器上有序地显示出来。浏览器还蛮聪明的,当看到&lt;img 
      src=..&gt;标签,那就会又自己发一个请求报文给服务器,要求得到一个图像文件,请求报文就像:</P>
      <DIV 
      style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
      <DIV><SPAN style="COLOR: #000000">GET&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">/</SPAN><SPAN 
      style="COLOR: #000000">image</SPAN><SPAN 
      style="COLOR: #000000">/</SPAN><SPAN 
      style="COLOR: #000000">pp.jpg&nbsp;HTTP</SPAN><SPAN 
      style="COLOR: #000000">/</SPAN><SPAN 
      style="COLOR: #000000">1.1</SPAN><SPAN 
      style="COLOR: #000000"><BR>....</SPAN></DIV></DIV>
      <P>这样,服务器端就找到这个.jpg图像,加上"说明"之后发给浏览器,浏览器收到后就显示在对应的位置上。遇到包含css、js...的标签也一样。</P>
      <P>如此重复,一个完整的web就会呈现在我们眼前了。</P>
      <P>服务器端代码:</P>
      <DIV 
      style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
      <DIV><SPAN style="COLOR: #008000">/*</SPAN><SPAN 
      style="COLOR: #008000">****************************************************************<BR><BR>&nbsp;&nbsp;mymultiwebserver.c&nbsp;<BR><BR>&nbsp;&nbsp;system:redhat&nbsp;linux&nbsp;Fedora&nbsp;Core&nbsp;5<BR><BR>&nbsp;&nbsp;enviroment:g++<BR><BR>&nbsp;&nbsp;compile&nbsp;command:g++&nbsp;-g&nbsp;-o&nbsp;mymultiwebserver&nbsp;-lpthread<BR><BR>&nbsp;&nbsp;date:10/15/2006<BR><BR>&nbsp;&nbsp;By&nbsp;Manio<BR><BR>&nbsp;****************************************************************</SPAN><SPAN 
      style="COLOR: #008000">*/</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>#include&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">&lt;</SPAN><SPAN 
      style="COLOR: #000000">stdlib.h</SPAN><SPAN 
      style="COLOR: #000000">&gt;</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>#include&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">&lt;</SPAN><SPAN 
      style="COLOR: #000000">sys</SPAN><SPAN 
      style="COLOR: #000000">/</SPAN><SPAN 
      style="COLOR: #000000">types.h</SPAN><SPAN 
      style="COLOR: #000000">&gt;</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>#include&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">&lt;</SPAN><SPAN 
      style="COLOR: #000000">sys</SPAN><SPAN 
      style="COLOR: #000000">/</SPAN><SPAN 
      style="COLOR: #000000">socket.h</SPAN><SPAN 
      style="COLOR: #000000">&gt;</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>#include&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">&lt;</SPAN><SPAN 
      style="COLOR: #000000">sys</SPAN><SPAN 
      style="COLOR: #000000">/</SPAN><SPAN 
      style="COLOR: #000000">stat.h</SPAN><SPAN 
      style="COLOR: #000000">&gt;</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>#include&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">&lt;</SPAN><SPAN 
      style="COLOR: #000000">netinet</SPAN><SPAN 
      style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #0000ff">in</SPAN><SPAN 
      style="COLOR: #000000">.h</SPAN><SPAN 
      style="COLOR: #000000">&gt;</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>#include&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">&lt;</SPAN><SPAN 
      style="COLOR: #000000">unistd.h</SPAN><SPAN 
      style="COLOR: #000000">&gt;</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>#include&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">&lt;</SPAN><SPAN 
      style="COLOR: #000000">pthread.h</SPAN><SPAN 
      style="COLOR: #000000">&gt;</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>#include&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">&lt;</SPAN><SPAN 
      style="COLOR: #000000">stdio.h</SPAN><SPAN 
      style="COLOR: #000000">&gt;</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>#include&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">&lt;</SPAN><SPAN 
      style="COLOR: #0000ff">string</SPAN><SPAN 
      style="COLOR: #000000">.h</SPAN><SPAN 
      style="COLOR: #000000">&gt;</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>#include&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">&lt;</SPAN><SPAN 
      style="COLOR: #000000">arpa</SPAN><SPAN 
      style="COLOR: #000000">/</SPAN><SPAN 
      style="COLOR: #000000">inet.h</SPAN><SPAN 
      style="COLOR: #000000">&gt;</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR><BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">#define</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;PORT&nbsp;8848</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">#define</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;BACKLOG&nbsp;5</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">#define</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;MAXDATASIZE&nbsp;1000</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">#define</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;DEBUG&nbsp;1</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">void</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;process_cli(</SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;connectfd,&nbsp;sockaddr_in&nbsp;client);<BR><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></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></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></SPAN><SPAN 
      style="COLOR: #0000ff">char</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;getextname(</SPAN><SPAN 
      style="COLOR: #0000ff">char</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;writehead(FILE</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;cfp,&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">char</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;extname);<BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">void</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;start_routine(</SPAN><SPAN 
      style="COLOR: #0000ff">void</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;arg);<BR><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></SPAN><SPAN 
      style="COLOR: #0000ff">struct</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;ARG&nbsp;{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;connfd;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sockaddr_in&nbsp;client;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><BR>main()<BR><BR>{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;listenfd,&nbsp;connectfd;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_t&nbsp;thread;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">id&nbsp;of&nbsp;thread</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ARG&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">arg;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">pass&nbsp;this&nbsp;var&nbsp;to&nbsp;the&nbsp;thread</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">struct</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;sockaddr_in&nbsp;server;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">server's&nbsp;address&nbsp;info</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">struct</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;sockaddr_in&nbsp;client;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">client's</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;sin_size;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">create&nbsp;tcp&nbsp;socket</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR>#ifdef&nbsp;DEBUG<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">socket.... 
      </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;</SPAN><SPAN 
      style="COLOR: #0000ff">if</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;((listenfd&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">=</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;socket(AF_INET,&nbsp;SOCK_STREAM,&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">0</SPAN><SPAN 
      style="COLOR: #000000">))&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;{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">creating&nbsp;socket&nbsp;failed.</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 

⌨️ 快捷键说明

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