Thread: Winsock.h and Winsock2.h

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    95

    Winsock.h and Winsock2.h

    I have issues with the winsock headers.

    When a header(1) needs the function and type declarations for winsock, I just include winsock2.h there. Fine.

    Unfortunately, another header included by said header(1) may include windows.h, which seems to include winsock.h, unless you define WIN32_LEAN_AND_MEAN (which I don' t necessarily want to do).
    When winsock.h gets included, of course I get a deluge of compiler errors, as it's trying to redefine all the types and structs from winsock2.h.

    Is there a nice solution (what's the difference between the winsock headers - I've always just assumed I needed 2, since I always use 2-specific functions)?

    ATM I just include winsock2.h whereever I just need windows.h, since w2 doesn't pull in winsock.h, and only pulls in windows.h if the include guard windows.h uses isn't already defined. Of course, that's not ideal, and only works if I write all the code.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Just include winsock2.h before you include the header file which is including winsock.h.

    what's the difference between the winsock headers
    The headers are for two different librarys. One is for ws2_32.lib (winsock2.h) and the other is for wsock32.lib (winsock.h).

    If the computer you are using supports winsock2, then use that one. winsock2 contains all the functions that the first winsock does, so you dont lose any functionality. But winsock2 supports some additional cool stuff like Layered Service Providers.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Thanks.

    My only concern is, if say a library user includes a header{that includes the winsock2 header} in order to use my library functions , after including something that pulls in winsock.h. This would cause the compiler errors.

    Ok, you could just require users to include the library header prior to anything that would pull in winsock.h, but that seems rather restrictive..

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well the correct way of handling this is to count on the programmer who is using your library functions to NOT include winsock.h on their own. If they have to use a header which includes winsock.h, then they would include that header AFTER including your own header.

    If you really want a way around this, you can always do this in your own code:

    Code:
    #ifndef _WINSOCKAPI_
    #define <winsock2.h>
    #endif
    This will of course break any of your code which requires winsock2.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Well yes - if the code didn't need winsock 2 bits, it wouldn't be including the winsock 2 header anyway

    It seems like rather poor design by MS - winsock2 should probably just include winsock.h, rather than declaring and defining everything winsock.h defines itself.

    grumblepreprocessorgrumble.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It's not poor design. The winsock2 library isn't a add on to winsock, it's an entire re-write. If you look at the different headers, many of the macro definitions are very different (for example FD_SET). That's why winsock2.h cant include winsock.h.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-04-2009, 01:57 PM
  2. winsock2.h problem
    By xixpsychoxix in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-08-2008, 05:08 PM
  3. winsock2.h
    By Carp in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 02:42 PM
  4. Errors when including winsock2.h
    By skiingwiz in forum Windows Programming
    Replies: 2
    Last Post: 12-27-2002, 07:32 PM