在Windows下面,使一個socket改成非同步有兩種方法。
(1)調用WSAAsyncSelect方法
(2)調用WSAEventSelect方法
對Socket進行輪尋可以使用Select函數,或者自己通過WSAWaitForMultipleEvents進行輪訓,但WSAWaitForMultipleEvents有一個限制令人很苦惱,即WSAWaitForMultipleEventsWSAWaitForMultipleEvents在每個線程中最多隻能監聽64個Socket,如果需要監聽更多的Socket,則需要啟動多個線程;Select函數雖然也有Socket個數限制,但卻是能最多監聽1024個Socket,比WSAWaitForMultipleEvents多多了。WSAAsyncSelect方法需要有接受響應訊息的視窗控制代碼;下面這個例子採用了WSAEventSelect和WSAWaitForMultipleEvents的方法下載網頁的Socket編程,它採用非同步Socket的方式從百度和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 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 for
}//end while
return(0);
}
對於非同步Socket,如果在Windows下面使用MFC的話,事情就變得容易多了,可以使用CAsyncSocket類,這個類封裝了非同步Socket請求的各種方法。
在Linux/Unix系統下編程,可以使用fnctl函數將一個Socket變成非同步,並且通過Select和Poll, epoll進行輪尋。