1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
<HTML><HEAD>
<TITLE>Tcl API Reference -- ns_sockselect</TITLE>
<LINK rel=Previous href="tapi-135.htm">
<LINK rel=ToC href="toc.htm">
<LINK rel=Index href="master.htm">
<LINK rel=Next href="tapi-137.htm">
</HEAD><BODY BGCOLOR="#ffffff"><A NAME="topofpage"></A>
<TABLE WIDTH=100%>
<TR>
<TD ALIGN=LEFT>
<A NAME="topofpage"></A> <IMG SRC="as-c-sm.gif">
</TD>
<TD ALIGN=RIGHT>
<A href="tapi-135.htm"><IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm> <IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm> <IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="tapi-137.htm"> <IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<A name="7983"> </A>
</TD>
</TR>
</TABLE>
<a name="82277">
</a><h3>ns_sockselect</h3>
<a name="82278">
</a><h4>Overview</h4>
<p><a name="82279">
</a>Determine readable file id's</p>
<a name="82280">
</a><h4>Syntax</h4>
<p><a name="82281">
</a><b>ns_sockselect</b> ?-timeout <i>seconds</i>? <i>rfds wfds efds</i></p>
<a name="82282">
</a><h4>Description</h4>
<p><a name="82283">
</a><b>ns_sockselect</b> uses a select() call to determine which file id's re readable, writeable, or have exceptional conditions. <b>ns_sockselect</b> returns a list of three lists of: readable file id's, writeable file id's, and file id's with exceptions.</p>
<p><a name="82284">
</a>The -timeout option specifies the length of time to wait in <i>seconds</i> for ns_sockselect to return before timing out.</p>
<p><a name="82285">
</a>The <i>rfds</i>, <i>wfds</i>, and <i>efds</i> arguments are lists of readable file id's, writeable file id's, and file id's with exceptions, respectively.</p>
<a name="82287">
</a><h4>Example</h4>
<p><a name="82288">
</a>This example attempts to connect to nine servers at once and service the first available connections:</p>
<pre> <a name="82289"></a># Start nonblocking connect()'s to www01 through
<a name="166822"></a>#www09.foo.com and remember the read fileid which
<a name="82290"></a>#corresponds to each write fileid.
<a name="82291"></a>for {set n 1} {$n < 10} {incr n} {
<a name="82292"></a> set host [format "www%2d.foo.com" $n]
<a name="82293"></a> set fds [ns_sockopen -nonblock $host 80]
<a name="82294"></a> set r [lindex $fds 0]
<a name="82295"></a> set w [lindex $fds 1]
<a name="82296"></a> set w2r($w) $r
<a name="82297"></a> lappend wfds $w
<a name="82298"></a>}
<a name="82299"></a># All connect()'s are in progress, use select to wait for one or
<a name="82300"></a># more to become writable in the next two seconds which means #
# they may have connected. We're not interested in readable or
# exception sockets so the corresponding lists are empty
<a name="82301"></a># (i.e., {}).
<a name="82302"></a>set sel [ns_sockselect -timeout 2 {} $wfds {}]
<a name="82303"></a># Select returned - get the sockets ready to write to.
<a name="82304"></a>set wfds [lindex $sel 1]
<a name="82305"></a># Use ns_sockcheck to see if the sockets actually connected and
<a name="82306"></a># didn't become writable because the connect() failed (e.g., no
<a name="82307"></a># Web server was running on port 80 on the corresponding machine).
<a name="82308"></a># Note that the wfds list may be empty, meaning all sockets timed
<a name="82309"></a># out on connect.
<a name="82310"></a>set ok ""
<a name="82311"></a>foreach w $wfds {
<a name="82312"></a> if [ns_sockcheck $w] {
<a name="82313"></a> # Socket is connected - send a GET HTTP request.
<a name="82314"></a> lappend ok $w
<a name="82315"></a> puts $w "GET /index.htm HTTP/1.0\r\n\r"
<a name="82316"></a> # The flush is important, otherwise the remote
<a name="82317"></a> # server may never see the data.
<a name="82318"></a> flush $w
<a name="82319"></a> }
<a name="82320"></a>}
<a name="82321"></a># Get the read ids for each socket which we sent the GET request
# to.
<a name="82322"></a>foreach w $ok {
<a name="82323"></a> lappend rfds $w2r($w)
<a name="82324"></a>}
<a name="82325"></a># Use select again to wait for the read sockets to have data
<a name="82326"></a># available in response to the GET request.
<a name="82327"></a>set sel [ns_sockselect -timeout 2 $rfds {} {}]
<a name="82328"></a>set rfds [lindex $sel 0]
<a name="82329"></a># Read the pages which came back.
<a name="82330"></a>foreach r $rfds {
<a name="82331"></a> if [ns_sockcheck $r] {
<a name="82332"></a> set page($r) [read $r]
<a name="82333"></a> }
<a name="82334"></a>}
<a name="82335"></a># Close all the sockets
<a name="82336"></a>foreach w [array names w2r] {
<a name="82337"></a> close $w
<a name="82338"></a> close $w2r($w)
<a name="82339"></a>}
</pre><p>
<TABLE BORDER="2" CELLPADDING="1" width="100%">
<TR><TD COLSPAN=3><P ALIGN=Center>
<IMG SRC="bluebult.gif">
<A HREF="#topofpage">
<FONT SIZE=-1>Top of Page</FONT></A>
<IMG SRC="bluebult.gif">
</TD></TR>
<TR><TD COLSPAN=3><P ALIGN=Center>
<A href="tapi-135.htm">
<IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm>
<IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm>
<IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="tapi-137.htm">
<IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<BR align=center>
<FONT size=-1>Copyright © 1998-99 America Online,
Inc.</FONT>
</TD></TR></TABLE></BODY></HTML>
|