Winsock Safety in C
To cut a long story short, I forsee some problems in my program in which I use Winsock (1.1 at the moment), and multiple threads, multiple functions. I've mapped out all the possible scenarios and what I need to do in each one, and I'd have the best option nailed down if someone more experienced with Winsock could answer these questions:
1) Does Windows require an internet connection to declare a WSADATA object or call WSAStartup()?
2) The actual server part of my app may be started and stopped more than once. Can WSAStartup() be called more than once? Or should I wait until the application is going to close completely to call WSACleanup()?
That should be all. From what I know about scope in C, I can figure out the rest of my problems, but I'm having a hard time finding information detailed enough to answer the above, so I'd really appreciate it if someone here can help.
Thanks in advance,
To answer your questions:
1) The WSAStartup function initializes the Winsock2 library for use by your program, and must be the first Winsock2 library function called by your program. Calling the WSAStartup function serves two major purposes:
- The first purpose is to load the DLL that implements the Winsock2 library (i.e. Ws2_32.dll). NOTE: The other Winsock2 library functions expect the Winsock2 library DLL to be loaded before they are called.
- The second purpose for calling the WSAStartup function is to establish what version of the Windows Sockets API your program should use.
Edit: There are return values if WSAStartup fails. If successful, the function returns zero. If an error occured, the function returns one of the following values specifying the error:
- WSAEFAULT [A valid WSADATA structure was not passed for lpWSAData.]
- WSAEINPROGRESS [A Winsock 1.1 blocking operation is in progress.]
- WSAEPROCLIM [The limit of the number of tasks supported by the Winsock implementation has been reached.]
- WSASYSNOTREADY [The network subsystem is not ready for network communication.]
- WSAVERNOTSUPPORTED [The requested version of Winsock is not provided by the current Winsock implementation.]
So to answer your question, you do not have to have an internet connection to declare a WSADATA object or call WSAStartup, though prepare the initialization to fail and return one of the 5 errors listed above.
2) When WSAStartup name-resolution functions such as gethostbyname and WSACleanup are called in a loop on Windows 9x or Windows NT 4.0, there is a small memory leak in each iteration. The Hostent structure that is allocated by the Winsock name-space provider is not freed after WSACleanup is called, because the memory is allocated on a per-thread basis and is freed only when the thread exits. If WSAStartup is called again before the thread exits, another Hostent structure is allocated.
WSACleanup frees the resources allocated when the program began using Winsock functions. Your program must call this function once it no longer needs to use Winsock functions, so that it frees the resources the prior call to WSAStartup consumed. The resources will not be freed unless the program calls WSACleanup the same number of times as it called WSAStartup (in case that function had been called more than once).
To answer this question, you can call WSAStartup more than once, though be sure to call WSACleanup according to the amount started. And be careful for memory leaks.
- Stack Overflow