In-depth understanding of asynchronous socket

zhaozj2021-02-17  47

In-depth understanding of asynchronous socket

CasyncSocket asynchronous socket is the class of the Socket of the MFC package.

Only FD_WRITE notifications will only be issued under three conditions: 1. Use connection or wsaconnect, a socket has been established for the first time; 2. Use accept or wsaaccept, the socket is accepted; Sendto or WSasendTo operation failed, returned WSAEWOULDBLOCK error

And the space of the buffer becomes available. Therefore, one application starts from receiving the FD_WRITE message, it is considered to be in a socket.

Add data. Therefore, the system is to notify the asynchronous socket message response function by mask (Example: fd_write).

In an instance program, the system is set to process asynchronous messages in the following functions: Void Pascal CasyncSocket :: DOCALLBACK (WPARAM WPARAM, LPARAM LPARAM) {IF (wparam == 0 && lparam == 0) Return ;

// HAS THE SOCKET BE CLOSED? CASYNCSOCKET * PSOCKET =

CasyncSocket :: LookupHandle ((socket) WPARAM, TRUE;

// if Yes Ignore Message IF (psocket! = Null) return;

PSocket = CasyncSocket :: LookupHandle ((socket) WPARAM, FALSE); if (psocket == null) {// must be in the middle of an accept call psocket = CasyncSocket :: lookuphandle (invalid_socket,

False); Assert (psocket! = Null); psocket-> m_hsocket = (socket) wParam; CasyncSocket :: DetachHandle (Invalid_Socket, false); CasyncSocket :: attachhandle (psocket-> m_hsocket, psocket,

False);

int nErrorCode = WSAGETSELECTERROR (lParam); switch (WSAGETSELECTEVENT (lParam)) {case FD_READ: // if the asynchronous message to FD_READ, ready to trigger OnReceive () function {DWORD nBytes; if (pSocket-> IOCtl (FIONREAD, & nBytes! Nerrorcode = wsagetlasterror (); if (nbytes! = 0 || NerrorCode! = 0) psocket-> onreceive (NERRORCODE);} Break; Case FD_WRITE: PSocket-> Onsend (NerrorCode); Break; Case FD_OOB: PSocket- > OnOutOfBandData (nErrorCode); break; case FD_ACCEPT: pSocket-> OnAccept (nErrorCode); break; case FD_CONNECT: pSocket-> OnConnect (nErrorCode); break; case FD_CLOSE: pSocket-> OnClose (nErrorCode); break;}} from The above code can be seen that the MFC is to process asynchronous network events through the Docallback () function. When we use AsyncSelect (FD_READ); then, if successful, the system will trigger function OnReceive (nErrorCode); likewise FD_WRITE the corresponding function OnSend (nErrorCode); likewise FD_ACCEPT the corresponding function OnAccept (nErrorCode); likewise FD_CONNECT the corresponding function OnConnect (nErrorCode); The same fd_close corresponds to the function onclose (nerrorcode); the same fd_oob corresponds to the function onoutofbanddata;

转载请注明原文地址:https://www.9cbs.com/read-28591.html

New Post(0)