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();
  • did you try reading it as byte instead of int? – Joachim Haglund Nov 11 '19 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. – Serchat Chasan Nov 11 '19 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. – blackapps Nov 11 '19 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. – blackapps Nov 11 '19 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? – Serchat Chasan Nov 11 '19 at 17:54
  • @blackapps you are right, it is a big endian little endian problem, can you help me for this ? I try to sort the array in server side and my client sometimes get the correct value and sometimes don't. Now I am sure my server sent 4 bytes. – Serchat Chasan Nov 12 '19 at 11:05
  • Welll you did not tell the four hex values of the integer at sending and receiving side. – blackapps Nov 12 '19 at 11:42
  • `Array.Sort(dataLength, 0, dataLength.Length);` ???? What should that do? – blackapps Nov 12 '19 at 11:44
  • `//here i convert little endian to big endian for communucation with the client` Then show the four hex values of the four bytes before and after sorting. Using sort does not look ok to me. – blackapps Nov 12 '19 at 11:45
  • https://stackoverflow.com/questions/2182002/convert-big-endian-to-little-endian-in-c-without-using-provided-func – blackapps Nov 12 '19 at 11:48

1 Answers1

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;
}
blackapps
  • 8,011
  • 2
  • 11
  • 25
  • 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. – Serchat Chasan Nov 12 '19 at 20:48
  • If you print intvalue hexadecimal before and after swap you will exactly see what happens. – blackapps Nov 12 '19 at 21:00