C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-29-2005, 08:29 PM   #1
Registered User
 
Join Date: Aug 2004
Posts: 731
streamReader problems

Been a long time sence I last posted here, got alot better at C# than I used to be.

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);
                }
            }
if you need more code just ask.
Rune Hunter is offline   Reply With Quote
Old 05-29-2005, 08:53 PM   #2
Banned
 
nickname_changed's Avatar
 
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   Reply With Quote
Old 05-29-2005, 09:17 PM   #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   Reply With Quote
Old 05-29-2005, 09:37 PM   #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();
            }
But now I know it just stops when it gets to the clientreader.ReadLine(); line. Found that out using the debugger now that I know how to use it
Rune Hunter is offline   Reply With Quote
Old 05-29-2005, 11:44 PM   #5
Banned
 
nickname_changed's Avatar
 
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   Reply With Quote
Old 05-30-2005, 08:00 AM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:56 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22