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

📄 csdn_文档中心_多路复用i-o模型支持多client的实现及效率讨论.htm

📁 csdn10年中间经典帖子
💻 HTM
📖 第 1 页 / 共 5 页
字号:
            </SPAN>}</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 27pt"><FONT size=2><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">其实</SPAN><SPAN 
            lang=EN-US>Winsock</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的</SPAN><SPAN 
            lang=EN-US>WSAEventSelect</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">模型是与之类似的。</SPAN></FONT></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN 
            lang=EN-US>3.2 &nbsp;</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在单线程中支持</SPAN><SPAN 
            lang=EN-US>63</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个</SPAN><SPAN 
            lang=EN-US>Client</SPAN></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; 
            </SPAN>SOCKET clientsockarray[FD_SETSIZE – 1];<SPAN 
            style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>// FD_SETSIZE is 64 
            </FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><FONT size=2><SPAN 
            lang=EN-US>&nbsp; </SPAN><SPAN lang=EN-US>// </SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">相关初始化处理</SPAN><SPAN 
            lang=EN-US>, </SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">创建监听套接字</SPAN></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
            lang=EN-US><SPAN style="mso-spacerun: yes"><FONT size=2>&nbsp;&nbsp; 
            </FONT></SPAN></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
            lang=EN-US><SPAN style="mso-spacerun: yes"></SPAN></SPAN><SPAN 
            lang=EN-US><FONT size=2>&nbsp; listen(listensock, 
            5);</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><FONT size=2><SPAN 
            lang=EN-US>&nbsp; </SPAN><SPAN lang=EN-US>// </SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">初始化套接字数组</SPAN></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><FONT size=2><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">&nbsp;</SPAN></FONT><SPAN 
            lang=EN-US><FONT size=2>InitSock(clientsockarray);</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><FONT size=2><SPAN 
            lang=EN-US>&nbsp; </SPAN><SPAN 
            lang=EN-US>FD_ZERO(&amp;readfds);</SPAN></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><FONT size=2><SPAN 
            lang=EN-US>&nbsp; </SPAN></FONT><SPAN lang=EN-US><FONT 
            size=2>FD_SET(listensock, &nbsp;&amp;readfds);</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><FONT size=2><SPAN 
            lang=EN-US>&nbsp; </SPAN><SPAN lang=EN-US>for&nbsp; (; 
            ;)</SPAN></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><FONT size=2><SPAN 
            lang=EN-US>&nbsp;</SPAN></FONT><SPAN lang=EN-US><FONT 
            size=2>{</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;</SPAN>nRet &nbsp;= select(0, 
            &nbsp;&amp;readfds, &nbsp;NULL,&nbsp; NULL,&nbsp; 
            NULL);</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US></SPAN><FONT size=2><SPAN lang=EN-US>// </SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">判断监听套接字是否可读</SPAN></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;</SPAN>if&nbsp; 
            (FD_ISSET(listensock,&nbsp; &amp;readfds))</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><FONT 
            size=2><SPAN lang=EN-US>&nbsp;</SPAN><SPAN 
            lang=EN-US>{</SPAN></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
            style="mso-tab-count: 1">&nbsp; </SPAN>clientsock = 
            accept(listensock,&nbsp; NULL,&nbsp; NULL);</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><FONT 
            size=2><SPAN lang=EN-US><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
            style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;</SPAN>// </SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">将客户套接字放到套接字数组中</SPAN></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp; </SPAN>if <SPAN 
            style="mso-spacerun: yes">&nbsp; 
            </SPAN>(!InsertSock(clientsockarray, clientsock))</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;</SPAN>{</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><FONT 
            size=2><SPAN 
            lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN><SPAN lang=EN-US>printf("</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">客户端超过了</SPAN><SPAN 
            lang=EN-US>63</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个</SPAN><SPAN 
            lang=EN-US>,</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">此次连接被拒绝</SPAN><SPAN 
            lang=EN-US>.\n");</SPAN></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN>closesocket(clientsock);</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN>continue;</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>}<SPAN 
            style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN></FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;</SPAN>}</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><SPAN style="mso-spacerun: yes"><FONT 
            size=2>&nbsp;&nbsp;&nbsp; </FONT></SPAN></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><FONT 
            size=2><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp; 
            </SPAN></SPAN><SPAN lang=EN-US>// </SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">逐个处理处于待决状态的套接字</SPAN></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;</SPAN>for&nbsp; (nIndex 
            &nbsp;=&nbsp; 0;&nbsp; nIndex&nbsp; &lt;&nbsp; FD_SETSIZE&nbsp; - 
            &nbsp;1; &nbsp;nIndex++)</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;</SPAN>{</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>if&nbsp; 
            &nbsp;(FD_ISSET(clientsockarray[nIndex], 
            &nbsp;&amp;readfds))</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>{</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN><SPAN 
            style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN>nRet &nbsp;= &nbsp;recv(clientsockarray[nIndex], 
            &nbsp;buff,&nbsp; sizeof(buff),&nbsp; 0);</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN><SPAN 
            style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN>if &nbsp;(nRet &nbsp;= =&nbsp; 0 &nbsp;|| &nbsp;nRet &nbsp;= 
            =&nbsp; SOCKET_ERROR)</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN><SPAN 
            style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN>{</FONT></SPAN></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0; mso-char-indent-size: 10.5pt"><SPAN 
            lang=EN-US><FONT size=2><SPAN 
            style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

⌨️ 快捷键说明

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