Is there a way to tell if WSAStartup() has been called?
Is there a way to tell if WSAStartup() has been called?
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.
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.
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.
i'm going to include other methods for encription and such. that way it will be likeCode: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);
Also you can pass existing sockets to this class and use all methods included.Code:cNetworkConnections chatserver; //blaaaa //blaaa chatserver.send(clients[0], buf, sizeof(buf), 0);
Just to help avoid errors it would check to see if WSAStartup() has been called.
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.
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
>>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(){
Not exactly...like this: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(){
That way none of the cNetworkConnections would have to worry about (re)initializing Winsock because the main program has already done it for them.Code:winmain() { WSAStartup(); cNetworkConnections chatServer; cNetworkConnections fileServer; cNetworkConnections myOwnWebServer; ... WSACleanup(); }
"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C