programad
programad

Reputation: 1311

Socket Client answer

I'm creating a client/server socket app. And I'm not being able to solve this problem, probably due to lack of knowledge on the matter.

The client must send an answer in order to proceed communication:

while(comunicate)
{
    if (chkCorrectAnswer.Checked)
                answer = encoder.GetBytes('\x02' + "82SP|" + '\x03');
            else
                answer = encoder.GetBytes("bla");
            ServerStream.Write(answer, 0, answer.Length);

            //or ??
            //tcp.Client.Send(answer);
}

And the server recieves it:

while(comunicate)
{
    var validanswer= encoder.GetBytes("myvalidanswer");

    answer = new byte[validanswer.Length];
    stream.Read(answer, 0, validanswer.Length);

    //or ??
    //tcp.Client.Receive(answer);

    if (answer.SequenceEqual(validanswer))
        // continue communication
}

Each code is in different app, looped "comunication thread".

The answer seems being sent correctly but the server doesn't seem to be recieveing it porperly. Sometimes it recieves blablab or lablabl and variations with 7 chars. I thoung the recieving would fill the buffer only with the incoming data and somehow it is filling the buffer with repeated data.

Two questions here:

  1. What should I use, stream.write/read or client.send/recieve?
  2. How to ensure this answer verification?

Upvotes: 0

Views: 420

Answers (3)

jgauffin
jgauffin

Reputation: 101140

0x02 and 0x03 is called start of text (STX) and end of text (ETX) and are separators used to identify where your messages starts and ends. There is really no need to use both, it was a common practice when doing serial communication.

You need to continue building a message until ETX is received.

something like (easiest solution but not very efficient if you have lots of clients)

string buffer = "";
var readBuffer = new byte[1];
int readBytes = 0;
while ((readBytes = stream.Read(readBuffer, 0, 1))  == 1 && readBuffer[0] != 3)
{
   buffer += readBuffer[0];
}

you can of course read larger chunks. But then you need to check if more than one message was arrived and process the buffer accordingly. That's how I would do it though.

Upvotes: 1

rare
rare

Reputation: 134

When you do the read you'll get everything you wrote including amything you've written previously.

Implement a length header or some kind of seperator so you know what's what --

length + message

or

message + seperator

Then parse it when you do the read.

Upvotes: 1

I thoung the recieving would fill the buffer only with the incoming data and somehow it is filling the buffer with repeated data.

Well, you are repeatedly sending the data in a loop, so this is to be expected.

If you want to read only a certain number of bytes off the stream you need to also send the size of the logical packet ahead of it so that the receiving end can first read the size (say as a fixed int value) and then the actual response.

Upvotes: 1

Related Questions