Thread: Winsock Safety in C

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    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,
    Sean.

  2. #2
    Hello,

    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
    Last edited by Stack Overflow; 11-24-2004 at 09:17 PM. Reason: Fixed Typos
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  2. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  3. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  4. weird winsock output and winsock 2 not working
    By whackaxe in forum Networking/Device Communication
    Replies: 4
    Last Post: 07-09-2004, 11:10 AM
  5. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM