国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

使用C#與NNTP服務器交互!

2024-07-21 02:17:27
字體:
來源:轉載
供稿:網友
  
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);
   
   
  }

 }
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涞源县| 岳阳县| 孟州市| 江永县| 邵武市| 临汾市| 河北区| 巫溪县| 万全县| 吉林省| 荆门市| 安乡县| 明溪县| 苍南县| 西宁市| 凤冈县| 夏津县| 贡山| 商河县| 伊通| 华安县| 凤冈县| 张家港市| 巴彦淖尔市| 进贤县| 深水埗区| 舟山市| 常德市| 横峰县| 辰溪县| 政和县| 图木舒克市| 临沂市| 西和县| 宣汉县| 福安市| 南漳县| 桂平市| 交城县| 怀远县| 江源县|