Keensleeeeeeee
Keensleeeeeeee

Reputation: 1246

NetworkStream.Read did not get all the bytes after a null byte/s

I'm making an application that will able to control remotely a device such as a Camera(Scopia XT5000 - Radvision). I need to send an initialization command to the device for me to be able to send AT commands that will control the device.

Once I send the Initialization Commands, the server should reply this:
In String

??\0\0\0 AT[<IP400C9XT5000-03.01.00.0028\r??\0\0\0OK\r

In Bytes

170 170 0 0 0 32 65 84 91 60 73 80 52 48 48 67 57 88 84 53 48 48 48 45 48 51 46 48 49 46 48 48 46 48 48 50 56 13 170 170 0 0 0 3 79 75 13

I tried to debug and got those values when I put a break in here

serverStream.Read(inStream, 0, inStream.Length);

But every time I run the program without debugging or putting a break, it will give me different string and bytes. Just like this
In String

??\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0

In Bytes

170 170 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

What could be the problem?
Is it:
- the Null bytes?
- the carriage return? "\r"?
- the multiple lines?

Whole Code:

{
    TcpClient clientSocket = new System.Net.Sockets.TcpClient();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        msg("Client Started");                        
    }

    public void openSocket()
    { 
        if (clientSocket.Connected)
        {
            clientSocket.Close();
        }
        clientSocket = new System.Net.Sockets.TcpClient();
        clientSocket.Connect("10.0.3.202", 55003);
        label1.Text = "Client Socket Program - Server Connected ...";

    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        openSocket();
        //header bytes and initAT bytes are the initialization command for me to send AT commands
        msg("Init: ");
        NetworkStream serverStream = clientSocket.GetStream();
        //header bytes to enable AT command
        byte[] header = {170,170,0,0,0,8 };
        serverStream.Write(header, 0, header.Length);
        serverStream.Flush();
        //Initialize the Interface
        byte[] initAT = System.Text.Encoding.ASCII.GetBytes("AT[&IPV\r"); 
        serverStream.Write(initAT,0, initAT.Length);
        serverStream.Flush();

        byte[] inStream = new byte[10025];
        serverStream.Read(inStream, 0, inStream.Length);
        string returndata = System.Text.Encoding.ASCII.GetString(inStream);
        msg("Data from Server : " + returndata);
    }

    private void btnRight_Click(object sender, EventArgs e)
    {
        msg("Right : ");
        NetworkStream serverStream = clientSocket.GetStream();
        //AT Command to move the main camera to right
        byte[] outStream3 = System.Text.Encoding.ASCII.GetBytes("AT[&SY011R\r");
        serverStream.Write(outStream3, 0, outStream3.Length);

        byte[] inStream2 = new byte[48];
        serverStream.Read(inStream2, 0,inStream2.Length);
        string returndata = System.Text.Encoding.ASCII.GetString(inStream2);            
        msg(returndata);
    }           

    public void msg(string mesg)
    {
        textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
    }


}

Upvotes: 2

Views: 1976

Answers (1)

Kieren Johnstone
Kieren Johnstone

Reputation: 42003

Read returns the number of bytes read. You have a 10025-byte buffer, but it is not full after Read. You may want to loop until all of the bytes you want are read.

It works when you debug because with the extra time spent, the network stream has time to fully receive the result, so when Read is called, all of the data is available and is put into the buffer.

Reference: http://msdn.microsoft.com/en-gb/library/system.io.stream.read.aspx

Upvotes: 1

Related Questions