Reputation: 45
I'm trying to implement my own NNTP client (a console program at the moment). My Connection class inherits from TcpClient, and has the following code:
Connect(hostname, port);
NetworkStream stream = GetStream();
StreamReader reader = new StreamReader(stream);
try
{
while (!reader.EndOfStream)
{
r = reader.ReadLine();
Console.WriteLine(r);
}
}
catch (IOException ioe)
{
Console.WriteLine(ioe.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
When I run this piece of code or debug it, it outputs the welcome message from the NNTP server, but appears to hang as soon as the ReadLine is executed again. There should be no more data from the server, but EndOfStream is false.
I decided to ditch the StreamReader, and ended up with the following:
Connect(hostname, port);
NetworkStream stream = GetStream();
try
{
int ch;
while ((ch = stream.ReadByte()) != -1)
{
Console.WriteLine((char) ch);
}
}
catch (IOException ioe)
{
Console.WriteLine(ioe.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
As with the first example, ReadByte never returns -1, and the code appears to hang.
No exceptions are thrown, the program just doesn't exit. Does anybody know why this is happening, or what I am doing wrong?
Thanks!
Upvotes: 0
Views: 73