C# equivalent of Java's DataOutputStream?

2019-04-13 19:24发布

问题:

I've done quite a bit of research on the topic and I see that this question is asked quite often, but none of them have the answer that I'm looking for, the most common solution was to use the BinaryWriter class that comes with C#, however it doesn't seem to be doing it's job.

For my Server Application I'm using Netty a Java Based NIO Networking Library which makes use of DataInput and DataOutput Streams. I have a client that works in java, but it was just for testing purposes and I'm now in the progress of moving it over to C# to put it into my game.

Here's the C# Code in its basic format, just trying to get things working for the time being..

Client = new TcpClient ();
try 
{
    Client.Connect (IPAddress.Parse ("127.0.0.1"), PORT);
    Stream = Client.GetStream();
    ClientOutput = new BinaryWriter(Stream);
    ClientInput = new BinaryReader(Stream);

    ClientOutput.Write ((string) "UserIsBob");
    ClientOutput.Write ((string) "MyPassLol");
    ClientOutput.Flush();

} 

Now, this looks fine; but here's the problem, when using

DataInputStream.readUTF()

On the Java server, nothing happens; However if I use

DataOutputStream.writeUTF(String)

From the java client, the server will read it perfectly fine and print it to the console.

I'm really not sure what I'm doing wrong, but there doesn't seem to be much information on it. The functionality that I require is the following:

  • WriteByte(byte)
  • WriteBytes(byte[])
  • WriteUTF(String)
  • WriteInt(int)
  • WriteLong(long)
  • WriteFloat(float)
  • WriteDouble(double)
  • WriteBoolean(boolean)

From my understanding you can do all of this in the BinaryWriter class by just typecasting, but it doesn't seem to be picked up by the Java DataInputStream

After reading the documentary posted by nodakai, I came up with this code:

void WriteUTF(string utf) 
{
    short buffer = (short)System.Text.ASCIIEncoding.ASCII.GetByteCount(utf);
    ClientOutput.Write ((short)buffer);
    ClientOutput.Write ((string)utf);
}

However, it still doesn't work.

I did a little more looking into it and found that if I used InputStream.ReadShort() that I could get the value sent, so the String with the value of "Hello World" would be send like this

WriteShort(11)
WriteString("Hello World")
Flush()

However, when doing the equivalent in C#, it still doesn't come through, even though it's read on the java side the same way.

回答1:

Have you read Javadoc carefully?

  • DataOutputStream.writeUTF()

DataInputStream.readUTF() / DataOutputStream.writeUTF() use their own encoding scheme. I don't think .NET BinaryReader/Writer can be compatible with them wrt Unicode strings. However you can manually serialize Unicode strings as a length data followed by a byte sequence.



回答2:

I have written a C# Conversion of Java's DataInputStream and DataOutputStream you can collect them here.

https://bitbucket.org/CTucker1327/c-datastreams/src

To construct these classes you would pass a BinaryWriter or BinaryReader into the constructor.

To Construct DataOutputStream

DataOutputStream out = new DataOutputStream(new BinaryWriter(Stream));

To Construct DataInputStream

DataInptuStream in = new DataInputStream(new BinaryReader(Stream));