WSAStartup

This is a discussion on WSAStartup within the Windows Programming forums, part of the Platform Specific Boards category; Is there a way to tell if WSAStartup() has been called?...

  1. #1
    adivinedude
    Guest

    WSAStartup

    Is there a way to tell if WSAStartup() has been called?

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    by your program or another?
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  3. #3
    adivinedude
    Guest
    By my program.
    I'm working on a class using sockets and need to know if it has been called, to avoid calling it again.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Reference Counting. I suspect you have some type of class wrapper or something for TCP/IP or the like. Just keep track of when you call it and when you cleanup.

  5. #5
    adivinedude
    Guest
    thats not the problem.
    This class will handle all the sockets that are
    1) made by it.
    2) passed to it.

    here is some cut and paste.
    Code:
    class cNetworkConnections {
        private::
    
        public::
            cNetworkConnections();
            ~cNetworkConnections();
            
            void SetVersion(int major, int minor);
            const WORD GetVersion();
    
            /*~~~~*/// Startup functions:
                int StartService();    // ONLY use if you are have already called
                                   // WSAStartup(). Otherwise use StartServiceWSA();
       	    int StartServiceWSA();
            /*~~~~*/// Shoutdown functions: house keeping
    
            int EndService();      // ONLY use if you are going to call WSACleanup().
                                   // Otherwise use EndServiceWSA().
            int EndServiceWSA();
    /*~~~~*/
            char* recv(SOCKET_ID id, char buf, int len, int flags);
            int   send(SOCKET_ID id, const char* buf, int len, int flags);
    i'm going to include other methods for encription and such. that way it will be like
    Code:
    cNetworkConnections chatserver;
    //blaaaa //blaaa
    chatserver.send(clients[0], buf, sizeof(buf), 0);
    Also you can pass existing sockets to this class and use all methods included.

    Just to help avoid errors it would check to see if WSAStartup() has been called.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well this may not be the best way but it should work. You can call the function "getsockopt( )" and pass in desired information to be filled in. If this function call fails with SOCKET_ERROR use WSAGetLastError( ) and see if the code is "WSANOTINITIALISED" if it is, guess what! It's not initialised. Good luck sir.

  7. #7
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Is there a reason against just calling WSAStartup() at the beginning of the program (handling the situation accordingly if an error is returned) and having your network class assume that Winsock has been started? When your program quits, make the main thread responsible for cleaning up rather than the network classes. That is, make initialization and shutdown global so it will automatically enable your network classes.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  8. #8
    adivinedude
    Guest
    >>Is there a reason against just calling WSAStartup()

    not really. I just want to avoid making the call twice.

    when the class is destroyed ~cNetworkCnnections() will read if Startup() or StartupWSA() has been called and perform accordingly

    >>That is, make initialization and shutdown global so it will automatically enable your network classes

    are you talking about
    Code:
    cNetworkConnections clients;
    winmain(){
    ?

  9. #9
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Originally posted by adivinedude
    >>That is, make initialization and shutdown global so it will automatically enable your network classes

    are you talking about
    Code:
    cNetworkConnections clients;
    winmain(){
    ?
    Not exactly...like this:

    Code:
    winmain() {
       WSAStartup();
    
       cNetworkConnections chatServer;
       cNetworkConnections fileServer;
       cNetworkConnections myOwnWebServer;
       ...
    
       WSACleanup();
    }
    That way none of the cNetworkConnections would have to worry about (re)initializing Winsock because the main program has already done it for them.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WSAStartup on new thread
    By abraham2119 in forum C Programming
    Replies: 2
    Last Post: 12-17-2008, 08:05 PM
  2. calling multiple WSAStartup()
    By Da-Nuka in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-16-2005, 08:45 AM
  3. Winsock Safety in C
    By sean in forum C Programming
    Replies: 1
    Last Post: 11-24-2004, 09:15 AM
  4. Unresolved External Error (WSAStartup, WSACleanup)
    By Epo in forum Networking/Device Communication
    Replies: 1
    Last Post: 01-16-2004, 11:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21