tags:

views:

67

answers:

3

hey I'm writing on an Server-Client program but when my client sends something, it never reaches my server!

I'm sending like this:

    public void Send(string s)  
    {  
        char[] chars = s.ToCharArray();  
        byte[] bytes = chars.CharToByte();  
        nstream.Write(bytes, 0, bytes.Length);  
        nstream.Flush();  
    }

and Receiving in a background thread like this

    void CheckIncoming(object dd)
    {
        RecievedDelegate d = (RecievedDelegate)dd;
        try
        {

            while (true)
            {
                List<byte> bytelist = new List<byte>();
                System.Threading.Thread.Sleep(1000);
                int ssss;
                ssss = nstream.ReadByte();
                if (ssss > 1)
                {
                    System.Diagnostics.Debugger.Break();
                }
                if (bytelist.Count != 0)
                {
                    d.Invoke(bytelist.ToArray());
                }
            }
        }
        catch (Exception exp)
        {
            MSGBOX("ERROR:\n" + exp.Message);
        }
    }

the ssss int is never > 1 whats happening here???

A: 

You should change the check of the return value to if (ssss >= 0).

ReadByte returns a value greater or equal 0 if it succeeds to read a byte (source).

To elaborate on Marc's comment: How is nstream created? Maybe there is an underlying class that does not flush.

mafutrct
Or even `>= 0` (as per my comment)
Marc Gravell
Woopsies, of course. Not used to off-by-2 :)
mafutrct
A: 

well, Im creating a TcpClient, and use GetStream(); to get the NetworkStream

alex
I see you're new to this site. Stackoverflow does not have a discussion structure like most forums. Instead of posting an answer, try to edit the question, or add a comment directly on another user's answer. It's a bit confusing at first, I made the same mistake myself ;)
mafutrct
About your actual answer: I see, that should be ok. No clue then. :(
mafutrct
+2  A: 

NetworkStream.Flush() actually has no effect:

The Flush method implements the Stream..::.Flush method; however, because NetworkStream is not buffered, it has no affect [sic] on network streams. Calling the Flush method does not throw an exception

How much data is being sent?
If you don't send enough data it may remain buffered at the network level, until you close the stream or write more data.

See the TcpClient.NoDelay property for a way to disable this, if you are only going to be sending small chunks of data and require low latency.

Mark