26

In WinSock2.h, the invalid socket and socket error are defined as these? Is there any significance to this?

#define INVALID_SOCKET  (SOCKET)(~0)
#define SOCKET_ERROR            (-1)
3
  • Probably because that value is "easy to handle". They could have chosen anything else, really.
    – user703016
    Commented May 30, 2012 at 13:40
  • Note that the question is somehow misleading. C++ does not define this define, it is from some other library/API. So the question is more accurately asked as "why does library XYZ" define it as such. And I am pretty sure that the library works under C too, so C or C++ is quite irrelevant at this point too.
    – PlasmaHH
    Commented May 30, 2012 at 13:58
  • @PlasmaHH -- WinSock2.h is the main header for the MS Windows "WinSock" library. It is part of the Windows SDK and is the most common sockets implementation available for Windows. It is almost compatible with BSD sockets. You are, of course, correct that it uses the same header for both C and C++.
    – Michael J
    Commented Jun 7, 2012 at 17:08

6 Answers 6

50
+25

Long ago, early in the morning of a cold rainy day, in the midst of misty green covered hills in a quaint but growing suburb of Seattle, WA, there was a small yet sprawling cluster of red-bricked buildings, in one of which was a medium sized conference room, where a growing number of network software engineers, managers, and architects were gathering. The meeting had started quietly enough, with the mostly male group sipping their cups of tea and coffee, twirling their mechanical pencils while scribbling on their memo pads, or scratching away on their Palm Pilots (there was that guy with the Newton, but everyone ignored him).

As the presenter continued with scrolling through the source code, he was stopped by one of the 5 software architects that had specifically not been invited to the review but showed up anyway. "What, pray tell, is that.", he seethed, wagging his finger just like President Clinton. The presenter stopped and stared at the finger, finding himself somehow mesmerized by the wiggling of the finger as the architect continued to wag. "I said," he began to repeat.

The presenter looked up at the screen, and didn't notice anything amiss. "What are you talking about?" he asked.

The architect raised his eyebrows, as his face first expressed consternation, then turned to indignation, then to constipation, as he yelled "That is a blatant coding style guideline violation!".

The observers began to whisper among themselves, wondering what the issue was all about. But, the presenter was nonplussed (the second kind), quickly gave thanks for the feedback, and continued with the review.

"I will not be ignored!", the architect screamed in a high pitched squeal, as he stood up suddenly, slamming his palms to the table top with a kind of a wimpy thud.

The room erupted as fists pounded the table and managers began devising ways to cover their backsides and engineers began flipping through their coding style manuals, complete with table of contents, index, footnotes, and references.

The presenter remained externally unperturbed, but was feeling annoyed, as he desperately wanted the review completed so that he could go on his ski trip in Snoqualmie. "Are you talking about the type mismatch?", he casually asked.

The architect for a moment looked taken aback, but quickly recomposed himself, "Obviously", he snorted.

The presenter replaced the two characters '-1' with '~0' and turned around. The architect scrutinized the change, then the rest of the screen, then consulted his notes, then finally sat back down.

The rest of the review was uneventful.

1
  • 1
    I can't read anything on the web anymore because my attention span is nonexistent, but I read all of this, and am glad I did. Nice work. This entire discussion exists simply because Microsoft loved (past tense; they've mended their ways) to make life difficult for people writing portable code. In this instance, they made the SOCKET type a UINT_PTR, because, you know, you never know when you'll need 2^64-1 socket descriptors (it's probably because it is internally mapped to HANDLE, which is a pointer).
    – aremmell
    Commented Oct 3, 2023 at 11:56
29

On a two's complement system (and Windows is always two's complement), ~0 is equal to -1, so there's no significance to the compiler.

There may be a significance to the reader: ~0 emphasizes that it's a value with all bits set, whereas -1 emphasizes that it's a value 1 less than 0.

Aside:

On a system which is not two's complement, and assuming that SOCKET is an unsigned type, it is generally wrong to write (SOCKET)(~0). The reason is that on such systems, ~0 does not represent the value -1, it's one of INT_MIN, negative zero, or a trap representation. Hence it will not necessarily convert to type SOCKET as the value with all bits zero, rather it will convert as INT_MAX+2, 0, or goodness-knows-what (perhaps the value with all bits set).

So generally you should initialize unsigned types with -1 to get the value with all bits set. You could use UINT_MAX, or ~0UL, or similar, if you know which unsigned type you're dealing with. But it's not worth it, because -1 works for all unsigned types.

1
  • 1
    @kunj2aan: I hesitate to guess what the author of winsock2.h thought the important difference was. I'm telling you what difference I think it makes. Commented May 30, 2012 at 22:57
4

Mostly it is sloppiness. -1 and ~0 are effectively the same thing.

You could argue that ~0 is better style, as a socket is an unsigned int, but really it doesn't make any difference for any practical purpose.

Because they use "#define" instead of "const unsigned" you are still open to strangeness. e.g.

    unsigned   a = SOCKET_ERROR;
    long long  b = a;
    if (b != SOCKET_ERROR)
        std::cout << "????\n";

might give some people a surprise.

0

because the SOCKET is unsigned, so can't use the (-1).

2
  • Yes, it can. Microsoft actually does use -1 for various other unsigned types in the Win32 API. They could have just as easily done the same thing in this case, eg: #define INVALID_SOCKET (SOCKET)(-1);. It is anyone's guess why they didn't. Commented May 30, 2012 at 15:31
  • This is actually the only correct answer here even though it's largely incomplete. A SOCKET is unsigned and can not be -1 ever. A function that returns a socket can either return a SOCKET or an error code that is signed. That's why these are #defined in this way.
    – Krum
    Commented Oct 25, 2018 at 3:45
0

INVALID_SOCKET is used for functions which return a SOCKET, which a pointer (much like a HANDLE). On 32-bit systems it will be a 32-bit value, while on 64-bit systems it will be a 64-bit value.

When properly cast to an integer of equivalent size, that will be the same as -1. But it is much safer to not assume that it will always be properly cast.

0

Every (-1) is (~0) but not every (~0) is (-1).

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.