Example of using the wsaasyncselect model to receive data from a Socket Client

Source: Internet
Author: User

(1) Declare the network event Response Function in the H file

Afx_msg void onnetevent (wparam, lparam ); 

 

(2) map the network_event event in the CPP file to the onnetevent () function

Begin_message_map (cinstrumentstatusview, cformview) <br/> on_message (network_event, onnetevent) <br/> end_message_map () 

 

(3) onnetevent () function body, which responds to fd_connect, fd_close, and fd_read events.

Void cinstrumentstatusview: onnetevent (wparam, lparam) {<br/> int ievent = callback (lparam); <br/> int error = wsagetselecterror (lparam ); // If error! = 0, it means failure <br/> socket cursock = (socket) wparam; <br/> If (error) <br/>{< br/> afxmessagebox ("onnetevent () receives the error message"); <br/> closesocket (wparam); <br/> return; <br/>}< br/> switch (ievent) {<br/> case fd_connect: <br/> onsockconnect (error); // void cinstrumentstatusview :: onsockconnect (INT error); <br/> break; </P> <p> case fd_close: <br/> closecurrsockhandle (); // void cinstrumentstatusview: closecurrsockhandle (); <br/> break; </P> <p> case fd_read: <br/> onsockreceive (); // Recv buffer and copy to m_pcurcommand-> m_commandbuffer <br/> break; </P> <p >}</P> <p>} 

 

 

(4) create a socket and use wsaasyncselect () to register the event for the socket m_sockclient

If (wsaasyncselect (m_sockclient, m_hwnd, network_event, fd_close | fd_read | fd_connect) = socket_error) <br/>{< br/> closesocket (m_sockclient ); <br/> // MessageBox ("ccomwnd failed to register the asynchronous network event! "); <Br/> // wsacleanup (); <br/> return 0; <br/>} 

 

(5) onsockreceive () function body. After receiving the fd_read message, execute this function.

Void cinstrumentstatusview: onsockreceive () {<br/> int readresults = 0; <br/> bool ready = false; <br/> char tempchar; <br/> wsaasyncselect (m_sockclient, m_hwnd, network_event, 0); // closes the event on the socket event m_sockclient. <br/> // read it cyclically, read one byte each time <br/> // while (total! = M_pcurcommand-> m_nexpectedbytes) <br/> while (! Ready) // when the end character is read, ready = true <br/>{< br/> readresults = Recv (m_sockclient, & tempchar ); <br/> If (readresults = socket_error) // Recv returns Exception Handling <br/>{< br/> If (wsaewouldblock = wsagetlasterror ()) <br/>{< br/> continue; // If the exception is "blocking exception ", continue to the next loop <br/>}< br/> else // handle non-wsaewouldblock exceptions, possibly due to network disconnection <br/>{< br/> m_bconnected = false; <br/> break; <br/>}< br/> else if (readresults = 0) <br/>{< br/> afxmessagebox (_ T ("Connection closed on the peer end ")); <br/>}< br/> else if (readresults = 1) <br/>{< br/> If (m_pcurcommand-> addtoreceivebuffer (tempchar) Ready = true; <br/> // In addtoreceivebuffer, judge whether tempchar is an ending character. If yes, true is returned, and the while loop condition ready is set to true, exit loop <br/>}< br/> wsaasyncselect (m_sockclient, m_hwnd, network_event, fd_close | fd_read | fd_connect); // The data is received successfully, recover the network event of the socket m_sockclient </P> <p>} 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.