0

I'm working on a simple project for my Data Communications class. I predicted it would take me two hours max but it's my first time writing software that makes a connection to the web and my first time working with C# and I'm hitting a pretty major road bump.

I establish a connection to the server but I appear to get absolutely no feedback from the server. Here is the code I am executing to establish a connection:

TcpClient conn = new TcpClient();
conn.Connect(host, port);
StreamReader reader = new StreamReader(conn.GetStream());
StreamWriter writer = new StreamWriter(conn.GetStream());
Console.WriteLine(conn.Connected);   //This line does print true.
//Console.WriteLine(reader.ReadLine());     I thought I'd get a message when I initially
//connected to the server but this line would hang

That is where I establish a connection. According to the project's specifications, inputting a 0 should prompt the server to return a ticket number. so I have the following code:

writer.WriteLine("0");
return reader.ReadLine();

The prgram hangs when trying to execute reader.ReadLine(). I've tried other methods in reader but it seems to hang on all of them. I've looked at plenty of code samples with setups similar to mine. Can anyone see any issues with my code? I don't think its a problem with the server. My professor said something about using a unix command "telnet [host] [port]" and I used the same host and port for this code. I'm developing this on a Windows 7 machine. I apologize for my ignorance in both C# and network programming.

If I left out any dire information, please let me know. As this is my first time dealing with this I'd imagine I left out something urgent.

Thanks a ton you guys!

0

1 Answer 1

4

You need to make sure that the writer (and the stream that it wraps) flushes it's output:

writer.WriteLine("0");
writer.Flush();
return reader.ReadLine();

Streams often buffer information to optimize I/O, because many I/O devices are more efficient with larger streams of data than short bursts. Your "0" is likely just too little data to cause the stream to actually send anything. This applies to things like disk I/O (writing files) as well.

4
  • 1
    Alternatively, to avoid having to do this every time, you can set the writer.AutoFlush property to true.
    – KingCronus
    Mar 23, 2012 at 16:51
  • @AdamKing yes, however that can significantly reduce performance if you are writing a lot of short bursts of data. There is a reason that it is false by default. Mar 23, 2012 at 16:52
  • Chris, I know that, but for @Toms homework I can't see that being a major concern.
    – KingCronus
    Mar 23, 2012 at 16:53
  • I tried both Flush() and AutoFlush but neither of them solved the problem of my program hanging on the ReadLine().
    – Tom
    Mar 23, 2012 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.