Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->编程语言 ->ASP.NET ->正文

使用C#与NNTP服务器交互!

来源:Linuxdby.com 作者:Webmaster 时间:2007-06-04 点击: [收藏] [投稿]

   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);
  
  
  }


 }

 }
}

 



 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:用C#生成中文汉字验证码的基本原理   下一篇:C#实现根据域名查询ip实例

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号