By Armel Fauveau a wooden translation
PHP can open a socket port on a remote or local host. This article is a small example of using a 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 this:
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. The 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 path to the socket, in which case port ports must be set to 0. The optional timeout parameter is used to set the time, in seconds, to wait for the opening of a socket.
For more information about Fsockopen (), please refer to: http://www.php.net/manual/function.fsockopen.php
Network News Transfer Protocol
Accessing a newsgroup server requires a protocol called NNTP (Network News Transfer Protocol). This protocol has detailed details in the rfc977, which can be obtained in 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 for accomplishing these tasks.
Connection
Connecting to an NNTP server requires knowing its hostname (or IP address) and the port it listens to. To avoid a connection attempt failure that causes the program to hang, you should use the timeout parameter.
$cfgServer = "Your.news.host";
$cfgPort = 119;
$cfgTimeOut = 10;
Open a socket
if (! $cfgTimeOut)
Without timeout
$usenet _handle = Fsockopen ($cfgServer, $cfgPort);
Else
With timeout
$usenet _handle = Fsockopen ($cfgServer, $cfgPort, & $errno, & $errstr, $cfgTimeOut);
if (! $usenet _handle) {
echo "Connection failed.";
Exit ();
}
else {
echo "Connected.";
$tmp = fgets ($usenet _handle, 1024);
}
?>
http://www.bkjia.com/PHPjc/531742.html www.bkjia.com true http://www.bkjia.com/PHPjc/531742.html techarticle by Armel Fauveau PHP can open a socket port on a remote or local host. This article is a small example of using a socket: Connect to a Usenet newsgroup server, talk to a server ...