Using sockets: Getting Articles from newsgroups (i)

Source: Internet
Author: User
Tags connect nntp nntp server socket domain name
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 sockets: Connect to a Usenet newsgroup server, talk to servers, and download articles from newsgroups.

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 port ports connected to 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 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 a socket to open.
For more information on fsockopen (), please refer to: http://www.php.net/manual/function.fsockopen.php

Network News Transfer Protocol
Access to a newsgroup server is required through a protocol called NNTP (Network News Transfer Protocol). This agreement has detailed details in the rfc977 and 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 to complete these tasks.

Connection
Connecting to an NNTP server requires knowing its host name (or IP address) and the port it listens on. To avoid a connection attempt failure causing the program to hang, you should use the timeout parameter.
<?php
$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.\n";
Exit ();
}
else {
echo "connected.\n";
$tmp = fgets ($usenet _handle, 1024);
}

?>

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.