[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 tot开发者_JAVA技巧al_length = ntohl(ipData->TOTAL_LENGTH);
These are the headers I am including.
#include <winsock2.h>
#include <ws2tcpip.h>
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?
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.
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.
精彩评论