2

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!

1 Answer 1

1
  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

5
  • Thanks, you gave hints regarding to some areas, but most is still pretty unclear. Especially sending "complex" data, for example float[]. I will definitively check out kryonet, but I'd probably still prefer learning this from ground base. People who discuss programming usually dont come from arses (; Thanks
    – Scavs
    Mar 14, 2015 at 18:52
  • 1
    Arrays would be reasonably "easy". Encapsulate all the values in the array, specifying how many bytes a value takes up. Possibly how many values you are encapsulating too. As you might understand there are a multitude of ways to implement data type serialization, so try to play with it a bit. Playing with how the bytes are stored in memory can also help you understand how it works on the low level. Glad I could be of some help.
    – xscanpix
    Mar 14, 2015 at 21:30
  • The problem with sending such data is that you don't know what you expect to get. For example, client may send 350-500 different types of "data set collections", it may send any of these at any time. So before "unboxing" i have to somehow know "for what to prepare".
    – Scavs
    Mar 15, 2015 at 23:14
  • 1
    If you send it with TCP you always know that you receive it in the order you send it. So implementing some kind of TCP wrapper that adds some more information about how much of a package is part of what object. You could also just send the total length of one object that you send and on the receiver side read the size and read that amount of bytes. (I'm just assuming you want to do it with TCP, since UDP would not be a good choice at all because of its unreliability)
    – xscanpix
    Mar 16, 2015 at 14:12
  • Thanks, i came up with my own "header structure" to read various data types and arrays. The only thing that makes me "suspicious" is that for data transfer i send 2 data sets. First one says how much the second-actual packet will be in length, then i create byte[] with that size. Second packet is the actual bytes. I wonder is there any way to read byte[] without knowing how much bytes there will be in the data packet.
    – Scavs
    Mar 18, 2015 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.