Scavs
Scavs

Reputation: 693

Sockets data sending C# and Java - Overally & Specifically

I have couple questions regarding to overall "sending" philosophy and specific question on what are the options to send various data types and structures over sockets.

Current stage

I have Java application which acts like a server and a client in written C#. I succeeded in connecting and sending integer from C# to Java.

Java

while(true){
    try{
        int a = input.readInt(); //input is DataInputStream which is initialized in constructor
        Console.LogClient("Received {"+a+"}");
    }catch(IOException e){
        e.printStackTrace();
    }
}

C#

TcpClient client = new TcpClient(ip, port);
//Connection established

int value = 11;
System.out.println("Sending: "+value);
byte[] bytes = BitConverter.GetBytes(value);

if (BitConverter.IsLittleEndian)Array.Reverse(bytes);

BinaryWriter a = new BinaryWriter(client.GetStream());
a.Write(bytes);
a.Flush();

enter image description here

Questions

  1. How exactly Write() works? Argument which is passed is treated as separate packet data set and is sent at once (on Write()) and flush() ..clears.. something? Or Write() works like and "add to" data set and everything is actually sent on flush()?
  2. As i understand, to talk C# <-> Java everything you send must be converted to lower level byte[] array? While it might be easy for primitive single values, what about sending float[] or int[]?
  3. This is similar to 2., what if I'd like to send string, float[] and int[], but the client/server doesn't know that it will receive. Must i define specific "patterns" or "header"? I must send/write each element by it own or combine? I'm quite lost here.
  4. I failed to send string, i used a.Write("someText"); and receiving with input.readUTF(); but it simply does nothing, looks like it freezes. I even tried converting to byte[] or/and with flipping to big endian.

Future plans

I'd like to learn and implement some optimizations later on, like compressions. As i understand it's pure byte[] manipulation? If so, is the BinaryWriter (C#) and DataInputStream/DataOutputStream (Java) the best solutions? Or there might be problems and best is to which to other class which handles this kind of job.

I hope someone could shed some light on this.

Thank you very much!

Upvotes: 2

Views: 1209

Answers (1)

xscanpix
xscanpix

Reputation: 76

  1. Looking quickly at the documentation of the BinaryWriter class, you can read that the function write(byte[] array) writes the byte array directly to the underlying stream. This means that flush() will probably not make a difference here. Actual citation: "Writes a byte array to the underlying stream."
  2. What you want to do is find/implement a serialization library for C# and Java since the languages doesn't represent the objects the same way binary. A way to do this would probably be to wrap every object with a unique code that you keep track of on both sides (better to read up on serialization first).
  3. A look at the Java network library kryonet might help you with at least the Java part of the problem. (It is a Java to Java serialization library)

I'm just writing with some basic knowledge, so please correct me if I'm completely writing from my arse

Upvotes: 1

Related Questions