亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來(lái)到蟲(chóng)蟲(chóng)下載站! | ?? 資源下載 ?? 資源專(zhuān)輯 ?? 關(guān)于我們
? 蟲(chóng)蟲(chóng)下載站

?? advanced.html

?? Beej的socket教材
?? HTML
?? 第 1 頁(yè) / 共 2 頁(yè)
字號(hào):
<HTML><HEAD><TITLE>Slightly Advanced Techniques</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.70"><LINKREL="HOME"TITLE="Beej's Guide to Network Programming"HREF="index.html"><LINKREL="PREVIOUS"TITLE="Client-Server Background"HREF="clientserver.html"><LINKREL="NEXT"TITLE="More References"HREF="reference.html"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=utf-8"></HEAD><BODYCLASS="sect1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Beej's Guide to Network Programming</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="clientserver.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="reference.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="sect1"><H1CLASS="sect1"><ANAME="advanced">6. Slightly Advanced Techniques</A></H1><P>These aren't <EM>really</EM> advanced, but they'regetting out of the more basic levels we've already covered.  In fact, ifyou've gotten this far, you should consider yourself fairly accomplishedin the basics of Unix network programming!  Congratulations!</P><P>So here we go into the brave new world of some of the moreesoteric things you might want to learn about sockets.  Have atit!</P><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="blocking">6.1. Blocking</A></H2><P>Blocking.  You've heard about it--now what the heck is it?  In anutshell, "block" is techie jargon for "sleep".  You probably noticedthat when you run <BCLASS="command">listener</B>, above, it just sits thereuntil a packet arrives.  What happened is that it called<TTCLASS="function">recvfrom()</TT>, there was no data, and so<TTCLASS="function">recvfrom()</TT> is said to "block" (that is, sleepthere) until some data arrives.</P><P>Lots of functions block.  <TTCLASS="function">accept()</TT> blocks.All the <TTCLASS="function">recv()</TT> functions block.  The reason theycan do this is because they're allowed to.  When you first create thesocket descriptor with <TTCLASS="function">socket()</TT>, the kernel sets itto blocking.  If you don't want a socket to be blocking, you have tomake a call to <TTCLASS="function">fcntl()</TT>:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    #include &#60;unistd.h&#62;    #include &#60;fcntl.h&#62;    .    .    sockfd = socket(AF_INET, SOCK_STREAM, 0);    fcntl(sockfd, F_SETFL, O_NONBLOCK);    .    . </PRE></TD></TR></TABLE><P>By setting a socket to non-blocking, you can effectively "poll"the socket for information.  If you try to read from a non-blockingsocket and there's no data there, it's not allowed to block--it willreturn <TTCLASS="constant">-1</TT> and <TTCLASS="parameter"><I>errno</I></TT> will beset to <TTCLASS="constant">EWOULDBLOCK</TT>.</P><P>Generally speaking, however, this type of polling is a bad idea.If you put your program in a busy-wait looking for data on the socket,you'll suck up CPU time like it was going out of style.  A more elegantsolution for checking to see if there's data waiting to be read comes inthe following section on <TTCLASS="function">select()</TT>.</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="select">6.2. <TTCLASS="function">select()</TT>--Synchronous I/O Multiplexing</A></H2><P>This function is somewhat strange, but it's very useful.  Take thefollowing situation: you are a server and you want to listen forincoming connections as well as keep reading from the connections youalready have.</P><P>No problem, you say, just an <TTCLASS="function">accept()</TT> and acouple of <TTCLASS="function">recv()</TT>s.  Not so fast, buster!  What ifyou're blocking on an <TTCLASS="function">accept()</TT> call?  How are yougoing to <TTCLASS="function">recv()</TT> data at the same time?  "Usenon-blocking sockets!"  No way!  You don't want to be a CPU hog.  What,then?</P><P><TTCLASS="function">select()</TT> gives you the power to monitorseveral sockets at the same time.  It'll tell you which ones are readyfor reading, which are ready for writing, and which sockets have raisedexceptions, if you really want to know that.</P><P>Without any further ado, I'll offer the synopsis of<TTCLASS="function">select()</TT>:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;       #include &#60;sys/time.h&#62;       #include &#60;sys/types.h&#62;       #include &#60;unistd.h&#62;       int select(int numfds, fd_set *readfds, fd_set *writefds,                  fd_set *exceptfds, struct timeval *timeout); </PRE></TD></TR></TABLE><P>The function monitors "sets" of file descriptors; in particular<TTCLASS="parameter"><I>readfds</I></TT>, <TTCLASS="parameter"><I>writefds</I></TT>, and<TTCLASS="parameter"><I>exceptfds</I></TT>.  If you want to see if you can readfrom standard input and some socket descriptor,<TTCLASS="parameter"><I>sockfd</I></TT>, just add the file descriptors<TTCLASS="constant">0</TT> and <TTCLASS="parameter"><I>sockfd</I></TT> to the set<TTCLASS="parameter"><I>readfds</I></TT>.  The parameter<TTCLASS="parameter"><I>numfds</I></TT> should be set to the values of the highestfile descriptor plus one.  In this example, it should be set to<TTCLASS="parameter"><I>sockfd+1</I></TT>, since it is assuredly higher thanstandard input (<TTCLASS="constant">0</TT>).</P><P>When <TTCLASS="function">select()</TT> returns,<TTCLASS="parameter"><I>readfds</I></TT> will be modified to reflect which of thefile descriptors you selected which is ready for reading.  You can testthem with the macro <TTCLASS="function">FD_ISSET()</TT>, below.</P><P>Before progressing much further, I'll talk about how to manipulatethese sets.  Each set is of the type <TTCLASS="type">fd_set</TT>.  The followingmacros operate on this type:</P><P></P><UL><LI><P><TTCLASS="function">FD_ZERO(fd_set *set)</TT> -- clears afile descriptor set</P></LI><LI><P><TTCLASS="function">FD_SET(int fd, fd_set *set)</TT> -- adds<TTCLASS="parameter"><I>fd</I></TT> to the set</P></LI><LI><P><TTCLASS="function">FD_CLR(int fd, fd_set *set)</TT> --removes <TTCLASS="parameter"><I>fd</I></TT> from the set</P></LI><LI><P><TTCLASS="function">FD_ISSET(int fd, fd_set *set)</TT> --tests to see if <TTCLASS="parameter"><I>fd</I></TT> is in theset</P></LI></UL><P>Finally, what is this weirded out <TTCLASS="type">struct timeval</TT>?Well, sometimes you don't want to wait forever for someone to send yousome data.  Maybe every 96 seconds you want to print "Still Going..." tothe terminal even though nothing has happened.  This time structureallows you to specify a timeout period.  If the time is exceeded and<TTCLASS="function">select()</TT> still hasn't found any ready filedescriptors, it'll return so you can continue processing.</P><P>The <TTCLASS="type">struct timeval</TT> has the follow fields:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    struct timeval {        int tv_sec;     // seconds        int tv_usec;    // microseconds    }; </PRE></TD></TR></TABLE><P>Just set <TTCLASS="parameter"><I>tv_sec</I></TT> to the number of seconds towait, and set <TTCLASS="parameter"><I>tv_usec</I></TT> to the number ofmicroseconds to wait.  Yes, that's <EM>micro</EM>seconds,not milliseconds.  There are 1,000 microseconds in a millisecond, and1,000 milliseconds in a second.  Thus, there are 1,000,000 microsecondsin a second.  Why is it "usec"?  The "u" is supposed to look like theGreek letter 渭 (Mu) that we use for "micro".  Also, when the functionreturns, <TTCLASS="parameter"><I>timeout</I></TT> <EM>might</EM> beupdated to show the time still remaining.  This depends on what flavorof Unix you're running.</P><P>Yay!  We have a microsecond resolution timer!  Well, don't counton it.  Standard Unix timeslice is around 100 milliseconds, so you mighthave to wait that long no matter how small you set your <TTCLASS="type">structtimeval</TT>.</P><P>Other things of interest:  If you set the fields in your<TTCLASS="type">struct timeval</TT> to <TTCLASS="constant">0</TT>,<TTCLASS="function">select()</TT> will timeout immediately, effectivelypolling all the file descriptors in your sets.  If you set theparameter <TTCLASS="parameter"><I>timeout</I></TT> to NULL, it will never timeout,and will wait until the first file descriptor is ready.  Finally, if youdon't care about waiting for a certain set, you can just set it to NULLin the call to <TTCLASS="function">select()</TT>.</P><P><AHREF="http://www.ecst.csuchico.edu/~beej/guide/net/examples/select.c"TARGET="_top">The following code snippet</A> waits 2.5 seconds forsomething to appear on standard input:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    /*    ** select.c -- a select() demo    */    #include &#60;stdio.h&#62;    #include &#60;sys/time.h&#62;    #include &#60;sys/types.h&#62;    #include &#60;unistd.h&#62;    #define STDIN 0  // file descriptor for standard input    int main(void)    {        struct timeval tv;        fd_set readfds;        tv.tv_sec = 2;        tv.tv_usec = 500000;        FD_ZERO(&#38;readfds);        FD_SET(STDIN, &#38;readfds);        // don't care about writefds and exceptfds:        select(STDIN+1, &#38;readfds, NULL, NULL, &#38;tv);        if (FD_ISSET(STDIN, &#38;readfds))            printf("A key was pressed!\n");        else            printf("Timed out.\n");        return 0;    } </PRE></TD></TR></TABLE><P>If you're on a line buffered terminal, the key you hit should beRETURN or it will time out anyway.</P><P>Now, some of you might think this is a great way to wait for dataon a datagram socket--and you are right: it <EM>might</EM>be.  Some Unices can use select in this manner, and some can't.  Youshould see what your local man page says on the matter if you want toattempt it.</P><P>Some Unices update the time in your <TTCLASS="type">struct timeval</TT> toreflect the amount of time still remaining before a timeout.  But othersdo not.  Don't rely on that occurring if you want to be portable.  (Use<TTCLASS="function">gettimeofday()</TT> if you need to track timeelapsed.  It's a bummer, I know, but that's the way it is.)</P><P>What happens if a socket in the read set closes the connection?Well, in that case, <TTCLASS="function">select()</TT> returns with thatsocket descriptor set as "ready to read".  When you actually do<TTCLASS="function">recv()</TT> from it, <TTCLASS="function">recv()</TT> willreturn <TTCLASS="constant">0</TT>.  That's how you know the client hasclosed the connection.</P><P>One more note of interest about <TTCLASS="function">select()</TT>: ifyou have a socket that is <TTCLASS="function">listen()</TT>ing, you cancheck to see if there is a new connection by putting that socket's filedescriptor in the <TTCLASS="parameter"><I>readfds</I></TT> set.</P><P>And that, my friends, is a quick overview of the almighty<TTCLASS="function">select()</TT> function.</P><P>But, by popular demand, here is an in-depth example.Unfortunately, the difference between the dirt-simple example, above, andthis one here is significant.  But have a look, then read thedescription that follows it.</P><P><AHREF="http://www.ecst.csuchico.edu/~beej/guide/net/examples/selectserver.c"TARGET="_top">This program</A> actslike a simple multi-user chat server.  Start it running in one window,then <BCLASS="command">telnet</B> to it ("<BCLASS="command">telnet hostname9034</B>") from multiple other windows.  When you type somethingin one <BCLASS="command">telnet</B> session, it should appear in all theothers.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    /*    ** selectserver.c -- a cheezy multiperson chat server    */    #include &#60;stdio.h&#62;    #include &#60;stdlib.h&#62;    #include &#60;string.h&#62;    #include &#60;unistd.h&#62;    #include &#60;sys/types.h&#62;    #include &#60;sys/socket.h&#62;    #include &#60;netinet/in.h&#62;    #include &#60;arpa/inet.h&#62;    #define PORT 9034   // port we're listening on    int main(void)    {        fd_set master;   // master file descriptor list        fd_set read_fds; // temp file descriptor list for select()        struct sockaddr_in myaddr;     // server address        struct sockaddr_in remoteaddr; // client address        int fdmax;        // maximum file descriptor number        int listener;     // listening socket descriptor        int newfd;        // newly accept()ed socket descriptor        char buf[256];    // buffer for client data        int nbytes;        int yes=1;        // for setsockopt() SO_REUSEADDR, below        int addrlen;        int i, j;        FD_ZERO(&#38;master);    // clear the master and temp sets        FD_ZERO(&#38;read_fds);        // get the listener        if ((listener = socket(AF_INET, SOCK_STREAM, 0)) == -1) {            perror("socket");            exit(1);        }        // lose the pesky "address already in use" error message        if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &#38;yes,                                                            sizeof(int)) == -1) {            perror("setsockopt");            exit(1);        }        // bind        myaddr.sin_family = AF_INET;

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
大胆欧美人体老妇| jlzzjlzz亚洲日本少妇| 中文字幕在线视频一区| 欧美高清精品3d| 91亚洲精华国产精华精华液| 免费观看一级欧美片| 一区二区三区在线不卡| 久久久久久久久久久久久久久99 | 一区二区三区色| 久久免费的精品国产v∧| 欧美三级韩国三级日本三斤| 丁香婷婷综合激情五月色| 免费成人深夜小野草| 亚洲国产日韩综合久久精品| 国产精品欧美久久久久无广告| 欧美一级高清大全免费观看| 97久久精品人人做人人爽50路| 另类小说一区二区三区| 午夜精品影院在线观看| 一区二区三区在线观看欧美| 国产精品美女www爽爽爽| 26uuu久久综合| 欧美一级免费观看| 欧美日本乱大交xxxxx| 91视频一区二区三区| 国产成人精品免费看| 国产在线一区观看| 毛片av一区二区三区| 秋霞电影网一区二区| 亚洲va欧美va国产va天堂影院| 亚洲精品高清在线| 亚洲欧美一区二区三区久本道91| 中国av一区二区三区| 26uuu国产一区二区三区| 精品精品国产高清a毛片牛牛 | www欧美成人18+| 日韩精品一区二| 日韩精品一区二区三区三区免费| 欧美一区二区免费观在线| 欧美群妇大交群的观看方式| 欧美视频一区在线| 欧美日韩一区国产| 欧美嫩在线观看| 91精品黄色片免费大全| 欧美一区二视频| 日韩午夜av一区| 337p粉嫩大胆噜噜噜噜噜91av| 精品处破学生在线二十三| 欧美tickle裸体挠脚心vk| 久久中文娱乐网| 国产女同互慰高潮91漫画| 国产日韩精品一区二区三区在线| 亚洲国产成人私人影院tom| 国产精品国产三级国产有无不卡 | 麻豆精品精品国产自在97香蕉| 日韩专区欧美专区| 精品亚洲aⅴ乱码一区二区三区| 久久www免费人成看片高清| 精品一区二区在线看| 国产麻豆精品在线| 99riav久久精品riav| 欧美性videosxxxxx| 91精品国产一区二区| 久久久国产综合精品女国产盗摄| 国产欧美日韩精品a在线观看| 亚洲天堂精品视频| 偷拍自拍另类欧美| 国产精品资源站在线| 一本到一区二区三区| 欧美一区日韩一区| 国产精品久久三区| 五月婷婷欧美视频| 国产馆精品极品| 色婷婷综合久久久久中文一区二区 | 视频一区在线播放| 国产一区二区三区四| 91原创在线视频| 91精品久久久久久久久99蜜臂| 久久久久国产免费免费 | 亚洲精品国产a| 捆绑变态av一区二区三区| 白白色 亚洲乱淫| 91精品国产91久久综合桃花| 国产欧美一区二区精品性色| 亚洲国产色一区| 国产91丝袜在线观看| 欧美精品在线一区二区| 日本一区二区成人| 麻豆精品蜜桃视频网站| 色94色欧美sute亚洲13| 欧美精品一区视频| 亚洲成人www| 92国产精品观看| 久久在线观看免费| 日本最新不卡在线| 色综合一区二区| 中文字幕第一区二区| 美女性感视频久久| 欧美视频在线一区| 综合自拍亚洲综合图不卡区| 久久精品国产999大香线蕉| 色拍拍在线精品视频8848| 国产欧美日韩另类视频免费观看| 偷偷要91色婷婷| 91国偷自产一区二区开放时间| 久久久不卡网国产精品一区| 免费精品视频最新在线| 欧美日韩免费电影| 亚洲精品日韩一| aaa欧美色吧激情视频| 国产亚洲精品资源在线26u| 男男成人高潮片免费网站| 欧美三级乱人伦电影| 亚洲精品乱码久久久久久日本蜜臀| 国产精品一区二区久激情瑜伽| 制服丝袜亚洲播放| 亚洲国产精品久久人人爱| 91网页版在线| 亚洲欧美一区二区久久| 972aa.com艺术欧美| 国产精品麻豆久久久| 成人国产视频在线观看| 国产清纯白嫩初高生在线观看91 | 亚洲成av人片| 欧美色爱综合网| 亚洲影院在线观看| 色婷婷精品久久二区二区蜜臂av | 亚洲欧洲av一区二区三区久久| 狠狠狠色丁香婷婷综合激情 | www.日本不卡| 欧美国产一区在线| 成人午夜电影久久影院| 国产婷婷色一区二区三区| 国产成人精品一区二区三区网站观看 | 欧洲在线/亚洲| 一区二区三区波多野结衣在线观看| 99re这里都是精品| 亚洲欧美另类小说| 色94色欧美sute亚洲线路一ni| 一区二区三区日韩在线观看| 欧美亚州韩日在线看免费版国语版 | xvideos.蜜桃一区二区| 国产在线一区观看| 国产欧美一区二区精品性| 国产成人综合精品三级| 国产精品理伦片| 色婷婷综合久久久久中文一区二区 | 91在线视频在线| 亚洲卡通动漫在线| 欧美视频一区二区在线观看| 亚欧色一区w666天堂| 欧美一区在线视频| 国产福利91精品| 亚洲精品你懂的| 7777精品伊人久久久大香线蕉的| 青青青爽久久午夜综合久久午夜| 精品国产精品一区二区夜夜嗨| 国产剧情av麻豆香蕉精品| 综合婷婷亚洲小说| 69p69国产精品| 精品一区二区三区在线视频| 国产日产欧美一区二区视频| 一本一本久久a久久精品综合麻豆| 亚洲一区二区三区免费视频| 欧美三级一区二区| 精品一区二区三区视频在线观看 | 免费精品视频在线| 国产欧美精品一区二区三区四区| 91天堂素人约啪| 麻豆久久久久久| 亚洲欧洲三级电影| 在线综合视频播放| 99这里只有久久精品视频| 石原莉奈在线亚洲二区| 日本一区二区久久| 欧美无乱码久久久免费午夜一区| 日韩国产成人精品| 国产清纯在线一区二区www| 欧美三级三级三级爽爽爽| 国产一区二区在线观看视频| 一区二区在线免费| 欧美成人精品二区三区99精品| 99精品久久只有精品| 日本视频一区二区三区| 中国色在线观看另类| 日韩一区二区三区四区 | 2020国产精品| 欧美亚洲国产一区二区三区va | 亚洲成人av电影在线| 国产午夜一区二区三区| 欧美色图第一页| 成人午夜伦理影院| 免费在线观看一区| 亚洲一区在线视频| 国产精品久久久久久久久久免费看| 欧美精品免费视频| 91免费在线视频观看| 国产一区二区不卡在线| 视频一区视频二区中文| 亚洲免费观看高清完整版在线|