Click to See Complete Forum and Search --> : How to write my own htons\htonl ??
Shvalb
May 3rd, 2009, 03:43 PM
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.
_Superman_
May 4th, 2009, 12:31 AM
Here is the code for htonl. You can figure out htons from that.
long lValue1 = 1234;
long lValue2 = 0;
lValue2 |= (lValue1 & 0xFF000000) >> 24;
lValue2 |= (lValue1 & 0x00FF0000) >> 8;
lValue2 |= (lValue1 & 0x0000FF00) << 8;
lValue2 |= (lValue1 & 0x000000FF) << 24;
Richard.J
May 4th, 2009, 03:11 PM
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 ;-)).
Shvalb
May 9th, 2009, 08:06 AM
Thank you _Superman_ !
Richard: you said something about #define guards, you mean something like:
#ifndef _WINSOCK2_
#define _WINSOCK2_
#include <Winsock2.h>
#endif;
??
Thank you both for the help.
Richard.J
May 10th, 2009, 12:23 PM
yes, I do.
including winsock2.h might interfere with winsock.h, so I do:
#define _WINSOCK_
#include <winsock2.h>
that does the trick.
Shvalb
May 10th, 2009, 01:11 PM
What's the difference between what I wrote and what you wrote ?!?!
it looks the same, no??
Richard.J
May 10th, 2009, 02:59 PM
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 ;-)
Shvalb
May 10th, 2009, 03:57 PM
Thanks a lot !!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.