Click here to Skip to main content
Click here to Skip to main content

Getting the list of groups in a news server

, 1 Jul 2003
Rate this:
Please Sign up or sign in to vote.
Introduction to Socket programming

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

Share

About the Author

Ly Hoang Hai
Engineer
Vietnam Vietnam
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.

Comments and Discussions

 
GeneralGet the Messages Pinmemberkuerbis27-Sep-03 13:42 
GeneralRe: Get the Messages PinsussAnonymous18-Oct-03 3:56 
GeneralNetworkStream.Flush Method PinmemberJacob Slusser30-Jul-03 8:20 
GeneralRe: NetworkStream.Flush Method PinsussMarkus Kraeutner21-Jan-05 12:32 
GeneralRe: NetworkStream.Flush Method PinsussMarkus Kraeutner21-Jan-05 12:32 
GeneralRe: NetworkStream.Flush Method PinmemberJacob Slusser24-Jan-05 6:20 
QuestionWhere is my name and my portraits ? PinmemberLy Hoang Hai9-Jul-03 21:32 
AnswerRe: Where is my name and my portraits ? PineditorNishant S9-Jul-03 22:42 
GeneralEasy but extreemly powerfull optimization PinsussAnders Dyhrberg8-Jul-03 12:21 
GeneralRe: Easy but extreemly powerfull optimization PinsussAnonymous5-Jul-04 4:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 2 Jul 2003
Article Copyright 2003 by Ly Hoang Hai
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid