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

using System;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Collections;
using System.Diagnostics;

namespace NntpTools {
///

1<summary>   
2/// Class1 的摘要说明。   
3/// </summary>

class Debug {
///

1<summary>   
2/// 应用程序的主入口点。   
3/// </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

  1<grouplist.count;i++){ !="211" "="" "\r\n"="" "\r\n";="" "\r\n.\r\n";="" "\r\n\r\n"="" "newsgroups:="" "subject:="" &&="" '="" (response=".\r\n" (response.substring(="" (start+100="" (true)="" )="" +="" 0,="" 200")="" 3)="" ;="" <="" \+="" arraylist="" arraylist();="" c:\\\news.txt");="" cardlist='nc.GetNews("microsoft.public.cn.dotnet.framework");' char[]="" console.writeline("=");   
  2StreamWriter sw=File.CreateText(" console.writeline(cardlist[i].tostring());="" console.writeline(grouplist[i].tostring());="" content="" content)="" continue;="" disconnect()="" else="" end="" exception(response);="" for(int="" from="" from,="" getnews(string="" getnewsgroups()="" i="0;i&lt;cardlist.Count;i++){" if="" long="" message="GROUP " message;="" new="" newsgroup="" newsgroup)="" newsgroup,="" post(string="" public="" response="Response();" response;="" return="" retval="new" retval.add(values[0]);="" retval;="" seps="{" start="Int32.Parse(values[2]);" string="" string[]="" subject="" subject,="" sw.writeline("=");   
  3}   
  4sw.Flush();   
  5sw.Close();   
  6nc.Disconnect();   
  7  
  8Console.ReadLine();   
  9}   
 10} 
 11
 12class NNTPClass:System.Net.Sockets.TcpClient{   
 13public void Connect(string server){   
 14string response; 
 15
 16Connect(server, 119); 
 17
 18response = Response(); 
 19
 20if (response.Substring( 0, 3) != " sw.writeline(cardlist[i].tostring());="" throw="" values="response.Split(seps);" void="" while="" write(message);="" {="" ||="" }="" };=""> 100) { 
 21
 22start = end-100; 
 23
 24} 
 25
 26for (long i=start;i&lt;end;i++) { 
 27
 28message = "ARTICLE " + i + "\r\n"; 
 29
 30Write(message); 
 31
 32response = Response(); 
 33
 34if (response.Substring( 0, 3) == "423") { 
 35
 36continue; 
 37
 38} 
 39
 40if (response.Substring( 0, 3) != "220") { 
 41
 42throw new Exception(response); 
 43
 44} 
 45
 46string article = ""; 
 47
 48while (true) { 
 49
 50response = Response(); 
 51
 52if (response == ".\r\n") { 
 53
 54break; 
 55
 56} 
 57
 58if (response == ".\n") { 
 59
 60break; 
 61
 62} 
 63
 64if (article.Length &lt; 1024) { 
 65
 66article += response; 
 67
 68}; 
 69
 70} 
 71
 72retval.Add(article); 
 73
 74} 
 75
 76return retval; 
 77
 78} 
 79
 80  
 81private string Response() { 
 82
 83//System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();   
 84System.Text.Encoding enc=Encoding.Default;   
 85byte []serverbuff = new Byte[1024]; 
 86
 87NetworkStream stream = GetStream(); 
 88
 89int count = 0; 
 90
 91while (true) { 
 92
 93byte []buff = new Byte[2]; 
 94
 95int bytes = stream.Read( buff, 0, 1 ); 
 96
 97if (bytes == 1) { 
 98
 99serverbuff[count] = buff[0]; 
100
101count++; 
102
103if (buff[0] == '\n') { 
104
105break; 
106
107} 
108
109} 
110
111else { 
112
113break; 
114
115}; 
116
117}; 
118
119string retval = enc.GetString( serverbuff, 0, count ); 
120
121System.Diagnostics.Debug.WriteLine("READ:" + retval); 
122
123return retval; 
124
125} 
126
127  
128private void Write(string message) { 
129
130System.Text.ASCIIEncoding en = new System.Text.ASCIIEncoding() ; 
131
132byte[] WriteBuffer = new byte[1024] ; 
133
134WriteBuffer = en.GetBytes(message) ; 
135
136NetworkStream stream = GetStream() ; 
137
138stream.Write(WriteBuffer,0,WriteBuffer.Length); 
139
140System.Diagnostics.Debug.WriteLine("WRITE:" + message);   
141  
142  
143} 
144
145  
146} 
147
148}   
149}</grouplist.count;i++){>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus