WinSock2 的问题,异步 WSARecv 的问题,(50分)

  • 主题发起人 主题发起人 QSmile
  • 开始时间 开始时间
Q

QSmile

Unregistered / Unconfirmed
GUEST, unregistred user!
代码如下:

function TSimpleSocket.ReadBuffer(p: Pointer; BufLen: Integer;dwTimeOut:DWORD): Integer;
var
ov_read : TOverlapped;
Err:DWORD;
nResult:DWORD;
dwFlag:DWORD;
WSBuf:TWSABUF;
dwLen:DWORD;
begin
//WSASend(m_hSocket,p,Len,@Len,0,@ov_read,nil);
Result := 0;
FillChar(ov_read,SizeOf(TOverlapped),#0);
ov_read.hEvent := WSACreateEvent;
WSBuf.len := BufLen;
WSBuf.buf := p;
dwFlag := 0;

nResult := WSARecv(m_hSocket,
@WSBuf,
1,
@dwLen,
@dwFlag,
@ov_Read,
nil);

if (nResult = SOCKET_ERROR) then
begin
Err := WSAGetLastError;
if (Err = WSA_IO_PENDING) then
begin
//WSAGetOverlappedResult(m_hSocket,@ov_read,@dwLen,True,@dwFlag);
Err := WaitForSingleObject(ov_read.hEvent,dwTimeOut);
if Err = WAIT_OBJECT_0 then
begin
Result := dwLen;
end;
end;
end;
Result := dwLen;
WSACloseEvent(ov_read.hEvent);
end;

数据能读出来。但 dwLen (读取的字节数) 还是 0 呢?
 
不会吧,我用完成例程的时候没发现这种问题呀 !
我试一下
 
问题解决了,不过很奇怪。

是与 Help 不同,还是我没认真看?
 
接受答案了.
 
后退
顶部