What is Winsock?
Winsock is a network programming interface in windows. It is developed from BSD socket in UNIX and is a programming interface unrelated to the network protocol.
Build a programming environment
Winsock has two major versions on common Windows platforms: winsock1 and Winsock2. Write compatible with winsock1ProgramYou need to reference the header file Winsock. H. if you write a program using Winsock2, You need to reference winsock2.h. In addition, there is a mswsock. h header file, which is specifically used to support the high-performance network program Extension function on the Windows platform. When using the Winsock. h header file, you also need the wsock32.lib library file. When using winsock2.h, you need ws2_32.lib. If you use the extended API in mswsock. H, you need mswsock. Lib. After correctly referencing the header file and linking the corresponding library file, you can build an environment for compiling Winsock network programs.
Initialize Winsock
Each Winsock program must use wsastartup to load the appropriate Winsock dynamic link library. If the loading fails, wsastartup will return socket_error. This error is wsanotinitialised. The definition of wsastartup is as follows:
Int wsastartup (
Word wversionrequested,
Lpwsadata
);
Wversionrequested specifies the Winsock version you want to load. The high byte specifies the secondary version number, while the low byte specifies the primary version number. You can use macro makeword (x, y) to specify the version number. Here, X represents the main version, and Y represents the next version. Lpwsadata is a pointer to the wsadata structure. wsastartup will fill the structure with the Winsock dynamic chain it loads
Library Information.
Lpwsadata is a pointer to the wsadata structure. wsastartup will fill the structure with the Winsock dynamic chain it loads
Library Information.
Lpwsadata is a pointer to the wsadata structure. wsastartup will fill the structure with the Winsock dynamic chain it loads
Library Information.
typedef struct wsadata
{< br> word wversion;
word whighversion;
char szdescription [wsadescription_len + 1];
char szsystemstatus [wsasys_status_len + 1];
unsigned short imaxsockets;
unsigned short imaxudpdg;
char far * lpvendorinfo;
} wsadata, * lpwsadata;
Wversion is the Winsock version you will use. whighversion is the maximum version supported by the loaded Winsock dynamic library. Note that their high bytes represent the next version, and the low bytes represent the main version.
Szdescription and szsystemstatus are set by Winsock of a specific version. In fact, szdescription and szsystemstatus are not very useful.
Imaxsockets indicates the maximum number of concurrent sockets. Its value depends on available hardware resources.
Imaxudpdg indicates the maximum length of a datagram. However, to obtain the maximum length of a datagram, you need to use wsaenumprotocols to query the protocol.
The maximum number of concurrent sockets is not a magic number. It is determined by the available physical resources.
Lpvendorinfo is the manufacturer information retained for Winsock implementation, which is of no use on Windows platform.
Windows 95 and later versions support Winsock 2.2. even so, you cannot think that these windows platforms support the latest Winsock version. To make your programs run on most platforms, it is best to use the winsock1.1 specification.
After using the Winsock interface, you need to call the following function to release the resources it occupies:
Int wsacleanup (void );
If the function fails to be called, the Operating System Automatically releases the function. Each wsastartup call should have a wsacleanup call.
Error Handling
If the Winsock function fails to be called, socket_error (actually-1) is returned. You can call wsagetlasterror to obtain the error details:
Int wsagetlasterror (void );
An error code is returned for calls to this function. The code value is defined in Winsock. h or winsock2.h (based on the version ).Start with wsaeYou can also use wsasetlasterror to customize the error code value.
The following is an example of using the above knowledge to initialize Winsock (remember to reference the Winsock lib library ).
# Include <winsock2.h>
Void main (void)
{
Wsadata;
Int ret;
// Initialize Winsock Version 2.2
If (ret = wsastartup (makeword (2, 2), & wsadata ))! = 0)
{
// Note: Since Winsock failed to load we cannot use
// Wsagetlasterror to determine the specific error
// Why it failed. Instead we can rely on the return
// Status of wsastartup.
Printf ("wsastartup failed with error % d \ n", RET );
Return;
}
// Setup Winsock communication code here
// When your application is finished Call wsacleanup
If (wsacleanup () = socket_error)
{
Printf ("wsacleanup failed with error % d \ n", wsagetlasterror ());
}
}