-
May 3rd, 2009, 03:43 PM
#1
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.
-
May 4th, 2009, 12:31 AM
#2
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;
-
May 4th, 2009, 03:11 PM
#3
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 ;-)).
-
May 9th, 2009, 08:06 AM
#4
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.
-
May 10th, 2009, 12:23 PM
#5
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.
-
May 10th, 2009, 01:11 PM
#6
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??
-
May 10th, 2009, 02:59 PM
#7
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 ;-)
-
May 10th, 2009, 03:57 PM
#8
Re: How to write my own htons\htonl ??
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|