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.