Click here to Skip to main content
6,929,467 members and growing! (12,764 online)
Email Password   helpLost your password?
General Programming » Internet / Network » NNTP     Intermediate

Getting the list of groups in a news server

By Ly Hoang Hai

Introduction to Socket programming
C#, Windows, .NET1.0, Dev
Posted:1 Jul 2003
Views:47,197
Bookmarked:26 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 3.39 Rating: 3.75 out of 5
1 vote, 12.5%
1
1 vote, 12.5%
2
1 vote, 12.5%
3
1 vote, 12.5%
4
4 votes, 50.0%
5

Introduction

This article will introduce you to socket programming in .NET via a practical application: getting all groups in a news server. To fully understand this article, you should have some knowledge about socket programming.

Using the code

To illustrate these things, we will make  a form which fetches all groups in a news server (e.g.. news.microsoft.com � the best and free news server I known) and put the results in a rich textbox.  Our form will connect to the server via TCP protocol and talking with it in NNTP protocol (you should reference RFC 977 at www.faqs.org to find more information about NNTP protocol. It�s rather simple). The following is the main source code

private void clickGo(object sender, System.EventArgs ex)
{
  // Connect to server;

  TcpClient tcpClient = new TcpClient();          
  tcpClient.Connect("news.microsoft.com", 119);   
  NetworkStream networkStream = tcpClient.GetStream();
  StreamReader reader = new StreamReader(networkStream);
  StreamWriter writer = new StreamWriter(networkStream);
                  
  string s;
  reader.ReadLine(); // Server respones = "200 Server ready"

  writer.WriteLine("List");
  writer.Flush();
  s = reader.ReadLine();  // Server response = "215 Newsgroups follow"

  while (s != ".") // "." response means that the list comes to the end

  {
    s = reader.ReadLine();
    if (s != ".") 
    {
      s = s.Substring(0,s.IndexOf(' '));
      textList.Text = textList.Text+s+"\n";
    }
  }                       
  // Disconnect

  networkStream.Close();
  tcpClient.Close();
}

First, we connect to the server and open a new stream with these lines :-

TcpClient tcpClient = new TcpClient(); 
tcpClient.Connect("news.microsoft.com", 119);
NetworkStream networkStream = tcpClient.GetStream();

networkStream is now purely a binary stream; so it is very difficult to use. We�ll turn it in to text-based stream with these lines of code:

StreamReader reader = new StreamReader(networkStream);
StreamWriter writer = new StreamWriter(networkStream);

The remaining work is talking with the server in NNTP protocol to get the list of groups in a way which is no different from reading and writing in a console window. We can imagine that the server is a real person who is only able to talk in NNTP language.

s1 = reader.ReadLine(); 
writer.WriteLine("List");
writer.Flush();
s2 = reader.ReadLine(); 

There is one thing that you should pay attention to : you must flush the buffer before reading from server. For example, when you order the server to send you the list of group

writer.WriteLine("List"); // without flush

The command List is stored in the buffer, instead of being sent immediately to the server and , certainly, the server doesn�t receive the command "List". And when you read the answer from the server :-

s2 = reader.ReadLine(); 

your application will hang because the server just waits and waits for your command. So, keep in your mind that you must call Flush before reading the answers from the server.

Final Notes

If you download the list via 56k Modem, it will take two or three minutes to complete. Be patient!  For keeping source-code simple , I assume that there is not any error when making the connection or enquiring the server. I�m not a native English speaker. So, I�m willing to receive your feed-back about the grammar and style of this article. Thanks.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Ly Hoang Hai


Member
My name is Ly Hoang Hai.
I am study at Ki Thuat TPHCM University in VietName (class MT00KSTN)
I’m proud to be a classmate with Mr Pham Thanh Phong, Mr Huynh Quang Thuan.
Occupation: Engineer
Location: Vietnam Vietnam

Other popular Internet / Network articles:

 
Article Top
 
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralGet the Messages Pinmemberkuerbis13:42 27 Sep '03  
How do I get the Postings in the Groops?

Thomas Bock
GeneralRe: Get the Messages PinsussAnonymous3:56 18 Oct '03  
GeneralNetworkStream.Flush Method PinmemberJacob Slusser8:20 30 Jul '03  
GeneralRe: NetworkStream.Flush Method PinsussMarkus Kraeutner12:32 21 Jan '05  
GeneralRe: NetworkStream.Flush Method PinsussMarkus Kraeutner12:32 21 Jan '05  
GeneralRe: NetworkStream.Flush Method PinmemberJacob Slusser6:20 24 Jan '05  
GeneralWhere is my name and my portraits ? PinmemberLy Hoang Hai21:32 9 Jul '03  
GeneralRe: Where is my name and my portraits ? PineditorNishant S22:42 9 Jul '03  
GeneralEasy but extreemly powerfull optimization PinsussAnders Dyhrberg12:21 8 Jul '03  
GeneralRe: Easy but extreemly powerfull optimization PinsussAnonymous4:03 5 Jul '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 1 Jul 2003
Editor: Nishant Sivakumar
Copyright 2003 by Ly Hoang Hai
Everything else Copyright © CodeProject, 1999-2010
Web11 | Advertise on the Code Project