0

The problem is GetBufferSize method, I send a buffer size of 40 and it returns some weird number 54124.
Why is this dose the java int byte code different from C# int byte code?

JAVA CLIENT

mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), UTF8)), true);

private void sendMessage(final String message) {

    if (mBufferOut != null && message != null) {
        try {
            Log.d("_TAG", "Message length: " + Integer.toString(message.length()));

            Log.d("_TAG", "Sending: " + message);
            mBufferOut.print(message.length());
            mBufferOut.flush();
            mBufferOut.print(message);
            mBufferOut.flush();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}

The code above sends two messages: the first one is the buffer size and the second is the data itself.

C# SERVER

private void Read() {
    int size = GetBufferSize();

    byte[] myReadBuffer = new byte[size];
    int numberOfBytesRead = 0;
    string str = "";

    do
    {
        numberOfBytesRead = networkStream.Read(myReadBuffer, 0, myReadBuffer.Length);

        str = Encoding.UTF8.GetString(myReadBuffer, 0, numberOfBytesRead);
    } while (networkStream.DataAvailable);
}

private int GetBufferSize()
{
    byte[] myReadBuffer = new byte[4];
    int numberOfBytesRead = 0;

    do
    {
        numberOfBytesRead = networkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
    } while (networkStream.DataAvailable);

    if (numberOfBytesRead > 0)
    {
        return BitConverter.ToInt32(myReadBuffer, 0);
    }
    else
    {
        return 0;
    }
}

Any ideas?

1
  • I recommend looking at the bytes you send and those you receive as individual bytes. Check that they are the same. Work out how they represent the human-readable value you are sending. Work out how they represent the erroneous value you're receiving. Commented Sep 22, 2018 at 1:36

2 Answers 2

1

Solution

         `return   Int32.Parse(Encoding.UTF8.GetString(myReadBuffer, 0, myReadBuffer.Length))`;

Problem

public void print (int inum)

Prints the string representation of the specified integer to the target.

The Integer get converted to the string representation of that int then converted to bytes.

Sign up to request clarification or add additional context in comments.

4 Comments

Surely the proper solution would be to send the value as bytes?
Ok but PrintWriter does not allow byte sending or is it , i checked it there is no methods available all of them take some other data type
I'm not familiar with Java, but: I think your problem stems from using text encoding to send your data. I imagine that the stream returned by getOutputStream() is capable of writing bytes. You're wrapping it with OutputStreamWriter which, according to the docs, is a bridge from character streams to byte streams. It depends how much work you want to put into it really. You could also just send "length,message" and then GetBufferSize() could just read until it hits the comma, and then parse the length out of the pre-comma string.
I'm also not familiar with Java, i searched out you can warp it with binaryformatter and write bytes directly.
0

It's likely you're having an issue regarding endianness. Java stores data in big endian, and .NET stores data based on your system architecture. If you're on a x86 machine, it's likely you're trying to read big-endian data as if it were little-endian. You'll have to reverse the byte array before reading it

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(buffer);
            }

Here's more info in endianness.

4 Comments

Actually both the Oracle JVM and Microsoft CLR store data in the same way, it’s system dependent. How they write that data to streams may differ.
35 buffer size turned into 859111424 ,i added your code did not work
the android device is VM ,and Windows applications is x86
@vandench Data inside the JVM is stored depending on the system architecture. However any multibyte data item stored in a program running on the JVM are big-endian. See page 69 of this book

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.