Use sockets: obtain an article from a newsgroup (1)

Source: Internet
Author: User
Tags nntp nntp server
ByArmelFauveau & nbsp; Yimu translation & nbsp; php (as the mainstream development language) enable the Socket port on the remote or local host. This article is a small example of using Socket: connect to a Usenet newsgroup server, talk to the server, and download some articles from the newsgroup By Armel Fauveau
Php (as the mainstream development language) can open Socket ports on remote or local hosts. This article is a small example of using Socket: connect to a Usenet newsgroup server, talk to the server, and download some articles from the newsgroup.

Open a socket in php (as the mainstream development language)
Use fsockopen () to open a socket. This function can be used in php (as the mainstream development language) 3 and php (as the mainstream development language. The function declaration is as follows:
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. 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 socket path. in this case, the port must be set to 0. The optional timeout parameter is used to set the waiting time for opening a socket, in seconds.
For more information about fsockopen (), see: http://www.php (as the mainstream development language). net/manual/function. fsockopen. php (as the mainstream development language)

Network news transmission protocol
To access a newsgroup server, you must use a protocol called NNTP (network news transmission protocol. This protocol has detailed details in rfc977, which can be obtained at 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 used to complete these tasks.

Connection
To connect to an NNTP server, you need to know its host name (or IP address) and the port on which it listens. To avoid program suspension caused by a connection attempt failure, you should use the timeout parameter.
$ Monitoring server = "your. news. host ";
$ Export Port = 119;
$ Required timeout = 10;

// Open a socket
If (! $ Timeout)
// Without timeout
$ Usenet_handle = fsockopen ($ container server, $ container port );
Else
// With timeout
$ Usenet_handle = fsockopen ($ response server, $ response port, & $ errno, & $ errstr, $ response timeout );

If (! $ Usenet_handle ){
Echo "Connection failed .";
Exit ();
}
Else {
Echo "Connected .";
$ 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.