Using Sockets in PHP: getting files from Usenet

Source: Internet
Author: User
Tags nntp nntp server requires socket

PHP can open a remote or local server sockets! Here is a simple example of using sockets: Connect to a Usenet news server, communicate with the server, and download some articles from an accurate newsgroup.

Open the socket with PHP

Use Fsockopen () to open a socket. This function is present in both PHP3 and PHP4. The prototype of the function is as follows:

<?php
intfsockopen
  (string hostname,
    int port[,
    int errno[,
    string errstr[,
    double timeout]]])
?>

For a network host, it will establish a TCP socket connected to the host name of the port. The host name can be a domain name or an IP address. For UDP connections, you need to explicitly indicate their protocol: Udp://hostname. For UNIX hosts, the host name is used in the socket path, in which case the port must be set to 0. Optional timeout can be used to set the number of seconds the connection times out.

More information about Fsockopen () can be accessed by http://www.php.net/manual/function.fsockopen.php

Network News Transfer Protocol (NNTP)

Accessing a Usenet news server requires a special protocol called NNTP, the Network News Transfer Protocol standard. The details of this agreement are in RFC977 and you can see it in http://www.w3.org/Protocols/rfc977/rfc977.html. This document describes in detail how to use different commands to connect and talk to the NNTP server.

Connecting to a server

Connecting to an NNTP server requires knowing the host name (or IP address) of the server and the port it will listen to. It is also recommended that you add a timeout so that the connection does not "freeze" the program when it fails.

<?php
$cfgServer  ="your.news.host";
$cfgPort  =119;
$cfgTimeOut  =10;
// open asocket
if(!$cfgTimeOut)
  // without timeout
  $usenet_handle=fsockopen($cfgServer,$cfgPort);
else
  // with timeout
  $usenet_handle=fsockopen($cfgServer,$cfgPort, &$errno, &$errstr,$cfgTimeOut);
if(!$usenet_handle) {
  echo"Connexionfailed\n";
  exit();
}  
else {
  echo"Connected\n";
  $tmp=fgets($usenet_handle,1024);
}
?>

Interacting with the server

Now that we are connected to the server, we are able to interact with the server through a previously open socket connection. Let's say to the server, "we're going to get the latest 10 articles from a newsgroup." RFC977 defines the command to select the correct newsgroup, as follows:

Groupggg

Required parameter GGG is the name of the newsgroup you are going to select, such as Net.news. Using the list command you can get a list of valid news lists. A successful selection response returns the news number of the two-story news in the group and an estimate of the archived news number.

Like what

chrome:~$ telnetmy.news.host 119
Trying aa.bb.cc.dd...
Connected tomy.news.host.
Escape character is'^]'.
200 my.news.hostInterNetNews NNRP server INN 2.2.2 13-Dec-1999 ready (posting ok).
GROUP alt.test
211 232 222996 223235alt.test
quit
205 .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.