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