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)
{
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.