Translated By Armel Fauveau
PHP can open Socket ports on a remote or local host. This article is a small example of using Socket: connect to a Usenet newsgroup server, talk to the server, and download some articles from the newsgroup.
Open a socket in php
Use fsockopen () to open a socket. This function can be used in both php3 and php4. The function declaration is as follows:
Int fsockopen (string hostname, int port [, int errno [, string errstr [, double timeout])
This function will open a TCP connection to the port of the host hostname. Hostname can be a valid domain name or an IP address. For udp connections, you must specify the Protocol: udp: // hostname. For unix domains, the host name uses the socket path. In this case, the port must be set to 0. The optional timeout parameter is used to set the waiting time for opening a socket, in seconds.
For more information about fsockopen (), see: http://www.php.net/manual/function.fsockopen.php
Network news transmission protocol
To access a newsgroup server, you must use a protocol called NNTP (network news transmission protocol. This Protocol has detailed details in rfc977, which can be obtained at http://www.w3.org/protocols/rfc977/rfc977.html. This document describes how to connect to the NNTP server, how to talk to the server, and the different commands used to complete these tasks.
Connection
To connect to an NNTP server, you need to know its host name (or IP address) and the port on which it listens. To avoid program suspension caused by a connection attempt failure, you should use the timeout parameter.
<? Php
$ Monitoring Server = "your. news. host ";
$ Export port = 119;
$ Required timeout = 10;
// Open a socket
If (! $ Timeout)
// Without timeout
$ Usenet_handle = fsockopen ($ container server, $ Container Port );
Else
// With timeout
$ Usenet_handle = fsockopen ($ response server, $ Response port, & $ errno, & $ errstr, $ response timeout );
If (! $ Usenet_handle ){
Echo "Connection failed. \ n ";
Exit ();
}
Else {
Echo "Connected. \ n ";
$ Tmp = fgets ($ usenet_handle, 1024 );
}
?>