Thread: streamReader problems

  1. #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.

  2. #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.

  3. #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.

  4. #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

  5. #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.

  6. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM