winsock IO 模型的笔记

//调用Windows Sockets DLL

WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);

... ...

//结束Winsock库,卸载这个库并释放资源

int WSACleanup (void);

... ...

//套接字,就是一个指向传输提供者的句柄,是一个独立的类型—S O C K E T.由两个函数建立

SOCKET socket (

int af,

int type,

int protocol

);

... ...

SOCKET WSASocket(

int af,

int type,

int protocol,

LPWSAPROTOCOL_INFO lpProtocolInfo,

GROUP g,

DWORD dwFlags);

... ...
//尝试重载enum的++运算符
http://topic.csdn.net/t/20060621/13/4834473.html

//20090531重叠IO事件通知代码整理
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2),&wsaData)) //调用Windows Sockets DLL
{
m_GRAPH1STATE=_T("无法创建服务器socket");
UpdateData(FALSE);
WSACleanup();
return;
}

unsigned long ul=1;
//int nRet;
//ServerSocket[0]=socket(PF_INET,SOCK_STREAM,0);
ServerSocket[0]=WSASocket(AF_INET, SOCK_STREAM, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED);
//nRet=ioctlsocket(ServerSocket[0],FIONBIO,(unsigned long *)&ul);
//if (nRet==SOCKET_ERROR)
//{
// return;
//}

sockStruct[0].sin_family=AF_INET; //使用TCP/IP协议
sockStruct[0].sin_port = htons(8001);
sockStruct[0].sin_addr.S_un.S_addr = inet_addr("192.168.1.7");
if(connect(ServerSocket[0],(LPSOCKADDR)&sockStruct[0],sizeof(sockStruct[0])) == SOCKET_ERROR)
{
int m_errorcode=WSAGetLastError();
//return;
}

WSAOVERLAPPED AcceptOverlapped;
WSAEVENT m_Event;
WSABUF DataBuf[64];
DWORD dwEventTotal = 0,
dwRecvBytes = 0,
Flags = 0;

int nSockIndex = 0; // socket数组的编号
BOOL bOverlapped = FALSE; // 是否处理重叠请求

Flags = 0;
m_Event = WSACreateEvent();
ZeroMemory(&AcceptOverlapped,sizeof(WSAOVERLAPPED));

char buffer[4096];
ZeroMemory(buffer,4096);

AcceptOverlapped.hEvent = m_Event; // 关联事件

DataBuf[0].len = 4096;
DataBuf[0].buf = buffer;
// 投递第一个WSARecv请求,以便开始在套接字上接受数据
if(WSARecv(ServerSocket[0] ,&DataBuf[0],1,&dwRecvBytes,&Flags,
& AcceptOverlapped ,NULL) == SOCKET_ERROR)
{
int mError=WSAGetLastError();
if(WSAGetLastError() != WSA_IO_PENDING)
{
// 返回WSA_IO_PENDING是正常情况,表示IO操作正在进行,不能立即完成
// 如果不是WSA_IO_PENDING错误,就表示操作失败了
AfxMessageBox("错误:第一次投递Recv操作失败!!此套接字将被关闭!");
closesocket(ServerSocket[0]);
ServerSocket[0] = INVALID_SOCKET;

WSACloseEvent(m_Event);

}
}

WSAOVERLAPPED AcceptOverlapped;
WSAEVENT m_Event;
WSABUF DataBuf[64];
DWORD dwEventTotal = 0,
dwRecvBytes = 0,
Flags = 0;

int nSockIndex = 0; // socket数组的编号
BOOL bOverlapped = FALSE; // 是否处理重叠请求

Flags = 0;
m_Event = WSACreateEvent();
ZeroMemory(&AcceptOverlapped,sizeof(WSAOVERLAPPED));

char buffer[4096];
ZeroMemory(buffer,4096);

AcceptOverlapped.hEvent = m_Event; // 关联事件

DataBuf[0].len = 4096;
DataBuf[0].buf = buffer;
// 投递第一个WSARecv请求,以便开始在套接字上接受数据
if(WSARecv(ServerSocket[0] ,&DataBuf[0],1,&dwRecvBytes,&Flags,
& AcceptOverlapped ,NULL) == SOCKET_ERROR)
{
int mError=WSAGetLastError();
if(WSAGetLastError() != WSA_IO_PENDING)
{
// 返回WSA_IO_PENDING是正常情况,表示IO操作正在进行,不能立即完成
// 如果不是WSA_IO_PENDING错误,就表示操作失败了
AfxMessageBox("错误:第一次投递Recv操作失败!!此套接字将被关闭!");
closesocket(ServerSocket[0]);
ServerSocket[0] = INVALID_SOCKET;

WSACloseEvent(m_Event);

}
}

// Process overlapped receives on the socket
while(TRUE)
{
DWORD Index;
// Step 5:
// Wait for the overlapped I/O call to complete
Index = WSAWaitForMultipleEvents(1,
&m_Event, FALSE, WSA_INFINITE, FALSE);

// Index should be 0 because we
// have only one event handle in EventArray

// Step 6:
// Reset the signaled event
WSAResetEvent(m_Event);

// Step 7:
// Determine the status of the overlapped
// request
unsigned long BytesTransferred=0;
WSAGetOverlappedResult(ServerSocket[0],
&AcceptOverlapped, &BytesTransferred,
FALSE, &Flags);

// First check to see whether the peer has closed
// the connection, and if so, close the
// socket

if (BytesTransferred == 0)
{
printf("Closing socket %d/n", ServerSocket[0]);

closesocket(ServerSocket[0]);
WSACloseEvent(m_Event);
return;
}

// Do something with the received data
// DataBuf contains the received data
//...

// Step 8:
// Post another WSARecv() request on the socket

Flags = 0;
ZeroMemory(&AcceptOverlapped,
sizeof(WSAOVERLAPPED));

AcceptOverlapped.hEvent = m_Event;

DataBuf[0].len = 4096;
DataBuf[0].buf = buffer;

if (WSARecv(ServerSocket[0] ,&DataBuf[0],1,&dwRecvBytes,&Flags,
& AcceptOverlapped ,NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSA_IO_PENDING)
{
// Unexpected error
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值