Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

15.2 Initializing and Cleaning Up

Windows socket implementations must be initialized before the socket API can be used by the net library, and must be cleaned up when the net library application exits. To simplify these processes, the net library provides the WinSockInfo class. Its constructor performs the initialization tasks and its destructor performs the termination tasks. To use this class, simply declare an instance of RWWinSockInfo in a scope that encloses all of the net library calls. Usually, instantiating one of these objects in the main() (or WinMain()) portion of your application is sufficient.

or

//1The declaration of sockApiInit invokes the RWWinSockInfo constructor, which causes the initialization of the socket API.
//2Since sockApiInit was the first named object constructed, it will be the last destroyed, insuring that no more net library calls are required for the application. This allows the socket implementation to free any resources associated with the application.

NOTE: If you use Windows and you plan to use any net library objects in a DLL, you must create a custom DLLMAIN() function, and you must create an instance of the RWWinsockInfo class in this function.

15.2.1 Using the net Library in Global Objects

The preceding strategy breaks down if you use the net library in global objects. For example, let's say that the program above also contains a global connection to a server:

The constructor for this global object gets executed before the constructor for the RWWinSockInfo in WinMain() or WinMain(). This can cause problems. One solution is to add a static global RWWinSockInfo object before the definition of globalPortal:

The RWWinSockInfo object defined in line //1 is guaranteed to be constructed before the portal defined in the next line. Similarly, it is not destroyed until after the portal is destroyed. Note that it is not guaranteed to be constructed first if the globals are in different modules.

Nesting RWWinSockInfo objects is not a problem. The first call to the constructor initializes the Winsock DLL, and the last call to the destructor cleans up the Winsock DLL.



Previous fileTop of DocumentContentsIndexNext file

©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.