简体   繁体   English

用C#BinaryWriter缓冲区发送段?

[英]C# BinaryWriter buffer for sending segments?

I'm not sure that a "Buffer" is what I'm looking for, so I'll show you my problem and then you guys can decide if that's the correct word and on a solution. 我不确定要寻找的是“缓冲区”,所以我将向您展示我的问题,然后你们可以确定这是否是正确的词并提供解决方案。 Currently I'm working on creating a networking port for the Java DataOutputStream class in C#. 目前,我正在为C#中的Java DataOutputStream类创建网络端口。 The last thing I have to do is fix this issue with sending segmented information. 我要做的最后一件事是通过发送分段信息来解决此问题。

Here's my WriteInt method in C# ( ClientOutput is an instance of BinaryWriter ) 这是我在C#中的WriteInt方法( ClientOutput是BinaryWriter的一个实例

public void WriteInt(int v)
{
    ClientOutput.Write (((uint)v >> 24) & 0xFF);
    ClientOutput.Write (((uint)v >> 16) & 0xFF);
    ClientOutput.Write (((uint)v >> 8) & 0xFF);
    ClientOutput.Write (((uint)v >> 0) & 0xFF);
    IncCount (4);
}

For anyone who wants to compare; 对于任何想比较的人; here's the original in Java 这是Java的原始版本

public final void writeInt(int v) throws IOException {
       out.write((v >>> 24) & 0xFF);
       out.write((v >>> 16) & 0xFF);
       out.write((v >>>  8) & 0xFF);
       out.write((v >>>  0) & 0xFF);
       incCount(4);
}

Normally in java you would have to call " Flush() " before the data would be sent to the server or client; 通常,在Java中,必须先调用“ Flush() ”,然后才能将数据发送到服务器或客户端。 However it appears that the BinaryWriter automatically flushes whenever you call " Write() " 但是,似乎每当您调用“ Write() ”时BinaryWriter都会自动刷新

Here's the " ReadInt() " code in Java. 这是Java中的“ ReadInt() ”代码。

public final int readInt() throws IOException {
        int ch1 = in.read();
        int ch2 = in.read();
        int ch3 = in.read();
        int ch4 = in.read();
        if ((ch1 | ch2 | ch3 | ch4) < 0)
            throw new EOFException();
        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}

When the Java code is executed to " WriteInt " and ReadInt is called on the server, the value is displayed properly; 当Java代码执行为“ WriteInt ”并在服务器上调用ReadInt时 ,该值将正确显示; However currently the server is processing the integer 4 different times and displaying an integer value based on the segments. 但是,当前服务器正在4次不同的时间处理整数,并根据分段显示整数值。

Example Input ( C# ): 输入示例( C# ):

Client.WriteInt(1000);

Example output( Java ): 示例输出( Java ):

0
0
50331648
-402653184

When the output should be: 当输出应为:

1000

Please bear with me as I just picked up C# a few days ago, and I may be asking a stupid question. 几天前我刚接触C#,请多多包涵,我可能会问一个愚蠢的问题。

BinaryWriter has a bunch of Write() method overloads and it looks like you are calling the Write(Int32) overload, which of course sends four 4-byte integers. BinaryWriter有很多Write()方法重载,看起来您正在调用Write(Int32)重载,它当然会发送四个4字节整数。

You should be able to fix the problem simply by casting your values to byte in the call to Write(): 您只需将值转换为Write()的字节即可解决此问题:

public void WriteInt(int v)
{
    ClientOutput.Write ((byte)(((uint)v >> 24) & 0xFF));
    ClientOutput.Write ((byte)(((uint)v >> 16) & 0xFF));
    ClientOutput.Write ((byte)(((uint)v >> 8) & 0xFF));
    ClientOutput.Write ((byte)(((uint)v >> 0) & 0xFF));
    IncCount (4);
}

Note that technically speaking, you should not need to cast the value v to uint before the shift. 请注意,从技术上讲,您无需在移位之前将值v强制转换为uint。 Even if sign-extension were to occur, you're masking off all but the last byte anyway. 即使发生符号扩展,您还是要屏蔽除最后一个字节以外的所有字节。 But that part shouldn't hurt anything. 但是那部分不应该伤害任何东西。

Check out Jon Skeet's endian-aware version of BinaryWriter. 查看Jon Skeet的BinianWriter的字节序感知版本。 There's discussion and some links here: BinaryWriter Endian issue 这里有讨论和一些链接: BinaryWriter Endian问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM