[Document Introduction]Windows Sockets is extended from Berkeley Sockets. Based on the Inheritance of Berkeley Sockets, Windows Sockets is extended.
I. Introduction
Windows Sockets is extended from Berkeley Sockets. Based on the Inheritance of Berkeley Sockets, Windows Sockets is extended. These extensions mainly provide some asynchronous functions and support asynchronous selection of network events that comply with the WINDOWS message driver features.
Windows Sockets consists of development components and running components.
Development components: Windows Sockets implementation documentation, application interface (API) import to database, and some header files.
Running components: Windows Sockets dynamic link library (WINSOCK. DLL) for the application interface ).
Ii. Main extension description
1. asynchronous selection mechanism:
The asynchronous selection function of Windows Sockets provides the network event selection of the message mechanism. When you use it to register a network event, the corresponding Window Function of the application receives a message, the message indicates the network event and event-related information.
Windows Sockets provides an asynchronous selection function WSAAsyncSelect () that registers network events of interest to the application. When these events occur, the corresponding Window Function of the application receives a message.
The function structure is as follows:
Int pascal far WSAAsyncSelect (SOCKET s, HWND hWnd, unsigned int wMsg, long lEvent );
Parameter description:
HWnd: Window handle
WMsg: message to be sent
LEvent: event (The following is the event content)
Value: |
Meaning: |
FD_READ |
Expect to receive a notification when receiving data on the socket (that is, when the read is ready) |
FD_WRITE |
Notification is received when data (that is, write preparation) can be sent on the socket. |
FD_OOB |
Expect to receive a notification when out-of-band data arrives on the socket |
FD_ACCEPT |
Expect to receive a notification when there is external connection on the socket |
FD_CONNECT |
Expect to receive a notification when the socket connection is established |
FD_CLOSE |
Expect to receive a notification when the socket is closed |
For example, to receive a notification when the socket is ready for reading or writing, the statement is as follows:
Rc = WSAAsyncSelect (s, hWnd, wMsg, FD_READ | FD_WRITE );
If you want to cancel the message sending for A Socket network event, set lEvent to 0.
2. asynchronous request Functions
In Berkeley Sockets, the request service is blocked. In addition to supporting such functions, windows sickets also adds the corresponding asynchronous request function (WSAAsyncGetXByY ();).
3. Blocking Methods
To enable a socket call of an application to be blocked, Windows Sockets can discard the CPU and run other applications. When the call is blocked, it enters a routine called "HOOK, this routine is responsible for receiving and allocating WINDOWS messages so that other applications can still receive their own messages and gain control.
WINDOWS is a non-preemptive multi-task environment. If a program does not give up its control, other programs cannot be executed. Therefore, when designing a Windows Sockets program, despite the system's support for blocking operations, it is still opposed to the use of this operation by programmers. However, because the socket operation of Berkeley Sockets under SUN is blocked by default, it is inevitable that WINDOWS Port SOCKETS to support this operation.
In Windows Sockets implementation, block operations that cannot be completed immediately are processed as follows: DLL initialization → cyclic operations. In a loop, it sends any WINDOWS message and checks whether the Windows Sockets call is complete. If necessary, it can discard the CPU to execute other applications (of course, CPU with hyper-threading will not have this trouble ). You can call the WSACancelBlockingCall () function to cancel this blocking operation.
In Windows Sockets, there is a default blocking processing routine BlockingHook () to simply get and send WINDOWS messages. To process complex programs, WSASetBlockingHook () in Windows Sockets provides the ability for users to install their own blocking processing routines. SWAUnhookBlockingHook () corresponds to this function (), it is used to delete any previously installed blocking processing routines and reinstall the default processing routines. Note that when designing your own blocking processing routine, it cannot use other Windows Sockets API functions except the WSACancelBlockingHook () function. Calling the WSACancelBlockingHook () function in the processing routine will cancel the blocking operation and end the blocking loop.
4. Error Handling
To be compatible with the multi-threaded environment (Windows/UNIX) in the future, WINDOWS Sockets provides two error handling functions to obtain and set the latest error number of the current thread. (WSAGetLastEror () and WSASetLastError ())
5. start and end
Use the WSAStartup () and WSACleanup () functions to start and end the socket.
Iii. Windows Sockets network programming Core
Finally, we can start the real Windows Sockets network program design. However, let's take a look at the content involved in every Windows Sockets network program. Let's take it step by step.
1. Start and end
Among all Windows Sockets functions, only the start function WSAStartup () and the end function WSACleanup () are required.
The START function must be the first function to be used. It allows you to specify the version of the Windows Sockets API and obtain some specific technical details of SOCKETS. The structure is as follows:
int PASCAL FAR WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData);
Among them, wVersionRequested ensures that SOCKETS can run normally in the DLL version. If not, an error message is returned.
Let's take a look at the code below the example and see how to call WSAStartup ().
WORD wVersionRequested; // defines the version information variable.
WSADATA wsaData; // defines the data information variable
Int err; // defines the error code variable.
WVersionRequested = MAKEWORD (); // assign a value to the version information
Err = WSAStartup (wVersionRequested, & wsaData); // assign a value to the error message
If (err! = 0)
{
Return; // tell the user that the appropriate version cannot be found
}
// Confirm that Windows Sockets DLL supports version 1.1.
// The DLL version can be higher than 1.1
// The version number returned by the system is always 1.1 of the minimum requirement, that is, the lowest supported version number in the application and DLL
If (LOBYTE (wsaData. wVersion )! = 1 | HIBYTE (wsaData. wVersion )! = 1)
{
WSACleanup (); // tell the user that the appropriate version cannot be found
Return;
}
// Windows Sockets DLL is accepted by the process. You can proceed to the next step.
When the function is disabled, any socket that has been opened and connected to SOCK_STREAM is reset, but those that have been closed by the closesocket () function, but there are still sockets that have not sent data, unsent data will still be sent. The WSAStartuo () function may be called multiple times when the program is running, but the value of wVersionRequested must be the same for each call.
2. asynchronous request service
In addition to supporting synchronous requests in Berkeley Sockets, Windows Sockets also adds a kind of asynchronous request service function WSAAsyncGerXByY (). This function is the asynchronous version of the blocking request function. When an application calls it, Windows Sockets DLL initializes this operation and returns the caller. This function returns an asynchronous handle to identify this operation. When the results are stored in the buffer provided by the caller and a message is sent to the corresponding window of the application. The common structure is as follows:
HANDLE taskHnd;
char hostname="rs6000";
taskHnd = WSAAsyncBetHostByName(hWnd,wMsg,hostname,buf,buflen);
Note that because Windows Memory objects can be set to removable and discarded, The WIindows Sockets DLL object must be available when operating on memory objects.
3. Asynchronous Data Transmission
Use the send () or sendto () function to send data, and use recv () or recvfrom () to receive data. Windows Sockets does not encourage users to transmit data in blocking mode, because it may block the entire Windows environment. Next we will look at an asynchronous data transmission instance:
Assume that the socket s has used the WSAAsyncSelect () function to register the network event FD_READ and FD_WRITE on it after the connection is established, and the wMsg value is UM_SOCK, then we can add the following branch statement in the Windows message loop:
case UM_SOCK:
switch(lParam)
{
case FD_READ:
len = recv(wParam,lpBuffer,length,0);
break;
case FD_WRITE:
while(send(wParam,lpBuffer,len,0)!=SOCKET_ERROR)
break;
}
break;
4. Error Handling