Use C # to interact with the NNTP server!

Source: Internet
Author: User
Tags continue count getstream connect nntp nntp server split tostring
Server | interaction
Using System;
Using System.Text;
Using System.Net;
Using System.IO;
Using System.Net.Sockets;
Using System.Collections;
Using System.Diagnostics;
Namespace Nntptools {
<summary>
Summary description of the CLASS1.
</summary>
Class Debug {
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main (string[] args) {
Nntpclass nc=new Nntpclass ();
nc. Connect ("msnews.microsoft.com");
ArrayList GROUPLIST=NC. Getnewsgroups ();
for (int i=0;i<grouplist. count;i++) {
Console.WriteLine (Grouplist[i]. ToString ());
}
ArrayList CARDLIST=NC. Getnews ("Microsoft.public.cn.dotnet.framework");
Console.WriteLine ("=============================================================");
StreamWriter Sw=file.createtext ("C:\\news.txt");
for (int i=0;i<cardlist. count;i++) {
Console.WriteLine (Cardlist[i]. ToString ());
Sw. WriteLine (Cardlist[i]. ToString ());
Sw. WriteLine ("=============================================================");
}
Sw. Flush ();
Sw. Close ();
nc. Disconnect ();

Console.ReadLine ();
}
}
Class nntpclass:system.net.sockets.tcpclient{
public void Connect (string server) {
string response;

Connect (server, 119);
Response = response ();
if (response. Substring (0, 3)!= "200") {
throw new Exception (response);
}

}
public void Disconnect () {
String message;
string response;

message = "quit\r\n";
Write (message);
Response = response ();
if (response. Substring (0, 3)!= "205") {
throw new Exception (response);
}
}
Public ArrayList getnewsgroups () {
String message;
string response;

ArrayList retval = new ArrayList ();

message = "list\r\n";
Write (message);
Response = response ();
if (response. Substring (0, 3)!= "215") {
throw new Exception (response);
}

while (true) {
Response = response ();
if (response = = ". \ r \ n" | |
Response = = ". \ n") {
return retval;
}
else {
Char[] SEPs = {'};
String[] values = response. Split (SEPs);
retval. ADD (Values[0]);
Continue
}
}
}
public void Post (string newsgroup, string subject, string from,
String content) {
String message;
string response;

message = "POST" + newsgroup + "\ r \ n";
Write (message);
Response = response ();
if (response. Substring (0, 3)!= "340") {
throw new Exception (response);
}

Message = ' From: ' + from + ' \ r \ n '
+ "Newsgroups:" + newsgroup + "\ r \ n"
+ "Subject:" + Subject + "\r\n\r\n"
+ content + "\r\n.\r\n";
Write (message);
Response = response ();
if (response. Substring (0, 3)!= "240") {
throw new Exception (response);
}
}

Public ArrayList getnews (string newsgroup) {
String message;
string response;

ArrayList retval = new ArrayList ();

message = "GROUP" + newsgroup + "\ r \ n";
Write (message);
Response = response ();
if (response. Substring (0, 3)!= "211") {
throw new Exception (response);
}

Char[] SEPs = {'};
String[] values = response. Split (SEPs);

Long start = Int32.Parse (values[2]);
Long end = Int32.Parse (values[3]);

if (start+100 < end && End > 100) {
start = end-100;
}

for (long i=start;i<end;i++) {
message = "ARTICLE" + i + "\ r \ n";
Write (message);
Response = response ();
if (response. Substring (0, 3) = = "423") {
Continue
}
if (response. Substring (0, 3)!= "220") {
throw new Exception (response);
}

String article = "";
while (true) {
Response = response ();
if (response = = ". \ r \ n") {
Break
}

if (response = = ". \ n") {
Break
}

if (article. Length < 1024) {
Article + = response;
};
}

retval. ADD (article);
}

return retval;
}

private String Response () {
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding ();
System.Text.Encoding Enc=encoding.default;
byte []serverbuff = new byte[1024];
NetworkStream stream = GetStream ();
int count = 0;
while (true) {
byte []buff = new byte[2];
int bytes = stream. Read (Buff, 0, 1);
if (bytes = = 1) {
Serverbuff[count] = buff[0];
count++;

if (buff[0] = = ' \ n ') {
Break
}
}
else {
Break
};
};

string retval = Enc. GetString (serverbuff, 0, Count);
System.Diagnostics.Debug.WriteLine ("READ:" + retval);
return retval;
}

private void Write (String message) {
System.Text.ASCIIEncoding en = new System.Text.ASCIIEncoding ();

byte[] WriteBuffer = new byte[1024];
WriteBuffer = en. GetBytes (message);

NetworkStream stream = GetStream ();
Stream. Write (writebuffer,0,writebuffer.length);

System.Diagnostics.Debug.WriteLine ("WRITE:" + message);


}

}
}
}

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.