An example of asynchronous socket communication (Windows)

Source: Internet
Author: User
Tags htons

In Windows, there are two methods to change a socket to asynchronous.

 

(1) Call the wsaasyncselect Method

(2) Call the wsaeventselect Method

 

 

You can use the select function to find the socket or conduct training by yourself through wsawaitformultipleevents. However, the restriction of wsawaitformultipleevents is distressing, that is, wsawaitformultipleeventswsawaitformultipleevents can only listen to 64 sockets in each thread, to listen to more sockets, you need to start multiple threads. Although the select function has a limit on the number of sockets, it can listen to a maximum of 1024 sockets, more than wsawaitformultipleevents. The wsaasyncselect method requires a window handle to receive the response message. The following example uses the wsaeventselect and wsawaitformultipleevents method to download the websocket programming. It uses the asynchronous socket method to download the homepage from Baidu and Google.

 

 

# Example. cpp

 

# Include "stdafx. H"
# Include <errno. h>
# Include <string. h>
# Include <assert. h>
# Include <winsock2.h>
# Include <windows. h>
# Include <WINBASE. h>
# Include <Io. h>

 

Int _ tmain (INT argc, _ tchar * argv []) {

 

Wsadata;
Wsastartup (0x0202, & wsadata );

Int sock [2];

Int Pos;
Unsigned int Port = 80;
Int res = 0;
Char * str_robots = "Get/index.html HTTP/1.0/R/nhost: www.baidu.com/R/nuser-AGENT: Test (Test@263.com)/R/n/R/N ";

Struct sockaddr_in server [2];
Char Buf [10240];

Sock [0] = socket (af_inet, sock_stream, 0 );
Sock [1] = socket (af_inet, sock_stream, 0 );

Server [0]. sin_family = af_inet;
Server [0]. sin_port = htons (port );
Server [0]. sin_addr.s_un.s_addr = inet_addr ("119.75.213.61"); // www.baidu.com

Server [1]. sin_family = af_inet;
Server [1]. sin_port = htons (port );
Server [1]. sin_addr.s_un.s_addr = inet_addr ("64.233.189.104"); // www.google.com

 

Wsaevent hevent [2];
Hevent [0] = wsacreateevent ();
Hevent [1] = wsacreateevent ();
Wsanetworkevents events;
Wsaeventselect (sock [0], hevent [0], fd_read | fd_write | fd_connect );
Wsaeventselect (sock [1], hevent [1], fd_read | fd_write | fd_connect );

 

Connect (sock [0], (struct sockaddr *) & server [0], sizeof (server ));

Connect (sock [1], (struct sockaddr *) & server [1], sizeof (server ));

 

Int Total = 2;

While (total)
{

Res = wsawaitformultipleevents (2, hevent, false, 10, false); // wsa_infinite
For (INT I = 0; I <2; I ++ ){
Int result = wsaenumnetworkevents (sock [I], hevent [I], & events );
If (result = socket_error ){
Printf ("error/N ");
Break;
} Else if (result = wsa_wait_timeout ){
Continue;
} Else {
If (events. lnetworkevents & fd_read ){
Pos = 0;
Count = 0;
Do {
Count = Recv (sock [I], BUF + POs, 512, 0 );
Pos + = count;
} While (count> 0 );
Buf [POS] = '/0 ';
Printf ("Buf = % s/n", Buf );
Total --;
} Else if (events. lnetworkevents & fd_write ){
Send (sock [I], str_robots, strlen (str_robots), 0 );
} // End if

} // End if

} // End
} // End while
 

 

Return (0 );

}

 

For asynchronous socket, if you use MFC in Windows, it will become much easier. You can use the casyncsocket class, which encapsulates various methods of asynchronous socket requests.

 

Programming in Linux/Unix systems, you can use the fnctl function to convert a socket into an asynchronous one, and perform round-robin Through select, poll, and epoll.

 

 

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.