CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2004
    Posts
    293

    Question 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.

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    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;
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    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 ;-)).

  4. #4
    Join Date
    Dec 2004
    Posts
    293

    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.

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    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.

  6. #6
    Join Date
    Dec 2004
    Posts
    293

    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??

  7. #7
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    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 ;-)

  8. #8
    Join Date
    Dec 2004
    Posts
    293

    Re: How to write my own htons\htonl ??

    Thanks a lot !!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured