IOCP投递进行读操作

原创 2006年06月02日 17:17:00

IOCP投递读操作

当前位置:开发语言频道 > 开发语言 > VC/MFC > 网络编程 > 为什么我的IOCP投递读操作,却收不到数据???
wqrz 发表于 2006-4-18 5:37:37


UINT CIocp::IoWorkerThread(LPVOID lParam)
{

CIocp* pThis = reinterpret_cast(lParam);
ASSERT(pThis);

HANDLE hIocp = pThis->m_hIocp;

DWORD dwIoSize;
ClientContext* lpClientContext=NULL;
OVERLAPPEDPLUS *pOverlapPlus=NULL;
LPOVERLAPPED lpOverlapped=NULL;
bool bError=false;

while(!bError)
{
pOverlapPlus=NULL;
lpClientContext=NULL;

BOOL bIORet = GetQueuedCompletionStatus(
hIocp,
&dwIoSize,
(LPDWORD) &lpClientContext,
&lpOverlapped, INFINITE);
//--------------------------------------------------------

if (bIORet==FALSE
&& lpOverlapped == NULL)
{
DebugPrint("IoWorkerThread - GetQueuedCompletionStatus()错误");
bError=TRUE;
}
else if (bIORet == FALSE
&& lpOverlapped != NULL)
{
// This happens occasionally instead of end-of-file. Not sure why.
closesocket(lpClientContext->m_hSocket);
delete lpClientContext;
DebugPrint("用户非正常退出");
}
else if (bIORet==0)
{
closesocket(lpClientContext->m_hSocket);
delete lpClientContext;
DebugPrint("用户已经关闭端口");
}
//--------------------------------------------------------
if(bIORet && lpOverlapped && lpClientContext)
{
pOverlapPlus=CONTAINING_RECORD(lpOverlapped, OVERLAPPEDPLUS, m_ol);
if(pOverlapPlus!=NULL)
pThis->ProcessIOMessage(pOverlapPlus, lpClientContext, dwIoSize);
}
//--------------------------------------------------------
if(lpClientContext==NULL && pOverlapPlus==NULL && pThis->m_bShutDown)
{
TRACE("lpClientContext==NULL /r/n");
bError=true;
}
//--------------------------------------------------------
if(bError==FALSE)
{
// 投递一个读操作
OVERLAPPEDPLUS * pOverlapPlus = new OVERLAPPEDPLUS(IORead);
ULONGulFlags = MSG_PARTIAL;

UINT nRetVal = WSARecv(lpClientContext->m_hSocket,
&lpClientContext->m_wsaInBuffer,
1,
&dwIoSize,
&ulFlags,
&pOverlapPlus->m_ol,
NULL);

if ( nRetVal == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING)
{
//pThis->RemoveStaleClient( lpClientContext, FALSE );
}
}
//--------------------------------------------------------
if(pOverlapPlus) delete pOverlapPlus;
//--------------------------------------------------------
}//while
TRACE("IOWorker Died!");
return 0xdead;
}

**************************************************
struct ClientContext
{
SOCKETm_hSocket;
DWORDm_dwID;
// Input Elements for Winsock
WSABUFm_wsaInBuffer;

// Output elements for Winsock
WSABUFm_wsaOutBuffer;


void NewInBuffer(DWORD nSize)
{
m_wsaInBuffer.buf = (CHAR*) new BYTE[nSize];
}

void NewOutBuffer(DWORD nSize)
{
m_wsaOutBuffer.buf = (CHAR*) new BYTE[nSize];
}

void FreeInBuffer()
{
//delete m_wsaInBuffer.buf;
m_wsaInBuffer.buf=NULL;
}

void FreeOutBuffer()
{
//delete m_wsaOutBuffer.buf;
m_wsaOutBuffer.buf=NULL;
}
};
************************************************************
void CIocp::OnAccept()
{
ClientContext* pClient=NULL;
SOCKETclientSocket=INVALID_SOCKET;

intnRet=-1;

clientSocket = accept(m_hListen,NULL,NULL);
ASSERT(clientSocket!=SOCKET_ERROR);

pClient = AllocateClient();
pClient->m_hSocket = clientSocket;
m_nIndex++;
pClient->m_dwID=m_nIndex;
pClient->NewInBuffer(1024);
........
}
*****************************************************************
void CIocp::ProcessIOMessage(OVERLAPPEDPLUS *pOverlapPlus, ClientContext* pClient, DWORD dwSize)
{
if(pOverlapPlus==NULL)
return;

switch (pOverlapPlus->m_ioType)
{
case IORead :
OnRead(pClient,dwSize);
break;
.......
}
}
***********************************************************

void CIocp::OnRead(ClientContext* pClient, DWORD dwSize)
{
if (dwSize == 0)
{
//RemoveStaleClient( pContext, FALSE );
return;
}
PostMessage(m_hWnd,WM_SOCKET_READ,pClient->m_dwID,(LPARAM)pClient->m_wsaInBuffer.buf);
}

***********************************************************************
wqrz 发表于 2006-4-18 5:42:20

和读操作有关的结构和函数基本都在上面了
我调试的时候投递一个读操作 可是OnRead的第2个参数dwSize老是0 导致无法收到任何信息
不知道是什么原因dwSize老是0呢?

现在这个类发送消息基本没问题了 就是接收信息接不到

如果上面代码还完整的话 谁有兴趣看一下整个工程文件 请留下邮箱~
gohappy_1999 发表于 2006-4-18 9:02:26

// 投递一个读操作
OVERLAPPEDPLUS * pOverlapPlus = new OVERLAPPEDPLUS(IORead);
ULONG ulFlags = MSG_PARTIAL;
/////////////////
memset(&pOverlapPlus->m_ol, 0, sizeof(pOverlapPlus->m_ol));
/////////////////
UINT nRetVal = WSARecv(lpClientContext->m_hSocket,
&lpClientContext->m_wsaInBuffer,
1,
&dwIoSize,
&ulFlags,
&pOverlapPlus->m_ol,
NULL);
wqrz 发表于 2006-4-18 9:13:27

不是楼上的XD讲的问题:
class OVERLAPPEDPLUS
{
public:
OVERLAPPEDm_ol;
IOTypem_ioType;

OVERLAPPEDPLUS(IOType ioType) {
ZeroMemory(this, sizeof(OVERLAPPEDPLUS));
m_ioType = ioType;
}
};
wqrz 发表于 2006-4-20 8:44:00
UP ~~~~~~~~~
gohappy_1999 发表于 2006-4-20 9:25:00
投递WSARecv后返回的错误码是多少?
wqrz 发表于 2006-4-20 9:41:00
WSARecv后应该没错误 因为WSARecv这句后的if条件不成立:

if ( nRetVal == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING)
{
//pThis->RemoveStaleClient( lpClientContext, FALSE );
}
wqrz 发表于 2006-4-20 11:32:00
搞定了 m_wsaInBuffer.len没赋值
快速回复
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

于完成端口的数据接收问题

DWORD   __stdcall       CompletionRoutine(LPVOID   Param)     {     CompletionPortModel*   pCP  ...

写文件系统时,内核在写操作的时候无法进行读操作

在内核中写代码总是很独特,读写操作可以拿到的变量的值都是不一样的,今天便遇到一个在读操作中很正常,在写操作中很奇葩的事情,就是获取inode的size的值。

TIFF文件读操作libtiff

  • 2011-07-26 13:14
  • 1.87MB
  • 下载

C# Excel文件的读操作

  • 2013-04-29 17:14
  • 52KB
  • 下载

s3c2440 nand 控制器(以对K9F2G08U0A 256M读操作为例)

2010-01-08 22:10:48|  分类: arm|字号 订阅 s3c2440 nand 控制器(以对K9F2G08U0A 256M读操作为例) (1)...

[转]怎么看时序图--nand flash的读操作详解

本文档摘自于:怎么看时序图--nand flash的读操作详解 http://www.51hei.com/bbs/dpj-27584-1.html (出处: 单片机论坛) 这篇文章不是介绍 nand...

怎么看时序图--nand flash的读操作详解

http://blog.chinaunix.net/uid-28852942-id-3992727.html 这篇文章不是介绍 nand flash的物理结构和关于nand flash的一些基本知识...

nand flash的读操作

韦老师的nand flash一章操作的flash与我开发板上的nand flash型号不同,做了一些修改。我的开发板上nand flash型号为K9F2G08,即2Gbit,8bit或者说是256M,...

C语言之文件读操作

C语言之文件读操作,通过输入输出流进行文件操作//  FileReadWrite.cpp   //  C语言文件读操作   //  Created by Coral on...

winform开发中对App.Config的写和读操作

using System; using System.Configuration; using System.Collections.Generic; using System.Text; using...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)