<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >socket_select</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" HREF="index.html"><LINK REL="UP" TITLE="Socket Functions" HREF="ref.sockets.html"><LINK REL="PREVIOUS" TITLE="socket_recvmsg" HREF="function.socket-recvmsg.html"><LINK REL="NEXT" TITLE="socket_send" HREF="function.socket-send.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-2"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.socket-recvmsg.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.socket-send.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.socket-select" ></A >socket_select</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN109230" ></A ><P > (PHP 4 >= 4.1.0, PHP 5)</P >socket_select -- Runs the select() system call on the given arrays of sockets with a specified timeout </DIV ><DIV CLASS="refsect1" ><A NAME="AEN109233" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >socket_select</B > ( array &read, array &write, array &except, int tv_sec [, int tv_usec])<BR ></BR ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Avertisment</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >Aceastã funcþie este <SPAN CLASS="emphasis" ><I CLASS="emphasis" >EXPERIMENTALÃ</I ></SPAN >. Comportamentul acestei funcþii, numele funcþiei, ºi orice altceva documentat despre aceastã funcþie ar putea fi schimbate farã un anunþ prealabil, intr-o viitoare versiune PHP. V-am avertizat, deci folosiþi aceastã extensie pe riscul dv.</P ></TD ></TR ></TABLE ></DIV ><P > <B CLASS="function" >socket_select()</B > accepts arrays of sockets and waits for them to change status. Those coming with BSD sockets background will recognize that those socket resource arrays are in fact the so-called file descriptor sets. Three independent arrays of socket resources are watched. </P ><P > The sockets listed in the <VAR CLASS="parameter" >read</VAR > array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a socket resource is also ready on end-of-file, in which case a <A HREF="function.socket-read.html" ><B CLASS="function" >socket_read()</B ></A > will return a zero length string). </P ><P > The sockets listed in the <VAR CLASS="parameter" >write</VAR > array will be watched to see if a write will not block. </P ><P > The sockets listed in the <VAR CLASS="parameter" >except</VAR > array will be watched for exceptions. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Avertisment</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > On exit, the arrays are modified to indicate which socket resource actually changed status. </P ></TD ></TR ></TABLE ></DIV ><P > You do not need to pass every array to <B CLASS="function" >socket_select()</B >. You can leave it out and use an empty array or <TT CLASS="constant" ><B >NULL</B ></TT > instead. Also do not forget that those arrays are passed <SPAN CLASS="emphasis" ><I CLASS="emphasis" >by reference</I ></SPAN > and will be modified after <B CLASS="function" >socket_select()</B > returns. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN109273" ></A ><P ><B >Exemplu 1. <B CLASS="function" >socket_select()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">/* Prepare the read array */<br /></font><font color="#0000BB">$read </font><font color="#007700">= array(</font><font color="#0000BB">$socket1</font><font color="#007700">, </font><font color="#0000BB">$socket2</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$num_changed_sockets </font><font color="#007700">= </font><font color="#0000BB">socket_select</font><font color="#007700">(</font><font color="#0000BB">$read</font><font color="#007700">, </font><font color="#0000BB">$write </font><font color="#007700">= </font><font color="#0000BB">NULL</font><font color="#007700">, </font><font color="#0000BB">$except </font><font color="#007700">= </font><font color="#0000BB">NULL</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">);<br /><br />if (</font><font color="#0000BB">$num_changed_sockets </font><font color="#007700">=== </font><font color="#0000BB">false</font><font color="#007700">) {<br /> </font><font color="#FF8000">/* Error handling */<br /></font><font color="#007700">} else if (</font><font color="#0000BB">$num_changed_sockets </font><font color="#007700">> </font><font color="#0000BB">0</font><font color="#007700">) {<br /> </font><font color="#FF8000">/* At least at one of the sockets something interesting happened */<br /></font><font color="#007700">}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > Due a limitation in the current Zend Engine it is not possible to pass a constant modifier like <TT CLASS="constant" ><B >NULL</B ></TT > directly as a parameter to a function which expects this parameter to be passed by reference. Instead use a temporary variable or an expression with the leftmost member being a temporary variable: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN109280" ></A ><P ><B >Exemplu 2. Using <TT CLASS="constant" ><B >NULL</B ></TT > with <B CLASS="function" >socket_select()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />socket_select</font><font color="#007700">(</font><font color="#0000BB">$r</font><font color="#007700">, </font><font color="#0000BB">$w</font><font color="#007700">, </font><font color="#0000BB">$e </font><font color="#007700">= </font><font color="#0000BB">NULL</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></BLOCKQUOTE ></DIV ><P > The <VAR CLASS="parameter" >tv_sec</VAR > and <VAR CLASS="parameter" >tv_usec</VAR > together form the <SPAN CLASS="emphasis" ><I CLASS="emphasis" >timeout</I ></SPAN > parameter. The <SPAN CLASS="emphasis" ><I CLASS="emphasis" >timeout</I ></SPAN > is an upper bound on the amount of time elapsed before <B CLASS="function" >socket_select()</B > return. <VAR CLASS="parameter" >tv_sec</VAR > may be zero , causing <B CLASS="function" >socket_select()</B > to return immediately. This is useful for polling. If <VAR CLASS="parameter" >tv_sec</VAR > is <TT CLASS="constant" ><B >NULL</B ></TT > (no timeout), <B CLASS="function" >socket_select()</B > can block indefinitely. </P ><P > On success <B CLASS="function" >socket_select()</B > returns the number of socket resources contained in the modified arrays, which may be zero if the timeout expires before anything interesting happens. On error <TT CLASS="constant" ><B >FALSE</B ></TT > is returned. The error code can be retrieved with <A HREF="function.socket-last-error.html" ><B CLASS="function" >socket_last_error()</B ></A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > Be sure to use the <VAR CLASS="literal" >===</VAR > operator when checking for an error. Since the <B CLASS="function" >socket_select()</B > may return 0 the comparison with <VAR CLASS="literal" >==</VAR > would evaluate to <TT CLASS="constant" ><B >TRUE</B ></TT >: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN109306" ></A ><P ><B >Exemplu 3. Understanding <B CLASS="function" >socket_select()</B >'s result</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">if (</font><font color="#0000BB">false </font><font color="#007700">=== </font><font color="#0000BB">socket_select</font><font color="#007700">(</font><font color="#0000BB">$r</font><font color="#007700">, </font><font color="#0000BB">$w</font><font color="#007700">, </font><font color="#0000BB">$e </font><font color="#007700">= </font><font color="#0000BB">NULL</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"socket_select() failed, reason: " </font><font color="#007700">.<br /> </font><font color="#0000BB">socket_strerror</font><font color="#007700">(</font><font color="#0000BB">socket_last_error</font><font color="#007700">()) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > Be aware that some socket implementations need to be handled very carefully. A few basic rules: <P ></P ><UL ><LI ><P > You should always try to use <B CLASS="function" >socket_select()</B > without timeout. Your program should have nothing to do if there is no data available. Code that depends on timeouts is not usually portable and difficult to debug. </P ></LI ><LI ><P > No socket resource must be added to any set if you do not intend to check its result after the <B CLASS="function" >socket_select()</B > call, and respond appropriately. After <B CLASS="function" >socket_select()</B > returns, all socket resources in all arrays must be checked. Any socket resource that is available for writing must be written to, and any socket resource available for reading must be read from. </P ></LI ><LI ><P > If you read/write to a socket returns in the arrays be aware that they do not necessarily read/write the full amount of data you have requested. Be prepared to even only be able to read/write a single byte. </P ></LI ><LI ><P > It's common to most socket implementations that the only exception caught with the <VAR CLASS="parameter" >except</VAR > array is out-of-bound data received on a socket. </P ></LI ></UL > </P ></BLOCKQUOTE ></DIV ><P > See also <A HREF="function.socket-read.html" ><B CLASS="function" >socket_read()</B ></A >, <A HREF="function.socket-write.html" ><B CLASS="function" >socket_write()</B ></A >, <A HREF="function.socket-last-error.html" ><B CLASS="function" >socket_last_error()</B ></A > and <A HREF="function.socket-strerror.html" ><B CLASS="function" >socket_strerror()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.socket-recvmsg.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.socket-send.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >socket_recvmsg</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.sockets.html" ACCESSKEY="U" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >socket_send</TD ></TR ></TABLE ></DIV ></BODY ></HTML >