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

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

📁 一个多线程web服务器实例(C,Linux,详细的web服务器原理)
💻 HTM
📖 第 1 页 / 共 5 页
字号:
      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;exit(</SPAN><SPAN 
      style="COLOR: #000000">1</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;opt&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">=</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;SO_REUSEADDR;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setsockopt(listenfd,&nbsp;SOL_SOCKET,&nbsp;SO_REUSEADDR,&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">&amp;</SPAN><SPAN 
      style="COLOR: #000000">opt,&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">sizeof</SPAN><SPAN 
      style="COLOR: #000000">(opt));<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bzero(</SPAN><SPAN 
      style="COLOR: #000000">&amp;</SPAN><SPAN 
      style="COLOR: #000000">server,</SPAN><SPAN 
      style="COLOR: #0000ff">sizeof</SPAN><SPAN 
      style="COLOR: #000000">(server));<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server.sin_family&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">=</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;AF_INET;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server.sin_port&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">=</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;htons(PORT);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server.sin_addr.s_addr&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">=</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;htonl(INADDR_ANY);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">bind.... 
      </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">if</SPAN><SPAN 
      style="COLOR: #000000">(bind(listenfd,(</SPAN><SPAN 
      style="COLOR: #0000ff">struct</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;sockaddr&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN 
      style="COLOR: #000000">&amp;</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">&nbsp;sockaddr))&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;perror(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">bind&nbsp;error.</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(</SPAN><SPAN 
      style="COLOR: #000000">1</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">listen.... 
      </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">if</SPAN><SPAN 
      style="COLOR: #000000">(listen(listenfd,BACKLOG)&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;perror(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">listen()&nbsp;error </SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(</SPAN><SPAN 
      style="COLOR: #000000">1</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sin_size&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">=</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">sizeof</SPAN><SPAN 
      style="COLOR: #000000">(</SPAN><SPAN 
      style="COLOR: #0000ff">struct</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;sockaddr_in);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">while</SPAN><SPAN 
      style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN 
      style="COLOR: #000000">)<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">accept()&nbsp;using&nbsp;main&nbsp;thread</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">accepting.... 
      </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">if</SPAN><SPAN 
      style="COLOR: #000000">((connectfd&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">=</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;accept(listenfd,<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(</SPAN><SPAN 
      style="COLOR: #0000ff">struct</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;sockaddr&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN 
      style="COLOR: #000000">&amp;</SPAN><SPAN 
      style="COLOR: #000000">client,<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(socklen_t</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN 
      style="COLOR: #000000">&amp;</SPAN><SPAN 
      style="COLOR: #000000">sin_size))&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;printf(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">accept()&nbsp;error </SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arg&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">=</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">new</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;ARG;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arg</SPAN><SPAN 
      style="COLOR: #000000">-&gt;</SPAN><SPAN 
      style="COLOR: #000000">connfd&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">=</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;connectfd;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy((</SPAN><SPAN 
      style="COLOR: #0000ff">void</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN 
      style="COLOR: #000000">&amp;</SPAN><SPAN 
      style="COLOR: #000000">arg</SPAN><SPAN 
      style="COLOR: #000000">-&gt;</SPAN><SPAN 
      style="COLOR: #000000">client,&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">&amp;</SPAN><SPAN 
      style="COLOR: #000000">client,&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">sizeof</SPAN><SPAN 
      style="COLOR: #000000">(client));<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">invoke&nbsp;start_routine&nbsp;to&nbsp;handle&nbsp;this&nbsp;thread</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;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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">if</SPAN><SPAN 
      style="COLOR: #000000">(pthread_create(</SPAN><SPAN 
      style="COLOR: #000000">&amp;</SPAN><SPAN 
      style="COLOR: #000000">thread,&nbsp;NULL,&nbsp;start_routine,&nbsp;(</SPAN><SPAN 
      style="COLOR: #0000ff">void</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">)arg)){<BR><BR>&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">pthread_create()&nbsp;error</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;exit(</SPAN><SPAN 
      style="COLOR: #000000">1</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;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;close(listenfd);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><BR>}<BR><BR><BR><BR><BR><BR></SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">handle&nbsp;the&nbsp;request&nbsp;of&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;process_cli(</SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;connectfd,&nbsp;sockaddr_in&nbsp;client)<BR><BR>{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;num;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #008000">//</SPAN><SPAN 
      style="COLOR: #008000">char&nbsp;recvbuf[MAXDATASIZE],&nbsp;sendbuf[MAXDATASIZE],&nbsp;cli_name[MAXDATASIZE];</SPAN><SPAN 
      style="COLOR: #008000"><BR></SPAN><SPAN 
      style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">char</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;requestline[MAXDATASIZE],&nbsp;filepath[MAXDATASIZE],&nbsp;cmd[MAXDATASIZE],extname[MAXDATASIZE];<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #0000ff">int</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;c;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">fp;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">*</SPAN><SPAN 
      style="COLOR: #000000">cfp;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;fp&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">=</SPAN><SPAN 
      style="COLOR: #000000">&nbsp;fdopen(connectfd,</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">r</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;<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">the&nbsp;host&nbsp;is:%s&nbsp;&nbsp;</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">,inet_ntoa(client.sin_addr)&nbsp;);<BR><BR></SPAN><SPAN 
      style="COLOR: #0000ff">#endif</SPAN><SPAN 
      style="COLOR: #000000"><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;fgets(requestline,MAXDATASIZE,fp);<BR><BR>#ifdef&nbsp;DEBUG<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"> 
      THE&nbsp;REQUEST&nbsp;IS&nbsp;:%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>&nbsp;&nbsp;&nbsp;&nbsp;strcpy(filepath,</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">./</SPAN><SPAN 
      style="COLOR: #000000">"</SPAN><SPAN 
      style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;strcpy(extname,&nbsp;getextname(filepath));<BR><BR>#ifdef&nbsp;DEBUG<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN 

⌨️ 快捷键说明

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