0

Yesterday I started to transfer some code from a working C#-Example to the equivalent Java-Application.

While I can read/write bytes successfully in C# and with that, control any of my devices like powering them on, change the color of the LED-Lamp etc, it's not working in Java.

Here's the working C# example:

try
{
    Int32 port = 4000;
    TcpClient client = new TcpClient(server, port);
    NetworkStream stream = client.GetStream();
    stream.Write(data, 0, data.Length);
    data = new Byte[256];
    Int32 bytes = stream.Read(data, 0, data.Length);
    stream.Close();
    client.Close();
    return data;
}
catch (ArgumentNullException e)
{
    Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
    Console.WriteLine("SocketException: {0}", e);
}
return null;

And this is the corresponding Java-Code I have which is not writing the "data" array correctly. I needed to change from byte to int because the target device is expecting numbers from 0 to 255 and a byte in Java covers from -128 to 127.

try
{
    int port = 4000;
    Socket socket = new Socket(server, port);
    OutputStream out = socket.getOutputStream();
    DataOutputStream dos = new DataOutputStream(out);

    // send length of data first
    dos.writeInt(data.length);

    // append all the integers to the message
    for(int i = 0; i < data.length; i++){
        dos.writeInt(data[i]);
    }

    // confirm send
    dos.flush();

    data = new int[256];
    InputStream in = socket.getInputStream();
    byte[] b = new byte[in.available()];
    in.read(b, 0, b.length);

    dos.close();
    out.close();
    socket.close();

    return data;
}
catch (Exception e)
{
    System.console().format("Exception: {0}", e);
}
return null;

I hope you can help me and show me my error.

gorkem
  • 731
  • 1
  • 10
  • 17
  • What kind of stream returned by `data` using `return data` in both code? It is possible that `in.read(b, 0, b.length)` auto-converting unsigned numbers from stream to signed ones, i.e. assigning `byte` to `int` array doesn't change those "broken" values automatically. – Tetsuya Yamamoto Jun 09 '17 at 07:08
  • Well, my code is a bit weird because i have write and read actions in the same part. I think `in.read(b, 0, b.length)` will convert automatically from unsigned numbers to signed ones, but in my opinion this should not affect the fact that the data sent above never gets sent ? – Benjamin Schurtenberger Jun 09 '17 at 07:16
  • I suggest you read how stream works here: https://stackoverflow.com/questions/1830698/what-is-inputstream-output-stream-why-and-when-do-we-use-them. To read `int` data written by `DataOutputStream` you may need `DataInputStream` & execute `readInt` method from it instead using byte array. – Tetsuya Yamamoto Jun 09 '17 at 07:33
  • "I needed to change from byte to int because the target device is expecting numbers from 0 to 255 and a byte in Java covers from -128 to 127." No, they're just bytes. Write them as bytes. Ignore the fact that Java will treat a bit pattern of 11111111 as -1 decimal and C# will treat the same bit pattern as 255 decimal. *They're the same bits on the wire.* – Jon Skeet Jun 09 '17 at 07:48
  • @JonSkeet thanks man, that was the mistake i didn't see. Its working now :) – Benjamin Schurtenberger Jun 09 '17 at 09:21

0 Answers0