Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the socket function to get articles from newsgroups

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use the socket function to get articles from newsgroups. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

What does the socket function declaration look like

Intfsockopen (stringhostname,intport_

[, interrno [, stringerrstr [, doubletimeout]])

This function opens a TCP connection to the port 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 hostname uses the path to socket, in which case the port port must be set to 0. The optional timeout parameter is used to set the time, in seconds, to wait for a socket to be opened.

Network news transmission protocol

Access to the newsgroup server requires a protocol called NNTP (Network News transfer Protocol). This document describes how to connect to the NNTP server, how to talk to the server, and the different commands to accomplish these tasks.

Connect

To connect to a NNTP server, you need to know its hostname (or ip address) and the port it listens on. To prevent a failed connection attempt from causing the program to hang, you should use the timeout parameter.

$cfgServer= "your.news.host"

$cfgPort=119

$cfgTimeOut=10

/ / openasocket

If (! $cfgTimeOut)

/ / withouttimeout

$usenet_handle=fsockopen ($cfgServer,$cfgPort)

Else

/ / withtimeout

$usenet_handle=fsockopen ($cfgServer,$cfgPort,&$errno,&$errstr,$cfgTimeOut)

If (! $usenet_handle) {

Echo "Connectionfailed.\ n"

Exit ()

}

Else {

Echo "Connected.\ n"

$tmp=fgets ($usenet_handle,1024)

}

? >

Talk to the server

Now that we are connected to the server, we can talk to the server through the socket that we opened earlier. For example, we need to get the last 10 articles from a newsgroup. RFC977 points out that the first step is to select the correct newsgroup with the GROUP command:

GROUPggg

The parameter ggg is the name of the newsgroup to be selected (for example, "net.news"), which is required. A list of available newsgroups can be obtained with the LIST command. When the command to select a newsgroup is successful, return the article numbers of the first and last articles in the group, as well as the number of articles in the group.

Here is an example:

Chrome:~$telnetmy.news.host119

Tryingaa.bb.cc.dd...

Connectedtomy.news.host.

Escapecharacteris' ^]'.

200my.news.hostInterNetNewsNNRPserverINN2.2.213-Dec-1999ready (postingok).

GROUPalt.test

211232222996223235alt.test

Quit

two hundred and five。

After receiving the command GROUPalt.test, the server returns "211232222996223235alt.test" .211, which is the return code defined in RFC, indicating that the command was executed successfully. The return message also indicates that there are now 232 articles, with the earliest article numbering 222996 and the latest article numbering 223235. We see that 22299632 is not equal to 223235. The missing seven articles were deleted from the server for some reason, either because they were cancelled by its legitimate author (which is possible and easy to do), or because they were irrigated articles.

It is important to note that some servers may require authentication before selecting a newsgroup, depending on whether it is a public or private server. It is also possible that the server allows anyone to read articles, but publishing articles requires authentication.

/ / $cfgUser= "xxxxxx"

/ / $cfgPasswd= "yyyyyy"

$cfgNewsGroup= "alt.php"

/ / identificationrequiredonprivateserver

If ($cfgUser) {

Fputs ($usenet_handle, "AUTHINFOUSER". $cfgUser. "n")

$tmp=fgets ($usenet_handle,1024)

Fputs ($usenet_handle, "AUTHINFOPASS". $cfgPasswd. "n")

$tmp=fgets ($usenet_handle,1024)

/ / checkerror

If ($tmpins = "281Okrn") {

Echo "502Authenticationerrorn"

Exit ()

}

}

/ / selectnewsgroup

Fput ($usenet_handle, "GROUP". $cfgNewsGroup. "n")

$tmp=fgets ($usenet_handle,1024)

If ($tmp== "480Authenticationrequiredforcommandrn") {

Echo$tmp

Exit ()

}

$info=split ("", $tmp)

$first=$info [2]

$last=$info [3]

Printf ("First:%sn", $first)

Printf ("Last:%lastn", $last)

? >

Thank you for reading! This is the end of the article on "how to use the socket function to get articles from newsgroups". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report