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)
{
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();
writer.WriteLine("List");
writer.Flush();
s = reader.ReadLine();
while (s != ".")
{
s = reader.ReadLine();
if (s != ".")
{
s = s.Substring(0,s.IndexOf(' '));
textList.Text = textList.Text+s+"\n";
}
}
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");
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.
| You must Sign In to use this message board. |
|
|
 |
|
|
 |
|
 |
http://www.ietf.org/rfc/rfc0977.txt
I hope this answers your question. I just began writing an NNTP client and found RFC very useful.
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
I am also working on an NNTP implementation and found your methods insightful. Just one thing that I noticed that might not be necessary... to my knowledge, calling the Flush method is not required and has no effect on Network Streams.
From MSDN documentation:
"...This method is reserved for future use.
"The Flush method implements the Stream.Flush method; however, because NetworkStream is not buffered, it has no affect on network streams. Calling the Flush method will not throw an exception."
Calling the Flush method will not cause an error in compilation or during execution, but it is not necessary. Correct me if I'm wrong.
Keep up the good work.
Jacob
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Jayob, you are right about the NetworkStream class.
But he is using a StreamWriter on top of the NetworkStream. The StreamWriter class is buffered and needs to be flushed to send the data to the underlying NetworkStream. I just ran into this problem and you asked for correction...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Jacob, you are right about the NetworkStream class.
But he is using a StreamWriter on top of the NetworkStream. The StreamWriter class is buffered and needs to be flushed to send the data to the underlying NetworkStream. I just ran into this problem and you asked for correction...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
You are correct! How could I have overlooked that?
Either he changed the article or code from what was originally published or... I was wrong from the beginning.

|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Dear Chris, I'm the author of this article and I cannot find my name(Ly Hoang Hai) and my portrait  Would you pls to check and correct this ?
My name is Ly Hoang Hai. I’m proud to be a classmate with Mr Pham Thanh Phong, Mr Huynh Quang Thuan.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Ly Hoang Hai wrote: I'm the author of this article and I cannot find my name(Ly Hoang Hai) and my portrait Would you pls to check and correct this ?
Hello Ly Hoang Hai
I’ve fixed this, and I also apologize for the error since it was I who edited this particular article. The problem was that you had two CP ids (both had the same email) and the second one weirdly did not have an associated name. I chose the newer ID automatically and that’s how this blank-name problem arose. It has been fixed now.
Regards, Nish
"I'm a bit bored at the moment so I'm thinking about writing a new programming language" - Colin Davies
My book :- Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Member No. 457654 (Sorry but I could not find your name)
I looked at your nice little socket tutorial. I find it nice and inspiering . This realy shows how easy it is to do great stuff in no time, using C#.
When I made the first run I thought my internet connection broke down, since it took me very long time (to long atleast) to retrieve the list. Since I have 2Mbit down stream something had to be wrong, normally this only takes a few seconds.
So I took a look at your code and noticed the following:
while (s != ".") { s = reader.ReadLine(); if (s != ".") { s = s.Substring(0,s.IndexOf(' ')); textList.Text = textList.Text+s+"\n"; } }
This is the preformance killer: textList.Text = textList.Text+s+"\n";
Since this will make your computer work like crazy creating new string objects. Remember the string is not overloaded to cascade extra strings using the + like in C++. Therefore this results in your computer creating a totally new string object and discarding the present one, everytime this line is run. In this case on time per group on microsoft newsserver.... hench MANY . and also leaving quite a bid og work for the Garbage Collecter.
Try changing the code to the following:
StringBuilder sb = new StringBuilder(); while (s != ".") { s = reader.ReadLine(); if (s != ".") { s = s.Substring(0,s.IndexOf(' ')); sb.Append(s+"\n"); } }
textList.Text = sb.ToString(); This is just one simple of many ways to fix it.
Just to make the sientific proof of my claims, I tried to add label to the form showing a measured execution time of each method. The inital method was 32.29 seconds to execute. The new easy improved method only used 2.14 seconds.
This Extreemly simpel change, made a factor 16 improvement on the code.  Worth noticing is that this factor would keep grow exponentially, because the new string is always is created partially(almost totally) from the old sting data which is growing and growing and therefore more and more needs to be copied every iteration of the while
I learned this the hard way myself when creating a cryto program where I needed to move around, large amounts of strings and chars alot.
Hope this comment was (or will be) worth your 5 minutes reading  Thanks
Anders Dyhrberg
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
Oh man! Thanks for the scientific proof! I had this question during my last job interview and coming from c++ world I used += for string. And, I kind of knew he was expecting StringBuilder...That was one of the reason the guy show me the door.
I think another way to "optimize" the code is use ListBox instead-AddItem for each line.
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
|