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
|
<!--plsfield:text-->
<HTML><HEAD>
<TITLE>C API Reference -- Ns_SockPipe</TITLE>
<LINK rel=Previous href="c-ch364.htm">
<LINK rel=ToC href="toc.htm">
<LINK rel=Index href="master.htm">
<LINK rel=Next href="c-ch366.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="c-ch364.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="c-ch366.htm"> <IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<A name="7983"> </A>
</TD>
</TR>
</TABLE>
<a name="305382">
</a><h3>Ns_SockPipe</h3>
<a name="305559">
</a><h4>Overview</h4>
Return a pair of connected sockets
<a name="306303">
</a><a name="305560">
</a><h4>Syntax</h4>
<pre> <a name="306306"></a>int Ns_SockPipe (
<a name="306309"></a>SOCKET socks[2]
<a name="306310"></a>);
</pre><p><a name="305561">
</a><h4>Description</h4>
<p><a name="306315">
</a>Ns_SockPipe returns a pair of connected sockets. On Unix, Ns_SockPipe uses socketpair(). A socket pipe can be used for IPC between threads or as a way to wakeup a thread waiting in a select call as in the example below.</p>
<a name="305557">
</a><h4>Examples</h4>
<pre> <a name="306333"></a>SOCKET sockPipe[2];
<a name="306334"></a>/* Init - called at startup to create the pipe. */
<a name="306335"></a>void Init(void)
<a name="306336"></a>{
<a name="306337"></a> Ns_SockPipe(sockPipe);
<a name="306338"></a>}
<a name="306339"></a>/* Wakeup - called by another thread to stop InteruptableIO in
<a name="306340"></a> another thread. */
<a name="306341"></a>
<a name="307176"></a>void Wakeup(void)
<a name="306342"></a>{
<a name="306343"></a> send(sockPipe[1], "w", 1, 0);
<a name="306344"></a>}
<a name="306345"></a>/* InterruptableIO - called by a thread dedicated to reading from
<a name="306346"></a> a remote host. Reading will continue until another thread
<a name="306347"></a> calls Wakeup, causing sockPipe to be readable. */
<a name="306348"></a>
<a name="307191"></a>void InteruptableIO(void)
<a name="306349"></a>{
<a name="306350"></a> SOCKET sock, max;
<a name="306351"></a> fd_set set;
<a name="306352"></a> char sig;
<a name="306353"></a> sock = Ns_SockConnect("slowmachine", 6767);
<a name="306354"></a> FD_ZERO(&set);
<a name="306355"></a> FD_SET(sock, &set);
<a name="306356"></a> FD_SET(sockPipe[0], &set);
<a name="306357"></a> max = sockPipe;
<a name="306358"></a> if (sock > max) {
<a name="306359"></a> max = sock;
<a name="306360"></a> }
<a name="306361"></a> while (1) {
<a name="306362"></a> select(max+1, &set, NULL, NULL, NULL);
<a name="306363"></a> if (FD_ISSET(sockPipe[0], &set)) {
<a name="306364"></a> /* Another thread called Wakeup().
<a name="306365"></a> * Read the signal and return. */
<a name="306366"></a> recv(sockPipe[0], &sig, 1, 0);
<a name="306367"></a> closesocket(sock);
<a name="306368"></a> return;
<a name="306369"></a> } else if (FD_ISSET(sock, &set)) {
<a name="306370"></a> recv(sock, buf, sizeof(buf), 0);
<a name="306371"></a> ... process buf ...
<a name="306372"></a> }
<a name="306373"></a> }
<a name="306374"></a>}
<a name="306375"></a>
</pre><p><p><a name="306376">
</a><b>Note:</b> Interruptable I/O typically makes use of the alarm() system call on Unix. The method above, used throughout AOLserver, works on all platforms and avoids the alarm system call which is inappropriate for a multithreaded application.</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="c-ch364.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="c-ch366.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><!--plsfield:end-->
|