Previous section To contents Next section

9.4 Listening to sockets - Stdio.Port

CLASS
Stdio.Port Stdio.File can handle connections to any TCP socket, but it can not listen to a local TCP port. For this purpose there is a special class called Stdio.Port. Stdio.Port cannot read or write any data, but it can accept connections which will be returned as clones of Stdio.File. These are the methods available in Stdio.Port:

METHOD
Stdio.Port.bind - open socket and bind it to a port

SYNTAX
int bind(int port);
int bind(int port,function accept_callback);
int bind(int port,function accept_callback, string IP);

DESCRIPTION
Bind opens a sockets and binds it to port number on the local machine. If the second argument is present, the socket is set to nonblocking and the callback function is called whenever something connects to the socket. The callback will receive the id for this port as argument. Bind returns 1 on success, and zero on failure.

If the optional argument IP is given, bind will try to bind to this IP name (or number).

SEE ALSO
Stdio.Port->accept

METHOD
Stdio.Port.listen_fd - listen to an already open port

SYNTAX
int listen_fd(int fd);
int listen_fd(int fd,function accept_callback);

DESCRIPTION
This function does the same as Stdio.Port->bind, except that instead of creating a new socket and bind it to a port, it expects that the file descriptor fd is an already open port.

NOTE
This function is only for the advanced user, and is generally used when sockets are passed to Pike at exec time.

SEE ALSO
Stdio.Port->bind and Stdio.Port->accept

METHOD
Stdio.Port.create - create and/or setup a port

SYNTAX
object(Stdio.Port) Stdio.Port("stdin")
object(Stdio.Port) Stdio.Port("stdin",function accept_callback)
object(Stdio.Port) Stdio.Port("stdin",function accept_callback)
object(Stdio.Port) Stdio.Port(int port)
object(Stdio.Port) Stdio.Port(int port,function accept_callback)
object(Stdio.Port) Stdio.Port(int port,function accept_callback, string ip)

DESCRIPTION
When create is called with "stdin" as argument, a socket is created out of the file descriptor 0. This is only useful if that actually is a socket to begin with. When create is called with an int as first argument, it does the same as bind() would do with the same arguments. The second and third argument has the same function as in the bind() call.

SEE ALSO
clone and Stdio.Port->bind

METHOD
Stdio.Port.set_id - set the id of a port

SYNTAX
void set_id(mixed id);

DESCRIPTION
This function sets the id used for accept_callback by this port. The default id is this_object().

SEE ALSO
Stdio.Port->query_id

METHOD
Stdio.Port.query_id - Return the id for this port.

SYNTAX
mixed query_id();

DESCRIPTION
This function returns the id for this port. The id is normally the first argument to accept_callback.

SEE ALSO
Stdio.Port->set_id

METHOD
Stdio.Port.errno - return the last error

SYNTAX
int errno();

DESCRIPTION
If the last call done on this port failed, errno will return an integer describing what went wrong. Refer to your Unix manual for further information.

SEE ALSO
Stdio.Port->errno

METHOD
Stdio.Port.accept - accept a connection

SYNTAX
object accept();

DESCRIPTION
This function completes a connection made from a remote machine to this port. It returns a two-way stream in the form of a copy of Stdio.File. The new file is by default set to blocking.

SEE ALSO
Stdio.File

Previous section To contents Next section