0

I have a C# socket server I want to send to my android client my buffer array size with BinaryWriter class. But my client can not read the integer value with readInt method. Can anyone help me where is my wrong?

C# code:

NetworkStream ns = new NetworkStream(socket.socket);
BinaryWriter bw = new BinaryWriter(ns);
bw.Write(message.Length);// message byte[] length 116868
bw.Write(message);
bw.Flush();
bw.Close();

Android code:

DataInputStream inputStream = new DataInputStream(sc.getInputStream());                                      
int len  = inputStream.readInt();// readed integer value -851443200

UPDATE My code has a big endian little endian problem. I'm trying to sort byte array on server side but my client sometimes get the correct value and sometimes dont. here is my server side code again:

NetworkStream ns = new NetworkStream(socket.socket);
BinaryWriter bw = new BinaryWriter(ns);
byte[] dataLength = BitConverter.GetBytes(message.Length);
//here i convert little endian to big endian for communucation with the client
Array.Sort(dataLength, 0, dataLength.Length);
bw.Write(dataLength);
bw.Flush();
bw.Close();
10
  • did you try reading it as byte instead of int? Commented Nov 11, 2019 at 11:56
  • No i will try. But I want to send int value and get int because i need to know the size of the byte[] in the client. Commented Nov 11, 2019 at 12:05
  • Print the the four bytes length on the sending side in hexadecimal notation. Do the same at the receiving side. It migth be that there is a big endian little endian problem and that you have to swap a bit. Commented Nov 11, 2019 at 14:25
  • bw.Write(message.Length); Are you shure that will write four bytes? inputStream.readInt(); . Are you shure that will read four bytes? Better try to read four bytes, print them hexadecimal and see if they are the same as the four sent but in a diffrent sequence. Commented Nov 11, 2019 at 14:51
  • No i'am not :( i am serching the fastest way for this situation. Can i do this task with a diffrent method? Commented Nov 11, 2019 at 17:54

1 Answer 1

1

This is a swap bytes of integer in java. You could use it for C# too.

private int swapint(int intvalue)
{
    int byte0 = ((intvalue >> 24) & 0xFF);
    int byte1 = ((intvalue >> 16) & 0xFF);
    int byte2 = ((intvalue >> 8) & 0xFF);
    int byte3 = (intvalue & 0xFF);

    int swappedvalue = (byte3 << 24) + (byte2 << 16) + (byte1 << 8) + (byte0);

    return swappedvalue;
}
Sign up to request clarification or add additional context in comments.

2 Comments

you saved my day! This code run very well. Can you explain me what we do exactly here or can you publish any resource? I am beginner in sockets and I want to learn.
If you print intvalue hexadecimal before and after swap you will exactly see what happens.

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.