日期:2008-09-26  浏览次数:20407 次

  
using System;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Collections;
using System.Diagnostics;
namespace NntpTools {
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 class Debug {
  /// <summary>
  /// 应用程序的主入口点。
  /// </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[]&nb