TcpClient.GetStream().Read() reads wrong bytes when request made for second time.

Veeran

New member
Joined
Mar 11, 2019
Messages
1
Programming Experience
1-3
In my windows application, where communication between client and server made through TCP/IP using NetworkStream. when client sends message(byte array of command) the response is received correctly. But for the second time when request made, the order of bytes in the response has changed. Wrong data has received from server.Here is the code.

Thread to handle data reception in page load:
C#:
ThreadDatareceive = new Thread(new ParameterizedThreadStart(udfHandleClientCommunication)); ThreadDatareceive.Start(Class.myclient);
ThreadDatareceive.Start(Class.myclient);

Write to stream:
C#:
bal.byteCommand = new byte[] { 0x90, 0x20, 0x02, 0x01, 0x00, 0x00, 0x01, 0xB0 };
if (Class.myclient.Connected)
{
    stream = Class.myclient.GetStream();
    stream.Write(bal.byteCommand, 0, bal.byteCommand.Length);
    bal.EventNum = 1;
    bal.timer.Start();
    bal.byteResponse.Clear();
}
//Scan();
else
    MessageBox.Show("Please Configure the Module");


Read from stream:

C#:
private void udfHandleClientCommunication(object client)
{
    if (this.InvokeRequired)
    {
        MyDelegate1 mydelegate2 = new MyDelegate1(udfHandleClientCommunication);
        this.Invoke(mydelegate2, new object[]{client});
    }
    else
    {
        try
        {
            //List byteData = new List();
            while (true)
            {
                tcpClient = (TcpClient)client;
                stream2 = tcpClient.GetStream();
                byte[] byteBuffer = new byte[1];
                //Array.Clear(byteBuffer, 0, byteBuffer.Length);
                stream2.Read(byteBuffer, 0, byteBuffer.Length);
                bal.byteResponse.AddRange(byteBuffer);

                try
                {
                    switch (bal.EventNum)
                    {
                        case 1:
                            // The scan response is of 97 bytes. So,
                            // wait for the full response
                            // before processing the response data.
                            if (bal.byteResponse.Count >= 97)
                            {
                                if(bal.byteResponse[2] == 0x42)
                                {
                                    udfReadStatusResponse();
                                    break;                                        
                                }  
                            }

                        case 2:
                            // The scan setup is of 11 bytes. So,
                            // wait for the full response
                            // before processing the response data.
                            if (bal.byteResponse.Count >= 11)
                            {
                                if(bal.byteResponse[2] == 0x43)
                                {
                                    udfReadSetupResponse();
                                    break;                                        
                                }  
                            }

                    }
                }
                catch
                {
                    bal.DataReceivedFlag = false;
                    bal.TimeouFlag = true;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

When I run application (Status win form) receives data correctly.But when I click status again, the data read from stream.read() is incorrect.

I am closing the client connection in the form close(). May be reading one byte from stream every time is the problem or closing connection properly.
 
Last edited by a moderator:
Back
Top Bottom