I hate the winsock library!

A place for Ogre users to discuss non-Ogre subjects with friends from the community.
Post Reply
User avatar
KungFooMasta
OGRE Contributor
OGRE Contributor
Posts: 2087
Joined: Thu Mar 03, 2005 7:11 am
Location: WA, USA
x 16
Contact:

I hate the winsock library!

Post 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!! :x

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? :x (Will I run into this problem if I switch to RakNet?)
Creator of QuickGUI!
User avatar
subquantum
Goblin
Posts: 270
Joined: Tue Oct 02, 2007 10:23 pm
Location: Rochester, NY
Contact:

Re: I hate the winsock library!

Post 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.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: I hate the winsock library!

Post 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? :x (Will I run into this problem if I switch to RakNet?)
Try POCO. :wink:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
johnhpus
Platinum Sponsor
Platinum Sponsor
Posts: 1186
Joined: Sat Apr 17, 2004 2:49 am
x 3

Re: I hate the winsock library!

Post 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.
CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
x 58
Contact:

Re: I hate the winsock library!

Post 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.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: I hate the winsock library!

Post 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)
User avatar
Nauk
Gnoll
Posts: 653
Joined: Thu May 11, 2006 9:12 pm
Location: Bavaria
x 36
Contact:

Re: I hate the winsock library!

Post 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? :x (Will I run into this problem if I switch to RakNet?)
Try POCO. :wink:
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.
User avatar
DanielSefton
Ogre Magi
Posts: 1235
Joined: Fri Oct 26, 2007 12:36 am
Location: Mountain View, CA
x 10
Contact:

Re: I hate the winsock library!

Post 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...
User avatar
KungFooMasta
OGRE Contributor
OGRE Contributor
Posts: 2087
Joined: Thu Mar 03, 2005 7:11 am
Location: WA, USA
x 16
Contact:

Re: I hate the winsock library!

Post 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.
Creator of QuickGUI!
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: I hate the winsock library!

Post by jacmoe »

I agree on Raknet.
It simply has it all. If you need it. :)
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: I hate the winsock library!

Post 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).
User avatar
KungFooMasta
OGRE Contributor
OGRE Contributor
Posts: 2087
Joined: Thu Mar 03, 2005 7:11 am
Location: WA, USA
x 16
Contact:

Re: I hate the winsock library!

Post by KungFooMasta »

Awesome! So I went ahead and added _WINSOCKAPI_ as a preprocessor definition, and it works! :D

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?
Creator of QuickGUI!
MattStevens
Goblin
Posts: 239
Joined: Mon Apr 07, 2008 10:27 pm
x 4

Re: I hate the winsock library!

Post 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
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: I hate the winsock library!

Post 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.
User avatar
KungFooMasta
OGRE Contributor
OGRE Contributor
Posts: 2087
Joined: Thu Mar 03, 2005 7:11 am
Location: WA, USA
x 16
Contact:

Re: I hate the winsock library!

Post 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. :D
Creator of QuickGUI!
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: I hate the winsock library!

Post 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).
User avatar
lonewolff
Ogre Magi
Posts: 1207
Joined: Wed Dec 28, 2005 12:58 am
x 6

Re: I hate the winsock library!

Post by lonewolff »

Winsock isn't so bad :D
Post Reply