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 is expected to receive a notification when receiving data on the socket (that is, when the read is ready)
FD_WRITE is expected to receive a notification when data (that is, write preparation) can be sent on the socket.
FD_OOB expects to receive a notification when there is out-of-band data on the socket.
FD_ACCEPT is expected to receive a notification when there is an external connection on the socket.
FD_CONNECT is expected to receive a notification when the socket connection is established.
FD_CLOSE is expected 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 we need to cancel the message sending to the Socket network eventLEvent is set 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 );
WhereWVersionRequested ensures that SOCKETS can run normally in the DLL version. If not, an error message is returned.
Let's take a look at the following code to see how