Windows asynchronous sockets (WSASocket)

Source: Internet
Author: User

Asynchronous sockets: If you use a blocked socket, it's OK under the console! If it is a WIN32 program, then it is easy to create the interface of suspended animation, because the receiving function has been waiting for the message to come in before it will be returned! So at this time the interface has been suspended animation state, if you move, then it is easy to really die. The solution is as follows: first: With multithreading, multithreading and socket combinations, you can use a single thread to receive messages. This will not affect other threads, such as interface threads, even if the receive function is blocked. But if you use multithreading, you have to take into account the problem of synchronization between multithreading, as well as critical resources. Second: Use asynchronous sockets. But with asynchronous sockets, at least you need to know about the Windows Messaging mechanism. Asynchronous sockets work based on Windows messages, and, like other message functions in MFC, when there is a message, the socket's related function is triggered. For example, when we double-click, the system senses the message and responds. The same is true of asynchronous sockets, where the receive function works when there is a network message, otherwise the receive function is not working. Use the following steps:
Wsadata Wsadata; WORD wversion= Makeword (2,2);//need to open a socket with version 2.2if(WSAStartup (wversion,&wsadata)! =0 ){//Open Failed ...}//determine if the version is 2.2if(Lobyte (wsadata.wversion)! =2|| Hibyte (wsadata.wversion)! =2) {WSACleanup ();}//in fact, the code is the same, just become the wsa*** function! SOCKET M_clientsocket;m_clientsocket= WSASocket (Af_inet,sock_dgram,0Null0,0);if(M_serversocket = =invalid_socket) {MessageBox ("failed to create socket ...");} //Registering Network Events//In fact, this function is the key, first of all, the last parameter is Fd_read actually//There are also the following parameters to fill in://event:fd_accept//Event:fd_address_list_change//Event:fd_group_qos//Event:fd_oob//Event:fd_qos//Event:fd_read//Event:fd_write//This function is called the registration message, meaning that if M_clientsocket receives the message//then send um_recvmessage this message, why is the message received//? because the registered is Fd_read Ah, if registered fd_accept then there is//Send * * message when a person requests a connectionif(socket_error==WSAAsyncSelect (M_clientsocket,m_hwnd,um_recvmessage,fd_read)) {//registration failed.} //receive function declaration! However, this declaration function must be written to the//Declare_message_map (), because this function is to be routed as a message//in the process of the function, in plain, this function is called by the message map to call theafx_msg LRESULT onserverrecvmessage (WPARAM wparam,lparam LPARAM);D eclare_message_map ()//receive function definition! The definition is finished, the above is declared, but how to call it! LRESULT cserverdlg::onserverrecvmessage (WPARAM wparam,lparam LPARAM) {Switch(LoWord (LParam)) { CaseFd_read:}return 0;} //This is the establishment of the message routing, must write to two macros, hehe, the previous few days just read Hou Junjie MFC, so feel this special essence, I remember when I wrote the code when I actually wrote to another class, fortunately I understand the principle, 2 minutes to find the wrong, if the previous words, It is estimated that no days can be found. HahaBegin_message_map (xx sub-class, XX parent Class) On_message (Um_recvmessage,&xx Sub-category: Onserverrecvmessage) End_message_map ()

Tidy up the idea, above, registered the Fd_read message. If a socket variable receives a message, it sends a um_recvmessage message, and we associate the message with On_message (Um_recvmessage,&xx subclass: Onserverrecvmessage). So after sending out the um_recvmessage message, we call Onserverrecvmessage, the function is to receive the message, and then deal with it! In fact, it is equal to the socket and Windows message driver combined, but it is very convenient to use, compared to multithreading save a lot of other steps.

Windows asynchronous sockets (WSASocket)

Related Article

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.