PHP can open Socket ports on a remote or local host. This article is a small example of using Socket: connection
Connect to a Usenet newsgroup server, talk to the server, and download some articles from the newsgroup.
Open a socket in php
Use fsockopen () to open a socket. This function can be used in both php3 and php4. Function declaration
Yes:
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. The hostname can be 1.
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. Optional
The timeout parameter is used to set the waiting time to open a socket, in seconds.
For more information about fsockopen (), refer to: h
Ttp: // www.php.net/manual/function.fsockopen.php
Network news transmission protocol
To access a newsgroup server, you must use a protocol called NNTP (network news transmission protocol. This protocol
Detailed details in rfc977 can be found 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 how to complete this
Different commands for some 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
To avoid program suspension caused by a connection attempt failure, you should use the timeout parameter.
<? Php
$ 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 );
}
?>
Dialog with server
Now that we have connected to the server, we can talk to the server through the socket opened previously. For example
We want to get the last 10 articles from a news group. RFC977 indicates that the first step is to use the GROUP command to select
Correct Newsgroup:
GROUP ggg
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.