contents   index   previous   next



Socket() with port

syntax:

Socket(port)

where:

port - Port to listen on

 

return:

object - A Socket object, or null on error.

 

description:

There are two types of sockets, in general. One type is a socket which is an established connection between a client and a server. This socket can be read to and written from just like a file. The other type of socket is a listening socket, which is a server-side socket which is not connected to a specific client, but rather to a certain port. It is listening for any new requests on that port. Requests can be checked for using the Socket.select() method. Once it is established that there is a request waiting, the peer-to-peer connection can be established using the accept() method. This creates a new connection socket on another port, leaving the original socket still listening for incoming connections.

 

see:

#link <sesock>, Socket select(), Socket accept()

 

example:

var listenSocket = Socket( 1000 );

 

if( listenSocket != null )

{

   if( listenSocket.ready() )

   {

      var connectSocket = listenSocket.accept();

      if( connectSocket != null )

      {

         // Finally! we have the socket

         // ... do stuff with socket ...

         connectSocket.close();

      }

   }

}

 

/* Creates a socket to listen on port 1000

 * and wait for any incoming

 * connections.  The no-parameter form

 * of ready() uses an infinite

 * timeout, so the program waits indefinitely

 * for a connection.  This is

 * also equiavalent to

 * "Socket.select(-1,listenSocket)", which is a

 * generic form which allows for

 * listening on multiple sockets.

 */

 


Socket accept()