-2

Okay so i'm receiving bytes from a request..

I call to read the bytes

        byte[] buffer = new byte[1024];
        int resp = socket.Receive(buffer);

If I use this,

 Console.WriteLine(resp)

it returns a integer of 9, meaning 9 bytes are received

when I use

 int byte1 = buffer[0];
 Console.WriteLine(byte1);

it returns

0

so then when I use

        long bytelong = BitConverter.ToInt64(buffer, 0);
        Console.WriteLine(bytelong);

I would expect

 0( + other 7 bytes from the array )

but what I receive is

 -1249284928492

which is wrong, because I know the first byte is

0

Really confused with this..

Hostd
  • 25
  • 2
  • 8
  • Can you add the first 8 bytes you are receiving – TheGeneral Mar 19 '19 at 02:04
  • You may want to use `BitConverter.ToUInt64` and display the value as hex for more clarity: `Console.WriteLine ("{0:X}", bytelong);` – Chris Olsen Mar 19 '19 at 02:04
  • 1
    `-1249284928492` as an 8-byte hex number is `FFFFFEDD20D0CC14`. In other words, the LSB is 0x14, and so your `byte[]` cannot possibly contain `0` at the first element. So the information you've provided in your post is simply incorrect. Either your array starts with the value 0, or passing it to `ToInt64()` returns `-1249284928492`. Both statements cannot be true at the same time. You need to provide at a minimum, a logically consistent question, and really you should be providing a good [mcve] that reliably reproduces the problem. – Peter Duniho Mar 19 '19 at 02:46
  • Well then @PeterDuniho why does int byte1 = buffer[0]; return 0...? – Hostd Mar 19 '19 at 02:48
  • _"why does int byte1 = buffer[0]; return 0"_ -- if you really get the number `-1249284928492` when you call `ToInt64()`, then it does not return 0. I don't know why you think it does, but as I've already said: the two statements are simply not possibly both true. Either the first byte in the array is 0, or the number returned when you call `ToInt64()` is `-1249284928492`. Since you haven't provided a code example demonstrating your claimed behavior, it's not possible to identify where your observation went wrong. But I guarantee you that it has. – Peter Duniho Mar 19 '19 at 03:14

1 Answers1

0

It's not possible to know from the information given exactly what your specific problem is, but here's some general advice for debugging network communications that may help.

The most important thing is to be able to see exactly what data is being sent and received, before it is interpreted and converted by the application(s). The best way to do this is with external line monitoring software such as WireShark. If you're going to be doing a lot of this, get familiar with a tool like that.

But for most jobs that's probably overkill. Just log the actual bytes that you're sending and receiving before you convert them, for example:

var count = socket.Receive(buffer);
Console.WriteLine("Received {0} bytes: {1}", count, BitConverter.ToString(buffer, 0, count));

Secondly, be aware of byte ordering issues when interpreting a byte stream as binary integers. If you don't know for certain that both sender and receiver will be using the same byte ordering, then consider using Network Byte Order (aka Big Endian) for your multi-byte binary values.

For example, assuming your c# socket app is running on Windows, your 64-bit integer value is likely going to be stored as little endian:

long le_value = 0x0807060504030201; // "01-02-03-04-05-06-07-08"
long be_value = IPAddress.HostToNetworkOrder(le_value); // "08-07-06-05-04-03-02-01"

To convert a received network byte order value back to little endian:

ulong le_value = IPAddress.NetworkToHostOrder(be_value);

Hope that helps.

Chris Olsen
  • 3,243
  • 1
  • 27
  • 41