WSAEventSelect程式設計模型實現

2021-06-04 06:03:21 字數 3008 閱讀 8431

伺服器端:

//把#include 放到最前面

//至於原因,那是因為windows.h這個標頭檔案已經包含了winsock.h

和winsock2.h貌似有衝突

#include #include #include //#include "winsock.h"

//#pragma comment(lib,"wsock32")

#pragma comment(lib,"ws2_32")

int main()

if (lobyte(wsadata.wversion)!=2 || hibyte(wsadata.wversion!=2))

wsaevent eventarray[wsa_maximum_wait_events]; /*the array to store the event object*/

socket sockarray[wsa_maximum_wait_events]; /*the array to store the socket object*/

int neventtotal = 0; /*the total number of the events*/

ushort nport = 6000;

/*create the listen socket and bind to specific port*/

socket slisten = socket(af_inet,sock_stream,ipproto_tcp);

sockaddr_in addr;

addr.sin_family = af_inet;

addr.sin_port = htons(nport);

addr.sin_addr.s_un.s_addr = inaddr_any;

ret = bind(slisten,(sockaddr *)&addr,sizeof(addr));

if (socket_error == ret)

ret = listen(slisten,10);

if (socket_error == ret)

//獲取傳送緩衝區和接受緩衝區的大小

printf("%d\n",so_max_msg_size);

int recvbuf_len;

int recvlength = sizeof(recvbuf_len);

getsockopt(slisten,sol_socket,so_rcvbuf,(char *)&recvbuf_len,&recvlength);

printf("recvbuf is: %d\n",recvbuf_len);

int sendbuf_len;

int sendlength = sizeof(sendbuf_len);

getsockopt(slisten,sol_socket,so_sndbuf,(char *)&sendbuf_len,&sendlength);

printf("sendbuf is: %d\n",sendbuf_len);

/*create a event object and bind it to a socket*/

wsaevent event = wsacreateevent(); //注意需要手動重置事件或者使用可以重置事件的api操作

//wsaevent event = createevent();

//呼叫這個函式後,套接字被自動設定為非阻塞模式

wsaeventselect(slisten,event,fd_accept|fd_close /*|fd_write*/);

//這樣做是為了當網路時間發生時,確定是在哪個套接字上發生的。

//之後就在這個套接字上做相應的操作。

eventarray[neventtotal] = event;

sockarray[neventtotal] = slisten;

neventtotal++;

/*deal with the network events*/

while (true)

socket snew = accept(sockarray[i],null,null);

wsaevent event1 = wsacreateevent();

wsaeventselect(snew,event1,fd_read|fd_close|fd_write);

eventarray[neventtotal]=event1;

sockarray[neventtotal]=snew;

neventtotal++;}}

else if (netevent.lnetworkevents & fd_read) //處理fd_read通知訊息 }}

else if (netevent.lnetworkevents & fd_close) //處理fd_close通知訊息 }}

end//*/}

} }return 0;

}

客戶端:

#include #include #include #include #include "winsock.h"

#pragma comment(lib, "wsock32")

using namespace std;

#define command 100

int main()

//測試fd_write事件觸發機制

char clisend_msg[10000]="";

int recv_len;

recv_len = recv(m_hclientsocket,clisend_msg,sizeof(clisend_msg),0);

while (recv_len > 0)

sleep(10000);

//測試fd_close事件觸發機制

closesocket(m_hclientsocket);

wsacleanup();

return 0;

}

WSAEventSelect 功能描述

wsaeventselect 簡述 確定與所提供的fd 網路事件集合相關的乙個事件物件。include int wsaapi wsaeventselect socket s,wsaevent heventobject,long lnetworkevents s 乙個標識套介面的描述字。lnetwor...

封裝WSAEventSelect模型

include include pragma comment lib,ws2 32.lib pragma once class casynchandler virtual casynchandler virtual int acceptnotify socket hsocket,const char...

WSAEventSelect模型詳解

wsaeventselect 是 winsock 提供的一種非同步事件通知i o模型,與 wsaasyncselect模型有些類似。該模型同樣是接收 fd 之類的網路事件,但是是通過事件物件控制代碼通知,而非像 wsaasyncselect一樣依靠windows的訊息驅動機制。與wsaasyncse...