欢迎光临
我们一直在努力

使用C#与NNTP服务器交互!-.NET教程,C#语言

建站超值云服务器,限时71元/月

  

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[] 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);

   

   

  }

 }

 }

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 使用C#与NNTP服务器交互!-.NET教程,C#语言
分享到: 更多 (0)

相关推荐

  • 暂无文章