bobber205
bobber205

Reputation: 13362

[Windows] Net to Host Not Working

The value is 10240 or 2800 in hex. TOTAL_LENGTH is a unsigned short. 0028 in decimal is 40 which is what I am expecting (or is at least a reasonable value).

Any ideas why I am getting a 0 instead of a 40? Thinking about reversing the bits myself but really don't want to. xD

unsigned short total_length = ntohl(ipData->TOTAL_LENGTH);

These are the headers I am including.

#include <winsock2.h>
#include <ws2tcpip.h>

Upvotes: 0

Views: 75

Answers (3)

t0mm13b
t0mm13b

Reputation: 34592

In conjunction to Kornel's answer, you need to be made aware of how the data is stored, the one big mistake is to assume the data is in the way you expect. Different platforms, different processors, the keyword is endianess. Some are stored from High to Low byte, the others are stored from Low to High byte. That is the sole purpose of using 'ntohs' and family. See here for an indepth usage of it by Beej, the network programming site.

Incidentally, you can use this as a standalone function if you were say, processing a data file from a different platform that has a different endianess, you could use this to convert the data to the right endian architecture prior to processing. It does not harm the overhead and guarantee success of processing the data.

Hope this helps, Best regards, Tom.

Upvotes: 0

Chris H
Chris H

Reputation: 6581

try ntohs. It was built for shorts. I'm guessing that it makes a difference since they bothered to make functions for the different types.

Upvotes: 0

Kornel Kisielewicz
Kornel Kisielewicz

Reputation: 57525

u_long WSAAPI ntohl(
  __in  u_long netlong
);

The result is a long, and you're assigning it to a short. Check if it doesn't get cut.

Also, if it's a short, then why aren't you using ntohs?

Upvotes: 1

Related Questions