C#相当于Java的DataOutputStream类的? [英] C# equivalent of Java's DataOutputStream?

查看:760
本文介绍了C#相当于Java的DataOutputStream类的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了相当多的关于该主题的研究,我看到这个问题是问很多时候,但他们都没有我要找的答案,最常见的解决方案是使用的BinaryWriter 随C#类然而它似乎,并没有做它的工作。

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.

有关我的服务器应用程序,我使用的网状基于Java的NIO网络库,这使得使用的DataInput和DataOutput中流。我有一个客户端在Java中的作品,但它只是用于测试目的,我现在把它移到C#来把它放到我的游戏进度来的。

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.

下面的C#代码在其基本格式,只是试图让事情暂且工作。

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()

在Java服务器,什么都不会发生;但是,如果我用

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

DataOutputStream.writeUTF(String)

从Java客户端,服务器将读取它完美的罚款,并将其打印到控制台上。

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(字节)

  • WriteBytes(字节[ ])

  • WriteUTF(字符串)

  • WriteInt(INT)

  • WriteLong(长)

  • WriteFloat(浮点)

  • WriteDouble(双)

  • WriteBoolean(布尔)

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

从我的理解,你可以通过只类型转换,完成所有这些工作在的BinaryWriter 类,但它似乎并没有被拿起由Java DataInputStream以

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

阅读张贴nodakai纪录片后,我想出了这个代码:

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.

我做了一些更多的寻找到它,并发现,如果我使用的 InputStream.ReadShort(),我可以得到价值发送,因此用的Hello World的值的字符串将被发送这样

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()

但是,这样做在C#中的等价物时,仍无法通过,即使它读取在Java端相同的方式。

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.

推荐答案

你读过的Javadoc仔细?

Have you read Javadoc carefully?

DataInputStream.readUTF() / DataOutputStream.writeUTF()用自己的编码方案。我不认为.NET BinaryReader /作家可与其兼容WRT Unicode字符串。然而,你可以手动序列化Unicode字符串作为长度的数据后面的字节序列。

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.

这篇关于C#相当于Java的DataOutputStream类的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆