<HTML ><HEAD ><TITLE >socket_select</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" 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-1"></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" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.socket-recvmsg.html" ACCESSKEY="P" >Prev</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" >Next</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="AEN87866" ></A ><P > (PHP 4 >= 4.1.0)</P >socket_select -- Runs the select() system call on the given arrays of sockets with a timeout specified by tv_sec and tv_usec </DIV ><DIV CLASS="refsect1" ><A NAME="AEN87869" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >socket_select</B > ( resource &read, resource &write, resource &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 >Warning</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >This function is <SPAN CLASS="emphasis" ><I CLASS="emphasis" >EXPERIMENTAL</I ></SPAN >. The behaviour of this function, the name of this function, and anything else documented about this function may change without notice in a future release of PHP. Use this function at your own risk.</P ></TD ></TR ></TABLE ></DIV ><P > The <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 <TT CLASS="parameter" ><I >read</I ></TT > 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 <TT CLASS="parameter" ><I >write</I ></TT > array will be watched to see if a write will not block. </P ><P > The sockets listed in the <TT CLASS="parameter" ><I >except</I ></TT > array will be watched for exceptions. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warning</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 > Example: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >/* Prepare the read array */ $read = array($socket1, $socket2); if (false === ($num_changed_sockets = socket_select($read, $write = NULL, $except = NULL, 0))) { /* Error handling */ else if ($num_changed_sockets > 0) { /* At least at one of the sockets something interesting happened */ }</PRE ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </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 BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >socket_select($r, $w, $e = NULL, 0);</PRE ></TD ></TR ></TABLE > </P ></BLOCKQUOTE ></DIV ><P > The <TT CLASS="parameter" ><I >tv_sec</I ></TT > and <TT CLASS="parameter" ><I >tv_usec</I ></TT > 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. <TT CLASS="parameter" ><I >tv_sec</I ></TT > may be zero , causing <B CLASS="function" >socket_select()</B > to return immediately. This is useful for polling. If <TT CLASS="parameter" ><I >tv_sec</I ></TT > 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 resorces 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 >Note: </B > Be sure to use the <TT CLASS="literal" >===</TT > operator when checking for an error. Since the <B CLASS="function" >socket_select()</B > may return 0 the comparison with <TT CLASS="literal" >==</TT > would evaluate to <TT CLASS="constant" ><B >TRUE</B ></TT >: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >if (false === socket_select($r, $w, $e = NULL, 0)) { echo "socket_select() failed, reason: " . socket_strerror(socket_last_error()) . "\n"; }</PRE ></TD ></TR ></TABLE > </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </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 <TT CLASS="parameter" ><I >except</I ></TT > 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" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.socket-send.html" ACCESSKEY="N" >Next</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" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >socket_send</TD ></TR ></TABLE ></DIV ></BODY ></HTML >