Windows Sockets network programming (iii) -- Windows Sockets 1.1 Programming Author: Xiaoying, freezing point Studio 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 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; // defines the data information variable int err; // defines the error code variable wversionrequested = makeword (1, 1 ); // assign err = wsastartup (wversionrequested, & wsadata) to the version information; // assign if (Err! = 0) {return; // tell the user that the appropriate version is not 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 the minimum 1.1, that is, if (lobyte (wsadata. wversion )! = 1 | hibyte (wsadata. wversion )! = 1) {wsacleanup (); // tell the user that the appropriate version of return cannot be found;} // Windows Sockets DLL is accepted by the process and 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 Windows provides a function to obtain the latest error code wsagetlasterror (). The recommended writing method is as follows: len = send (s,lpBuffer,len,0); of((len==SOCKET_ERROR)&&(WSAGetLastError()==WSAWOULDBLOCK)){...} |