【问题标题】:tcpclient receiving data incorrectlytcpclient 接收数据不正确
【发布时间】:2012-05-14 19:48:29
【问题描述】:

不确定这是 TCPClient 还是其他东西,我使用网络嗅探器检查从服务器发送和接收的内容以及数据是否正确,但我的软件接收数据不正确。

让我解释一下,我发送一个查询字节 (05) 我收到一个确认 (06) 然后我发送一些以 9B 字节开头的数据,一旦发送,我应该收到一个 06 字节,然后那个字节我应该得到一个 C5 字节,但是根据我的软件,我得到另一个 06 字节,而根据嗅探器,情况并非如此!

byte[] buff;
if (!this.isConnected())
    this.connect(); 

NetworkStream gs = _Socket.GetStream();

gs.Write(enq, 0, enq.Length);
gs.Flush();
outputByte(enq, "Trans"); //outputs ---> 05

buff = new byte[1];
gs.Read(buff, 0, buff.Length);
gs.Flush();
outputByte(buff, "Rec");// outputs <--- 06

if (buff[0] == 0x06)
{
    byte[] data = new byte[] {
                                            0x9B, 0x00,         0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x09,         
        0x67, 0x11, 0x01, 0x49, 0x4D, 0x41, 0x47, 0x45,         0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,         
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,         0x01, 0x53, 0x75, 0x6D, 0x6D, 0x61, 0x72, 0x79,         
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,         
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,         0x00, 0x00, 0x02, 0x08, 0x00, 0x00, 0x08, 0x00,         
        0x00, 0x00, 0x1F, 0x09, 0x01, 0x00, 0x04, 0x0A,         0x10, 0x00, 0x12, 0x01, 0x1F, 0x00, 0x00, 0x00,         
        0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,         0x12, 0x10, 0x1E, 0x0E, 0x1E, 0x54, 0x65, 0x73,         
        0x74, 0x69, 0x6E, 0x67, 0x10, 0x00, 0x12, 0x01,         0x1F, 0x00, 0x00, 0x00, 0x05, 0x00, 0x01, 0x00,         
        0x00, 0x00, 0x00, 0x00, 0x12, 0x10, 0x0D, 0x00,         0x00, 0x90
    };
    outputByte(data, "Trans"); /outputs --> with above byte information
    gs.Write(data, 0, data.Length);
    gs.Flush();

    buff = new byte[1];
    gs.Read(buff, 0, buff.Length);
    gs.Flush();
    // this is the first receive of 06
    outputByte(buff, "Rec");//outputs <--- 06

    if (buff[0] == 0x06)
    {
        gs.Flush();
        Console.WriteLine("fdsfsdfs");
        byte[] resp = new byte[5];
        gs.Read(resp, 0, resp.Length);
        gs.Flush();
        //this outputs <--- 06 but it should be showing <--- c5000100c4
        outputByte(buff, "Rec"); 
        gs.Write(ack, 0, ack.Length);
        outputByte(ack, "Trans");
        gs.Flush();
    }
}

根据嗅探器,这是应该发生的事情

---> 05
<--- 06
---> 9b008000000080000009671101494d414745310000000000000000000000000000000153756d6d617279000000000000000000000000000000000000000000000000000002080000080000001f090100040a100012011f000000050001000000000012101e0e1e54657374696e67100012011f000000050001000000000012100d000090
<--- 06
<--- c5000100c4

根据软件,这是正在发生的事情

---> 05
<--- 06
---> 9b008000000080000009671101494d414745310000000000000000000000000000000153756d6d617279000000000000000000000000000000000000000000000000000002080000080000001f090100040a100012011f000000050001000000000012101e0e1e54657374696e67100012011f000000050001000000000012100d000090
<--- 06
<--- 06

有什么想法吗?另外,如果您对如何改进代码有任何建议,我将不胜感激

【问题讨论】:

  • 我要说的第一件事是:你能输出每个Read调用的结果吗?
  • @MarcGravell 这就是 outputByte 函数的作用,这就是上面显示的内容!

标签: c# tcpclient


【解决方案1】:

这里:你输出了错误的缓冲区:

    gs.Read(resp, 0, resp.Length);
    gs.Flush();

    outputByte(buff, "Rec");  <==== should be "resp"

但是!!!

您的所有读取调用都已中断;他们必须处理返回值,尤其是在读取多个字节时。数据包碎片会杀死你的代码。

正确的“准确读取 [n] 个字节”方法是:

public static void ReadExact(this Stream stream, byte[] buffer,
           int offset, int count) {
    int read;
    while(count > 0 && (read = stream.Read(buffer, offset, count)) > 0) {
        count -= read;
        offset += read;
    }
    if(count != 0) throw new EndOfStreamException();
}

然后:

gs.ReadExact(resp, 0, resp.Length);

将正确填充它,如果流中没有足够的数据则错误。

【讨论】:

  • 天哪,这就是为什么我讨厌同时处理多个事情,2 个 c# 项目一个网站和 2 台笔记本电脑进行维修,Marc 非常感谢你,我根本看不到那个错误,改变了它并现在它是正确的,再次感谢 ReadExact 函数我会尽快实现它,我还需要一些东西来写吗?
猜你喜欢
  • 1970-01-01
  • 2021-10-12
  • 2021-01-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2021-07-20
  • 1970-01-01
  • 2017-05-10
相关资源
最近更新 更多