CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2009
    Posts
    166

    Windows SOCKET type question

    Hello,

    On http://www.sockets.com/winsock.htm it says:

    Because the SOCKET type is unsigned, compiling existing source code from, for example, a UNIX environment may lead to compiler warnings about signed/unsigned data type mismatches.

    This means, for example, that checking for errors when the socket() and accept() routines return should not be done by comparing the return value with -1, or seeing if the value is negative (both common, and legal, approaches in BSD). Instead, an application should use the manifest constant INVALID_SOCKET as defined in winsock.h. For example:

    TYPICAL BSD STYLE:

    s = socket(...);
    if (s == -1) /* or s < 0 */
    {...}

    PREFERRED STYLE:

    s = socket(...);
    if (s == INVALID_SOCKET)
    {...}


    But, when I run a program on Windows and I do this line of code:

    int test = INVALID_SCOKET;

    test has the value or -1. According to http://www.sockets.com/winsock.htm#Accept accept() returns a SOCKET type, so how can SOCKET have a -1 value if it is an unsigned int?

    Regards,
    Ellay K.

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Windows SOCKET type question

    Quote Originally Posted by ekhule View Post
    Hello,

    On http://www.sockets.com/winsock.htm it says:

    Because the SOCKET type is unsigned, compiling existing source code from, for example, a UNIX environment may lead to compiler warnings about signed/unsigned data type mismatches.

    This means, for example, that checking for errors when the socket() and accept() routines return should not be done by comparing the return value with -1, or seeing if the value is negative (both common, and legal, approaches in BSD). Instead, an application should use the manifest constant INVALID_SOCKET as defined in winsock.h. For example:

    TYPICAL BSD STYLE:

    s = socket(...);
    if (s == -1) /* or s < 0 */
    {...}

    PREFERRED STYLE:

    s = socket(...);
    if (s == INVALID_SOCKET)
    {...}


    But, when I run a program on Windows and I do this line of code:

    int test = INVALID_SCOKET;

    test has the value or -1. According to http://www.sockets.com/winsock.htm#Accept accept() returns a SOCKET type, so how can SOCKET have a -1 value if it is an unsigned int?

    Regards,
    Ellay K.
    Socket is unsigned in winsock. Use the macro SOCKET instead of int anywhere a socket is used:
    Code:
    SOCKET test = INVALID_SOCKET;

  3. #3
    Join Date
    Mar 2009
    Posts
    166

    Re: Windows SOCKET type question

    The questions i am asking is how can you assign a negative value (INVALID_SOCKET) to a unsigned int (SOCKET) ?

    Regardless, if I use int instead of SOCKET the code works. Because INVALID_SOCKET is a -1 then this code works fine, because accept() returns -1 (like in unix):

    Code:
    int sock = accept(socket, (sockaddr*)(&addr), &addrLength);
    if(sock < 0)
       cout << "ERROR!" << endl;
    So why on Windows would I even bother to change the int to SOCKET? If I keep it as int, it works on both Unix and Windows. Or am I missing something?

    Regards,
    Ellay K.

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Windows SOCKET type question

    In theory, it should work fine. I don't know what MS's rationale was for deciding to deviate from Berkeley on this matter. I doubt you'd end up with 2 billion+ open sockets (the max for a signed int). But, if you use int, you'll get warnings about possible loss of data during conversion, and/or signed/unsigned mismatch comparisons unless you static cast every instance.

  5. #5
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Windows SOCKET type question

    The hex pattern for INVALID_SOCKET is 0xFFFFFFFF. Casting to an unsigned integer will interpret this as decimal value 4,294,967,295, while casting to a signed integer will interpret this as decimal -1.

    If you look at the header file, you will see the following define:
    Code:
    #define INVALID_SOCKET  (SOCKET)(~0)
    where the "~" is the bit-wise one's complement.

    Does this help? (Maybe not, sorry.)
    Last edited by MikeAThon; May 11th, 2010 at 05:56 PM.

  6. #6
    Join Date
    Mar 2009
    Posts
    166

    Re: Windows SOCKET type question

    Yeah it makes perfect sense. So if you declare your return type as int instead of SOCKET it still works on windows if you compare the return value to see if it is less than 0. This makes Unix socket code work also on windows without modification. I dont know why they made such a big deal about using the SOCKET data type, when it's really not needed.

    Regards,
    Ellay K.

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