通过Socket与NNTP服务器交互及GROUP命令解析
与服务器建立对话
完成连接后,即可通过已打开的Socket通道与服务器进行交互。例如,若需获取特定新闻组最新的10篇文章,根据RFC977规范,首要步骤是使用GROUP命令来选定目标新闻组。
GROUP ggg
参数ggg代表待选定的新闻组名称(如"net.news"),此为必填项。可通过LIST命令查询所有可用的新闻组列表。当GROUP命令执行成功后,服务器将返回该组中第一篇和最后一篇文章的编号,以及组内文章的总数。
以下是一个具体的交互示例:
chrome:~$ telnet my.news.host 119
Trying aa.bb.cc.dd...
Connected to my.news.host.
Escape character is '^]'.
200 my.news.host InterNetNews NNRP server INN 2.2.2 13-Dec-1999 ready (posting ok).
GROUP alt.test
211 232 222996 223235 alt.test
quit
205 .
在接收到 GROUP alt.test 命令后,服务器响应 "211 232 222996 223235 alt.test"。其中211是RFC定义的成功返回码。该响应表明当前共有232篇文章,起始编号为222996,最新编号为223235。值得注意的是,222996加232并不等于223235,缺失的7篇文章可能因作者撤销或被视为垃圾信息而被删除。
需注意,部分服务器在选择新闻组前要求身份认证,这取决于服务器是公共还是私有性质。有些服务器允许公开读取,但发帖需验证;有些则全程限制访问。
<?php //$cfgUser = "xxxxxx";
//$cfgPasswd = "yyyyyy";
$cfgNewsGroup = "alt.php";
//identification required on private server
if($cfgUser) {
fputs($usenet_handle, "AUTHINFO USER ".$cfgUser."n");
$tmp = fgets($usenet_handle, 1024);
fputs($usenet_handle, "AUTHINFO PASS ".$cfgPasswd."n");
$tmp = fgets($usenet_handle, 1024);
//check error
if($tmp != "281 Okrn") {
echo "502 Authentication errorn";
exit();
}
} //select newsgroup
fput($usenet_handle, "GROUP ".$cfgNewsGroup."n");
$tmp = fgets($usenet_handle, 1024);
if($tmp == "480 Authentication required for commandrn") {
echo $tmp;
exit();
} $info = split(" ", $tmp);
$first= $info[2];
$last = $info[3];
printf("First : %sn", $first);
printf("Last : %lastn", $last);
?>