standard.net.socket.SocketListen

SocketListen is available since version 1.0.

Prototype:

number SocketListen(number socket, number port, number max_connections_count=0x100)

Parameters

socket
a valid and socket descriptor, as returned by SocketCreate
port
the port to listen to
max_connections_count
the maximum number of simultaneous connections

Description:

Places a socket in a state in which it is listening for an incoming connection.

Return value:

Returns 0 if succeeded, or -1 if failed. This function can fail, if there is already a socket listening to the same port, or a firewall is not allowing the specified port to be used. Also fails if socket binding fails.

Example
import standard.net.socket
import standard.lib.thread


class ThreadClient {
	var socket;

	function ClientThread() {		
		var info=SocketInfo(socket);
		echo info;

		var i=0;
		while (SocketRead(socket, var buffer, 100)>0) {
			echo "Received: $buffer\n";

			// read just half the messages ...
			if (++i>5)
				break;
		}

		if (SocketHasData(socket)) {
			echo "Socket still has data waiting to be read !!!";
		}

		echo "Bye !";
		SocketClose(socket);
	}
}

class Main {

	function Main() {
		var s=SocketCreate();

		SocketListen(s,3500);

		do {
			var s_client=SocketAccept(s);
			echo "Accepted !";
			if (s_client>=0) {
				var cl=new ThreadClient();
				cl.socket=s_client;

				RunThread(cl.ClientThread);
			}
		} while (1);
	}
}