contents   index   previous   next



Socket.select()

syntax:

Socket.select([timeout ,] socket1[, socket2

              ...])

where:

timeout - Maximum time to poll, in milliseconds, -1 for no timeout

 

socketN - A list of sockets to poll for data, or an array of sockets

 

return:

object - The first supplied socket object which is ready for reading, or null if none is ready before the timeout is reached.

 

description:

This method is an alternative to Socket ready(). The other ready method is a property of Socket instances, and only polls the current socket for data. This global method allows for polling of multiple sources, which is needed when multiple sockets are open. When any of the specified sockets are ready to be read from, then this method returns the first socket which is so ready. Note that these sockets can be either connected sockets or listening sockets. A listening socket that is ready to be read from means that a request is waiting. If no timeout is specified, then -1 (infinite) timeout is used.

 

see:

#link <sesock>, Socket ready()

 

example:

var listenSocket1 = Socket( 1000 );

var listenSocket2 = Socket( 1001 );

 

// Assume 'done' is a global flag somewhere

if( listenSocket1 != null && listenSocket2 != null )

{

   while( !done )

   {

      var acceptSocket;

      if((acceptSocket = Socket.select(100,

          [listenSocket1, listenSocket2])) != null)

      {

         // Connect with socket ...

      }

 

      // Do other stuff ...

   }

}

 

/* This code opens two sockets for listening,

 * and then continuously polls

 * these two sockets for incoming connections.

 * Note that in a real

 * program, it would be better to create

 * a dynamic array which holds all

 * of the open sockets.

 */