How to write my own htons\htonl ??
I need to write my own methods since those methods requires to include "Winsock2.h" and it's making me problems including it at different files, especially where at some places it's a template file with only .h file.
How can I write me own htons\htonl methods ??
Thanks.
Re: How to write my own htons\htonl ??
Here is the code for htonl. You can figure out htons from that.
Code:
long lValue1 = 1234;
long lValue2 = 0;
lValue2 |= (lValue1 & 0xFF000000) >> 24;
lValue2 |= (lValue1 & 0x00FF0000) >> 8;
lValue2 |= (lValue1 & 0x0000FF00) << 8;
lValue2 |= (lValue1 & 0x000000FF) << 24;
Re: How to write my own htons\htonl ??
I recommend to solve the problem with the header files instead of rewriting the functions. I know it is a real pain to include winsock.h and winsock2.h, but sometimes it helps to #define the include guard of one of these header files to get rid of the problems they introduce (I know from my own expirience ;-)).
Re: How to write my own htons\htonl ??
Thank you _Superman_ !
Richard: you said something about #define guards, you mean something like:
Code:
#ifndef _WINSOCK2_
#define _WINSOCK2_
#include <Winsock2.h>
#endif;
??
Thank you both for the help.
Re: How to write my own htons\htonl ??
yes, I do.
including winsock2.h might interfere with winsock.h, so I do:
Code:
#define _WINSOCK_
#include <winsock2.h>
that does the trick.
Re: How to write my own htons\htonl ??
What's the difference between what I wrote and what you wrote ?!?!
it looks the same, no??
Re: How to write my own htons\htonl ??
well,
the thing you wrote checks (unnessarily) for a double inclusion of winsock2.h because winsock2.h should define WINSOCK2.
What I wrote is to define _WINSOCK_ to make sure winsock.h is not include any more and make it possible to include winsock2.h afterwards and avoid the duplicate definition of structs and so on.
Check it out yourself ;-)
Re: How to write my own htons\htonl ??