PHP能打开远程或本地主机上的Socket端口。本文是一个使用Socket的小例子:连 4fYLinux联盟 接到一个Usenet新闻组服务器,同服务器对话,从新闻组中下载一些文章。 4fYLinux联盟 4fYLinux联盟 在php中打开一个socket 4fYLinux联盟 使用fsockopen()打开一个socket.这个函数在php3和php4种都可以使用。函数声明 4fYLinux联盟 是这样的: 4fYLinux联盟 int fsockopen (string hostname, int port [, int errno [, string errstr [, double timeout]]]) 4fYLinux联盟 这个函数将打开一个连接到主机hostname的port端口的TCP连接。hostname可以是一 4fYLinux联盟 个有效的域名,或者是一个ip地址。对于udp连接,你必须指定协议:udp://hostname. 4fYLinux联盟 对于unix域,主机名使用到socket的路径,这种情况下,端口port必须置为0。可选 4fYLinux联盟 的timeout参数用来设定等待打开一个socket的时间,单位为秒。 4fYLinux联盟 关于fsockopen()的更多信息,请参考:h 4fYLinux联盟 ttp://www.php.net/manual/function.fsockopen.php 4fYLinux联盟 4fYLinux联盟 网络新闻传输协议 4fYLinux联盟 访问新闻组服务器需要通过称为NNTP(网络新闻传输协议)的协议来进行。这个协议 4fYLinux联盟 在rfc977中有详细的细节,可以在http://www.w3.org/Protocols/rfc977/rfc977.html 4fYLinux联盟 得到。这个文档分别描述了怎样连接到NNTP服务器,怎样同服务器对话,以及完成这 4fYLinux联盟 些任务的不同命令。 4fYLinux联盟 4fYLinux联盟 连接 4fYLinux联盟 连接到一个NNTP服务器需要知道它的主机名(或者是ip地址)和它侦听的端口。为了 4fYLinux联盟 避免一个连接企图失败导致程序挂起,你应该使用timeout参数。 4fYLinux联盟 <?php 4fYLinux联盟 $cfgServer = "your.news.host"; 4fYLinux联盟 $cfgPort = 119; 4fYLinux联盟 $cfgTimeOut = 10; 4fYLinux联盟 4fYLinux联盟 //open a socket 4fYLinux联盟 if(!$cfgTimeOut) 4fYLinux联盟 // without timeout 4fYLinux联盟 $usenet_handle = fsockopen($cfgServer, $cfgPort); 4fYLinux联盟 else 4fYLinux联盟 // with timeout 4fYLinux联盟 $usenet_handle = fsockopen($cfgServer, $cfgPort, &$errno, &$errstr, $cfgTimeOut); 4fYLinux联盟 4fYLinux联盟 if(!$usenet_handle) { 4fYLinux联盟 echo "Connection failed.n"; 4fYLinux联盟 exit(); 4fYLinux联盟 } 4fYLinux联盟 else { 4fYLinux联盟 echo "Connected.n"; 4fYLinux联盟 $tmp = fgets($usenet_handle, 1024); 4fYLinux联盟 } 4fYLinux联盟 4fYLinux联盟 ?> 4fYLinux联盟 4fYLinux联盟 4fYLinux联盟 与服务器对话 4fYLinux联盟 现在我们已经连接到了服务器,可以通过前面打开的socket同服务器对话了。比如 4fYLinux联盟 说我们要从某个新闻组得到最近的10篇文章。RFC977指出,第一步要用GROUP命令选择 4fYLinux联盟 正确的新闻组: 4fYLinux联盟 GROUP ggg 4fYLinux联盟 参数ggg是要选择的新闻组的名字(比如说是"net.news"),这是必需的。可用的新 4fYLinux联盟 闻组的列表可以用LIST命令得到。选择新闻组的命令成功后,返回组中第一篇和最 4fYLinux联盟 后一篇文章的文章编号,以及组中文章的数目。 4fYLinux联盟 下面是一个例子: 4fYLinux联盟 chrome:~$ telnet my.news.host 119 4fYLinux联盟 Trying aa.bb.cc.dd... 4fYLinux联盟 Connected to my.news.host. 4fYLinux联盟 Escape character is '^]'. 4fYLinux联盟 200 my.news.host InterNetNews NNRP server INN 2.2.2 13-Dec-1999 ready (posting ok). 4fYLinux联盟 GROUP alt.test 4fYLinux联盟 211 232 222996 223235 alt.test 4fYLinux联盟 quit 4fYLinux联盟 205 . 4fYLinux联盟 4fYLinux联盟 接收到命令 GROUP alt.test 后,服务器返回"211 232 222996 223235 alt.test". 4fYLinux联盟 211是RFC中定义的返回码,指示命令已成功执行。返回信息还指出,现在有232篇文 4fYLinux联盟 章,最早的文章的编号是222996,最新的文章的编号是223235。我们看到, 4fYLinux联盟 222996+232并不等于223235。丢失的7篇文章因为某种原因被从服务器删除了,可能 4fYLinux联盟 是因为被它的合法作者取消了(这是可能的,而且很容易做到),或者因为是灌水文 4fYLinux联盟 章而被删。 4fYLinux联盟 需要注意的事,有些服务器在选择新闻组之前可能要求身份认证,这取决于这是一个 4fYLinux联盟 公共的或者是私用的服务器。也有可能服务器允许任何人读取文章,但发表文章需要 4fYLinux联盟 身份验证。 4fYLinux联盟 4fYLinux联盟 <?php 4fYLinux联盟 4fYLinux联盟 //$cfgUser = "xxxxxx"; 4fYLinux联盟 //$cfgPasswd = "yyyyyy"; 4fYLinux联盟 $cfgNewsGroup = "alt.php"; 4fYLinux联盟 4fYLinux联盟 //identification required on private server 4fYLinux联盟 if($cfgUser) { 4fYLinux联盟 fputs($usenet_handle, "AUTHINFO USER ".$cfgUser."n"); 4fYLinux联盟 $tmp = fgets($usenet_handle, 1024); 4fYLinux联盟 fputs($usenet_handle, "AUTHINFO PASS ".$cfgPasswd."n"); 4fYLinux联盟 $tmp = fgets($usenet_handle, 1024); 4fYLinux联盟 4fYLinux联盟 //check error 4fYLinux联盟 4fYLinux联盟 if($tmp != "281 Okrn") { 4fYLinux联盟 echo "502 Authentication errorn"; 4fYLinux联盟 exit(); 4fYLinux联盟 } 4fYLinux联盟 } 4fYLinux联盟 4fYLinux联盟 //select newsgroup 4fYLinux联盟 4fYLinux联盟 fput($usenet_handle, "GROUP ".$cfgNewsGroup."n"); 4fYLinux联盟 $tmp = fgets($usenet_handle, 1024); 4fYLinux联盟 4fYLinux联盟 if($tmp == "480 Authentication required for commandrn") { 4fYLinux联盟 echo $tmp; 4fYLinux联盟 exit(); 4fYLinux联盟 } 4fYLinux联盟 4fYLinux联盟 $info = split(" ", $tmp); 4fYLinux联盟 $first= $info[2]; 4fYLinux联盟 $last = $info[3]; 4fYLinux联盟 4fYLinux联盟 printf("First : %sn", $first); 4fYLinux联盟 printf("Last : %lastn", $last); 4fYLinux联盟 4fYLinux联盟 ?> 4fYLinux联盟
|