NetworkStream.Read没有得到空字节/秒后的所有字节

本文关键字:字节 Read NetworkStream | 更新日期: 2023-09-27 18:04:56

我正在制作一个能够远程控制相机(Scopia XT5000 - Radvision)等设备的应用程序。我需要向设备发送一个初始化命令,以便我能够发送控制设备的AT命令。

一旦我发送初始化命令,服务器应该回复这个:
在字符串

? ?' ' 0 ' 0在[& lt; IP400C9XT5000-03.01.00.0028 ' r ? ?' ' 0 ' 0好' r

在字节

170 170 0 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 48 48 48 50 56 13 170 170 0 0 0 3 79 75 13

我试着调试并得到了这些值当我在这里放一个break

serverStream。Read(stream, 0, stream . length);

但是每次我在没有调试或设置中断的情况下运行程序时,它会给我不同的字符串和字节。就像这样
在字符串

? ?' ' 0 ' ' ' 0 ' 0 ' ' ' 0 ' 0 ' ' ' 0 ' 0 ' ' ' 0 ' 0 ' ' ' 0 ' 0 ' ' ' 0 ' 0 ' ' ' 0 ' 0

在字节

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

有什么问题吗?
它是:
-空字节?
-回车?"' r"?
-多条线?

整个代码:

{
    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;
    }

}

NetworkStream.Read没有得到空字节/秒后的所有字节

Read返回读取的字节数。您有一个10025字节的缓冲区,但是在Read之后它还没有满。你可能想要循环,直到你想要的所有字节都被读取。

它在调试时工作,因为花费了额外的时间,网络流有时间完全接收结果,所以当Read被调用时,所有的数据都是可用的并被放入缓冲区。

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