4 Replies - 760 Views - Last Post: 10 February 2012 - 09:06 AM Rate Topic: -----

#1 hellborg  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 19-March 11

netstream writing wrong message

Posted 10 February 2012 - 06:37 AM

so i have 2 projects, one client and one server. now these can connect to each other, and i can send messages, but sometimes the message sent is wrong, for instance, if i write from server 1 2 3 4 5. it works good, but if i then write TO the server 6 7 8 9 it sends 5 instead of 6.

like this http://img36.imagesh...58/47615075.png


here is the code, for the client read and the server read functions.

CLIENT
while (true)
                {
                    if (NetStream.CanRead)
                    {

                        Invoke(new MethodInvoker(delegate { lblStatus.Text = "Connected"; btnReconnect.Visible = false; }));
                    }
                    data = new byte[1024];
                    recv = NetStream.Read(data, 0, data.Length);
                    NetStream.Flush();
                    Invoke(new MethodInvoker(delegate { lstMessages.Items.Add(Encoding.UTF8.GetString(data, 0, recv)); }));
                    
                }




SERVER
    while (true)
                {
                    data = new byte[1024];
                    recv = NetStream.Read(data, 0, data.Length);

                    if (recv == 0)
                        break;
                    NetStream.Flush();
                    Invoke(new MethodInvoker(delegate { lstMessages.Items.Add(Encoding.UTF8.GetString(data, 0, recv)); }));
                }



and here is the write for both
CLIENT
NetStream.Flush();
            data = Encoding.UTF8.GetBytes(textBox1.Text);
            NetStream.Write(data, 0, data.Length);

            NetStream.Flush();
            lstMessages.Items.Add(Encoding.UTF8.GetString(data));
            textBox1.Clear();

SERVER
NetStream.Flush();
            data = Encoding.UTF8.GetBytes(textBox1.Text);
            NetStream.Write(data, 0, data.Length);

            NetStream.Flush();
            lstMessages.Items.Add(Encoding.UTF8.GetString(data));
            textBox1.Clear();


Is This A Good Question/Topic? 0
  • +

Replies To: netstream writing wrong message

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5466
  • View blog
  • Posts: 11,742
  • Joined: 02-June 10

Re: netstream writing wrong message

Posted 10 February 2012 - 06:51 AM

Are you saying you have completely seperate but nearly identical code for the two projects? You don't have a shared class for things like doing the communication?

I recommend the tutorials listed in FAQ 6 to clean up this architecture before proceeding.



TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2



FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 hellborg  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 19-March 11

Re: netstream writing wrong message

Posted 10 February 2012 - 07:06 AM

i have not thought of using a shared class simply because its two separate pieces of software, and its for a learning purpose and the code for the server will be changed once i start to learn a bit more about how tcplistener and client actually works. i realize this might be the wrong way to go, and i will look into the link you provided, after all, i do want to learn.
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: netstream writing wrong message

Posted 10 February 2012 - 08:39 AM

You do know that CanRead means that it is a readable stream, not that there is data to read? You should be checking if there is data to read before you attempt to read it (DataAvailable property)
Was This Post Helpful? 0
  • +
  • -

#5 hellborg  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 19-March 11

Re: netstream writing wrong message

Posted 10 February 2012 - 09:06 AM

hmmm ok, i did know canread means its readable, but never occured to me to use dataavailable.

thanks, ill check if it fixes my problem :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1