Wsastartup and wsacleanup
Wsastartup should be used in pairs with wsacleanup. The function of wsastartup is to initialize Winsock DLL, and wsacleanup is to unbind from the socket library and release the system resources occupied by the socket library.
In Windows, socket is implemented in the form of DLL. There is a counter in the DLL. Only wsastartup is called for the first time to load the DLL. The subsequent call is simply to add a counter, while the wsacleanup function is opposite, every call reduces the counter by 1. When the counter is reduced to 0, the DLL will be detached from the memory! Therefore, the number of wsastartup calls should be the number of wsacleanup.
Wsastartup () Description:
# Include <Winsock. h>
Int Pascal far wsastartup (word wversionrequested, lpwsadata );
The maximum version number that can be used by calling wversionrequested Windows Sockets API. The high byte indicates the secondary version (corrected) number, and the low byte indicates the primary version number.
The lpwsadata pointer to the wsadata data structure, which is used to receive details about the implementation of Windows Sockets.
Note:
This function must be the first Windows Sockets function called by an application or DLL. it allows applications or DLL to specify the version number of the Windows Sockets API and obtain the implementation details of a specific Windows Sockets. an application or dll can call further Windows Sockets API functions only after a successful wsastartup () call.
To support Windows Sockets implementation and applications that may be different from Windows Sockets 1.1 in the future, a protocol is defined in wsastartup. the caller and Windows Sockets DLL of wsastartup () notify each other of the highest versions they support, and confirm that the highest version of the other is acceptable. at the wsastartup () function entry, Windows Sockets DLL checks the version required by the application. if the version is higher than the minimum version supported by the DLL, the call is successful and the DLL returns the highest version supported by the DLL in whighversion. In the wversion, the DLL returns the higher version and the smaller version in wversionrequested. then, Windows Sockets DLL assumes that the application will use wversion. if the wversion field in the wsdata structure is unacceptable to the caller, it should call the wsacleanup () function and either search in another Windows Sockets DLL or initialization fails.
This protocol allows Windows Sockets DLL and Windows Sockets applications to support a certain range of Windows Sockets versions. if version ranges overlap, the application can successfully use Windows Sockets DLL. the following charts show how wsastartup () works in different applications and Windows Sockets DLL versions:
Application version dll version wversionrequested wversion whighversion final result
1.1 1.1 1.1 1.1 1.1 Use 1.1
1.0 1.1 1.0 1.1 1.0 1.0 use 1.0
1.0 1.0 1.1 1.0 1.0 1.1 Use 1.0
1.1 1.0 1.1 1.1 1.1 1.1 Use 1.1
1.1 1.0 1.1 1.0 1.0 failed
1.0 1.1 1.0 -- wsavernotsupported
1.0 1.1 1.0 1.1 1.1 1.1 1.1 Use 1.1
1.1 2.0 1.1 2.0 1.1 1.1 Use 1.1
2.0 1.1 2.0 1.1 1.1 failed
The following code snippet shows how wsastartup () is called only for Windows Sockets 1.1 applications:
Word wversionrequested;
Wsadata;
Int err;
Wversionrequested = makeword (1, 1 );
Err = wsastartup (wversionrequested, & wsadata );
If (Err! = 0 ){
/* Tell the user that we couldn't find a useable */
/* Winsock. dll .*/
Return;
}
/* Confirm that the Windows Sockets DLL supports 1. 1 .*/
/* Note that if the DLL supports Versions greater */
/* Than 1.1 in addition to 1.1, it will still return */
/* 1.1 in wversion since that is the version we */
/* Requested .*/
If (lobyte (wsadata. wversion )! = 1 |
Hibyte (wsadata. wversion )! = 1 ){
/* Tell the user that we couldn't find a useable */
/* Winsock. dll .*/
Wsacleanup ();
Return;
}
/* The Windows Sockets DLL is acceptable. Proceed .*/
The following code snippet demonstrates how wsastartup () negotiation is performed on Windows Sockets DLL that only supports version 1.1:
/* Make sure that the version requested is >=1.1 .*/
/* The low byte is the major version and the high */
/* Byte is the minor version .*/
If (lobyte (wversionrequested) <1 |
(Lobyte (wversionrequested) = 1 &&
Hibyte (wversionrequested) <1 ){
Return wsavernotsupported;
}
/* Since we only support 1.1, set both wversion and */
/* Whighversion to 1.1 .*/
Lpwsadata-> wversion = makeword (1, 1 );
Lpwsadata-> whighversion = makeword (1, 1 );
Once the application or DLL is successfully called by wsastartup (), it can continue to call other required Windows Sockets APIs. after using the Windows Sockets DLL service, the application or dll must call wsacleanup () to allow Windows Sockets DLL to release any resources of the application.
The actual implementation details of Windows Sockets are described in the wsadata structure as follows:
Struct wsadata {
Word wversion;
Word whighversion;
Char
Szdescription [wsadescription_len + 1];
Char szsystemstatus [wsasysstatus_len + 1];
Unsigned short imaxsockets;
Unsigned short imaxudpdg;
Char far * lpvendorinfo;
};
The members of this structure are:
Member usage
Wversion Windows Sockets DLL is expected to use the Windows Sockets standard version by the caller.
The maximum version of the Windows Sockets specification that whighversion DLL supports. Generally, it is the same as wversion.
Szdescription is an ASCII string ending with null. Windows Sockets DLL copies the description and vendor description of the implementation of Windows Sockets to this string. this section of text (up to 256 characters) may contain any character, but the manufacturer notices that the control and format characters are not included: the most likely use of this string is to display it in the status message.
Szsystemstatus is an ASCII string ending with null. Windows Sockets DLL copies the relevant status and configuration information to this string. windows Sockets DLL uses this domain only when this information is available to users or support personnel: it should not be considered an extension of the szdescription domain.
The maximum number of interfaces that an imaxsockets process can open. the implementation of Windows Sockets can provide a global set of interface pools to allocate to any process, or separate supporting interface resources for each process. this number indicates how Windows Sockets DLL or network software is configured. application programmers can use this number to determine whether the Windows Sockets can be used by applications. for example, an X Windows Server may check imaxsockets when it is started: if it is smaller than 8, the application should display an error message asking the user to reconfigure the network software. (This is a possible situation for szsystemstatus .) obviously, it is not guaranteed that a specific application can be actually allocated to an imaxsockets set of interfaces, because other Windows Sockets applications may be in use.
The maximum UDP datagram size that can be sent or received by the Windows Sockets application in bytes. if the application does not provide a limit, imaxudpdg is 0. in many implementations of the Berkeley set interface, there is an implicit limit of 8192 bytes for UDP datagram guidance. the implementation of Windows Sockets can be defined based on the allocation of the shard reorganization buffer. for Windows Sockets, the minimum value of imaxudpdg is 512. note that it is unwise to try to send a broadcast number larger than the maximum transmission unit (MTU) over the network without considering the imaxudpdg value. (The Windows Sockets API does not provide a mechanism to discover MTU, but it must be no less than 512 bytes .)
Lpvendorinfo points to the remote pointer of the data structure specified by the vendor. The definition of this structure (if provided) exceeds the scope of this specification.
If an application or DLL needs to obtain the wsadata structure information multiple times, it must call wsastartup () multiple times (). however, the wversionrequired parameter is assumed to be the same in all wsastartup () calls. That is, the application or DLL cannot change the version number of Windows Sockets after wsastartup () is called for the first time.
Each wsastartup () call must have a wsacleanup () call, so that Level 3 (third-party) dll can use Windows Sockets DLL related to the application. this means, for example, if the application calls wsastartup () three times, it must call wsacleanup () three times. the first two calls to wsacleanup () do nothing except reduce the built-in counter. The last wsacleanup () call to the task releases all required resources.
Return Value:
0 succeeded.
Otherwise, one of the following error codes is returned. note that the error code obtained by applications calling the wsagetlasterror () mechanism cannot be used, because Windows Sockets dll may not create a "previous error" Information Storage region of customer data.
Description of the Windows Sockets provider:
Every Windows Sockets application must call wsastartup () before calling other Windows Sockets APIs. In this way, this function can be used for initialization.
Further descriptions are discussed in the description of wsacleanup.
Error code:
Wsasysnotready indicates that the network subsystem on which network communication depends is not ready yet.
The version of the Windows Sockets API required by wsavernotsupported is not provided by the specific Windows Sockets implementation.
The Windows Sockets version specified by the wsaeinval application is not supported by this DLL.
See:
Send (), sendto (), wsacleanup ()
From: http://linger7510.blog.ccidnet.com/blog-htm-do-showone-uid-51606-itemid-183369-type-blog.html