Page 1 of 1
I hate the winsock library!
Posted: Wed May 20, 2009 5:49 am
by KungFooMasta
Between ENet and my SplashScreen code (which includes windows.h), I can't seem to compile my project anymore, and I don't know why!!
Code: Select all
1>c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(91) : warning C4005: 'AF_IPX' : macro redefinition
1> c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(460) : see previous definition of 'AF_IPX'
1>c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(127) : warning C4005: 'AF_MAX' : macro redefinition
1> c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(479) : see previous definition of 'AF_MAX'
1>c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(163) : warning C4005: 'SO_DONTLINGER' : macro redefinition
1> c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(402) : see previous definition of 'SO_DONTLINGER'
1>c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(206) : error C2011: 'sockaddr' : 'struct' type redefinition
1> c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(485) : see declaration of 'sockaddr'
1>c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(384) : error C2143: syntax error : missing '}' before 'constant'
1>c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(384) : error C2143: syntax error : missing ';' before 'constant'
... (continues on for hundreds of errors)
I thought I fixed these problems in the past, and now it haunts me again. Some stupid problem with including
winsock2.h multiple times or something.
Does anybody know of a C++ networking library, one that uses namespaces and doesn't have a problem if multiple files link against its headers?

(Will I run into this problem if I switch to RakNet?)
Re: I hate the winsock library!
Posted: Wed May 20, 2009 5:53 am
by subquantum
If you want low-level sockets stuff, the Alhem library does that very nicely. Pretty sure they're GPL though, if that's an issue.
Re: I hate the winsock library!
Posted: Wed May 20, 2009 6:40 am
by jacmoe
KungFooMasta wrote:Does anybody know of a C++ networking library, one that uses namespaces and doesn't have a problem if multiple files link against its headers?

(Will I run into this problem if I switch to RakNet?)
Try POCO.

Re: I hate the winsock library!
Posted: Wed May 20, 2009 6:54 am
by johnhpus
Any sort of include guard should keep this from happening. If the file winsock2.h doesn't have an include guard, make a new file called Include_Winsock2.h and wrap the "#include <winsock2.h>" in your own include guard, then use your proxy file instead.
Re: I hate the winsock library!
Posted: Wed May 20, 2009 6:56 am
by CABAListic
POCO is no ENet substitute, though.
@kungfoomasta: Why the heck would your splashscreen code include ENet headers? Unless I misunderstood that, this sounds to me as if you might want to rearrange code so that the splashscreen is in a separate compile unit.
Re: I hate the winsock library!
Posted: Wed May 20, 2009 8:11 am
by Kojack
The problem is include order.
You have to include winsock2.h before anything includes windows.h in the same cpp. Windows.h includes the old winsock.h file which will define everything before winsock2.h does.
(Winsock2.h sets up an include guard to block winsock.h from being loaded, but winsock.h and windows.h don't block winsock2.h)
Re: I hate the winsock library!
Posted: Wed May 20, 2009 4:26 pm
by Nauk
jacmoe wrote:KungFooMasta wrote:Does anybody know of a C++ networking library, one that uses namespaces and doesn't have a problem if multiple files link against its headers?

(Will I run into this problem if I switch to RakNet?)
Try POCO.

I am using Raknet in a project with many different namespaces and dependencies, no problem at all, also I think currently it is one of the best choices out there, coming with a lot of turnkey ready and nicely matured solutions in the package. There was a license change recently too, which makes it free to use for projects under 250k gross revenue as long as you put up the Raknet Logo.
Re: I hate the winsock library!
Posted: Wed May 20, 2009 4:54 pm
by DanielSefton
Nauk wrote:I am using Raknet in a project with many different namespaces and dependencies, no problem at all, also I think currently it is one of the best choices out there, coming with a lot of turnkey ready and nicely matured solutions in the package. There was a license change recently too, which makes it free to use for projects under 250k gross revenue as long as you put up the Raknet Logo.
I second that completely.
RakNet would make your life so much easier, honestly. There's no reason not to use it now in pretty much any UDP networked app, especially since the license change.
Though, I don't think you should go through the hassle of changing to another library just because of namespace issues...
Re: I hate the winsock library!
Posted: Wed May 20, 2009 7:00 pm
by KungFooMasta
Thanks all for the responses, I think Kojack's post sums up the problem. I'll try to see if I can get around this by staring at my include order and seeing where files are coming in from, but ultimately I'll probably switch to RakNet. Not only to fix this lame issue, where includes have to be carefully included in the correct order, but also to check out all the extra features RakNet has.
@CABAListic: My splash screen class includes windows.h, which includes the old winsock headers, and somehow (honestly I don't know, I use forward declarations whenever possible) this is being included into another file that includes winsock2 headers afterwards.
Re: I hate the winsock library!
Posted: Wed May 20, 2009 8:58 pm
by jacmoe
I agree on Raknet.
It simply has it all. If you need it.

Re: I hate the winsock library!
Posted: Wed May 20, 2009 10:16 pm
by Kojack
Or you can ignore the include order and just put #define _WINSOCKAPI_ as the first line in the cpp.
The winsock2.h file defines both _WINSOCKAPI_ (to stop the old one loading) and _WINSOCK2API_ (to avoid including itself several times).
Re: I hate the winsock library!
Posted: Thu May 21, 2009 6:04 pm
by KungFooMasta
Awesome! So I went ahead and added _WINSOCKAPI_ as a preprocessor definition, and it works!
I do get a lot of warnings, although this is fine, considering I won't have any issues of this nature anymore:
Code: Select all
1>c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(29) : warning C4005: '_WINSOCKAPI_' : macro redefinition
1> command-line arguments : see previous definition of '_WINSOCKAPI_'
1>GaiaCommandManager.cpp
1>c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(29) : warning C4005: '_WINSOCKAPI_' : macro redefinition
1> command-line arguments : see previous definition of '_WINSOCKAPI_'
1>GaiaCommand.cpp
1>c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(29) : warning C4005: '_WINSOCKAPI_' : macro redefinition
1> command-line arguments : see previous definition of '_WINSOCKAPI_'
1>GaiaGameState.cpp
I thought of adding _WINSOCK2API_ to preprocessor definitions, but that means the file will never be loaded, right?
Re: I hate the winsock library!
Posted: Fri May 22, 2009 4:54 pm
by MattStevens
you can also define WIN32_LEAN_AND_MEAN before any windows.h include. It will strip out some includes (like winsock.h), and if you're missing some important onces you can always include them yourself after.
At least that's what I do when using ENet and it works well.
- Matt
Re: I hate the winsock library!
Posted: Fri May 22, 2009 6:29 pm
by Kojack
Yep, winsock.h is in the non WIN32_LEAN_AND_MEAN section, so defining it will stop winsock (and around 17 other headers) from being used.
Re: I hate the winsock library!
Posted: Fri May 22, 2009 8:45 pm
by KungFooMasta
I'll try that out. Thanks all for the understanding and solutions, I don't think I'll be having the redefinition problems anymore.

Re: I hate the winsock library!
Posted: Sat May 23, 2009 1:43 am
by Kojack
Although with socket programming you'll soon be looking back on these redefinition problems with fondness.

I got hit by some of the freaky windows 2000+ changes to winsock's udp stuff, until I eventually tracked down a fix.
I found it pretty important to do this to every socket I made:
Code: Select all
DWORD dwBytesReturned = 0;
BOOL bNewBehavior = FALSE;
WSAIoctl(send_socket, SIO_UDP_CONNRESET, &bNewBehavior, sizeof(bNewBehavior), NULL, 0, &dwBytesReturned, NULL, NULL);
That turns back on the BSD / Win98 style of socket behaviour.
UDP is supposed to be fire and forget. You never know if a packet reaches it's destination, and it has no effect on the sender. But win2000 and above changed that. If you send a udp packet through socket on computer A to a destination socket on computer B, but that destination socket has closed because the program crashed or didn't neatly shut down, then B sends back a connection reset message control message. The standard behaviour when A receives the reset message is to ignore it and continue like normal. The new behaviour is to flag the socket on A as invalid, which makes every winsock function on the socket fail, even if you are trying to send to a completely different (and still working fine) computer. The only way I found to restore the socket is to delete it and make a new one.
Most of my students hit the problem (when one of their clients crashed, the server would start returning errors on every send attempt, until I found the code above).
Re: I hate the winsock library!
Posted: Tue May 26, 2009 6:31 am
by lonewolff
Winsock isn't so bad
