![]() |
| | #1 |
| Registered User Join Date: Aug 2004
Posts: 731
| streamReader problems I am creating a simple server and client program. The client connects to the server (both running on my computer) and that works fine, they can connect without errors. And as far as I can tell I can send data using a networkstream and laying a streamreader and/or streamwriter over it. But when I go to read a line of data the program freezes or acts as if it is constantly trying to do somthing (like writing a 1gb file). I waited for a couple mins to see if I could grab an exception but none came, it just sat there frozen. writing code: Code: if (host == false)
{
clientwriter.Write(textBox_TextWrite.Text);
clientwriter.Flush();
}
else
{
serverwriter.Write(textBox_TextWrite.Text);
clientwriter.Flush();
}
reading code: Code: if (host == false)
{
textBox_TextRead.Text = clientreader.ReadToEnd();
}
else
{
try
{
textBox_TextRead.Text = serverreader.ReadLine();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
|
| Rune Hunter is offline | |
| | #2 |
| Banned Join Date: Feb 2003 Location: Australia
Posts: 986
| Step through it with a breakpoint in your debugger (see below). I have a feeling your code is blocking on the clientreader.ReadToEnd(); line - are you sure data is being sent? Using the debugger Assuming you're using Visual Studio, press F9 to set a breakpoint on a line of code, then run the program with F5. When it hits your line of code, the program will freeze and return to Visual Studio with that line highlighted. Press F11 to step through the code line by line to see what its doing. |
| nickname_changed is offline | |
| | #3 |
| Registered User Join Date: Aug 2004
Posts: 731
| Ok I see how to use the debugger but it doesn't say anything is wrong. Last edited by Rune Hunter; 05-29-2005 at 09:22 PM. |
| Rune Hunter is offline | |
| | #4 |
| Registered User Join Date: Aug 2004
Posts: 731
| I changed the code a bit writing: Code: if (host == false)
{
clientwriter.Write(textBox_TextWrite.Text);
clientwriter.Flush();
}
if (host == true)
{
serverwriter.Write(textBox_TextWrite.Text);
serverwriter.Flush();
}
reading: Code: if (host == false)
{
if (clientreader.EndOfStream == false)
{
textBox_TextRead.Text = clientreader.ReadLine();
}
}
if (host == true)
{
textBox_TextRead.Text = serverreader.ReadLine();
}
|
| Rune Hunter is offline | |
| | #5 |
| Banned Join Date: Feb 2003 Location: Australia
Posts: 986
| Yep, the debugger will save you a lot of time If it's pausing on ReadLine(), that probably means it's waiting until it reaches the end of the line. Try changing it to Read(), or ensure a '\n' or '\r\n' (preferably use Environment.NewLine) is sent at the end of the text. Last edited by nickname_changed; 05-29-2005 at 11:51 PM. |
| nickname_changed is offline | |
| | #6 |
| Registered User Join Date: Aug 2004
Posts: 731
| ahh yah I looked at my book more closely and found I should use writeline instead of write. That fixed it, thanks. This is the first time tcp/ip worked for me. |
| Rune Hunter is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| No clue how to make a code to solve problems! | ctnzn | C Programming | 8 | 10-16-2008 02:59 AM |
| C Pointers Problems | mhelal | C Programming | 8 | 01-10-2007 06:35 AM |
| Rendering problems (DirectX?) | OnionKnight | Tech Board | 0 | 08-17-2006 12:17 PM |
| contest problems on my site | DavidP | Contests Board | 4 | 01-10-2004 09:19 PM |
| DJGPP problems | stormswift | C Programming | 2 | 02-26-2002 04:35 PM |