-
January 10th, 2015, 01:14 AM
#1
WSAStartup best practices?
I'm writing a network-related module for a larger project. It's cross-platform, and the Windows implementation uses Winsock2.
I'm having some trouble figuring out the best way to handle the need to call WSAStartup()/WSACleanup(). If these things were reference-counted in some way, then the correct thing would be obvious; just make sure to call WSAStartup() in my object's constructor and WSACleanup() in the destructor. However, they aren't as far as I can tell, which makes calling WSACleanup() at any time perilous. An unrelated part of the code might also be trying to use sockets, and I don't want to rip them out from under it! I'm guessing, but I'm not certain, that calling WSAStartup() multiple times does no harm.
Any tips on best practices here?
-
January 10th, 2015, 11:35 AM
#2
Re: WSAStartup best practices?
The online documentation of WSAStartup says
"An application must call the WSACleanup function for every successful time the WSAStartup function is called. " (http://msdn.microsoft.com/de-de/libr...=vs.85%29.aspx)
So maybe your approach with your object's c'tor/d'tor is not completely wrong.
-
January 10th, 2015, 05:19 PM
#3
Re: WSAStartup best practices?
Wrap your code (in a class) such that before you use a socket, you call WSAStartup and when you are finished with the socket, you call WSACleanup. The docs mention that the startup calls are ref counted and only the last cleanup call unloads the dll.
Assuming that your code makes corresponding 1:1 calls in terms of WSAStartup and WSACleanup, you won't yank the rug out from socket calls made from other parts of the code because they too have made at least one WSAStartup call (and *should* make at least one WSACleanup call).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|