关于用API函数检测TCP连接的问题 ( 积分: 100 )

  • 主题发起人 主题发起人 wsdgs
  • 开始时间 开始时间
W

wsdgs

Unregistered / Unconfirmed
GUEST, unregistred user!
用API写的TCP ClientSocket程序,为什么总检测不到server端断开连接。
function TClientSocket.ReceiveBuffer(var Buffer; BufferSize: Integer): Integer;
var
GetLong: Integer;
ErrorCode: Integer;
begin
if BufferSize = -1 then begin
GetLong := ioctlsocket(FSocket, FIONREAD, Longint(Result));
if (GetLong < 0) then begin
ErrorCode := WSAGetLastError;
if (ErrorCode = WSAEWOULDBLOCK) then begin
Result := 0;
end
else begin
Result := SOCKET_ERROR;
Disconnect;
end;
end;
end
else begin
Result := recv(FSocket, Buffer, BufferSize, 0);
if Result < 0 then begin
ErrorCode := WSAGetLastError;
if (ErrorCode = WSAEWOULDBLOCK) then begin
Result := 0;
end
else begin
Result := SOCKET_ERROR;
Disconnect;
end;
end;
end;
end;
如果server断开,它只是接不到数据,却不能判断已经断开
 
用API写的TCP ClientSocket程序,为什么总检测不到server端断开连接。
function TClientSocket.ReceiveBuffer(var Buffer; BufferSize: Integer): Integer;
var
GetLong: Integer;
ErrorCode: Integer;
begin
if BufferSize = -1 then begin
GetLong := ioctlsocket(FSocket, FIONREAD, Longint(Result));
if (GetLong < 0) then begin
ErrorCode := WSAGetLastError;
if (ErrorCode = WSAEWOULDBLOCK) then begin
Result := 0;
end
else begin
Result := SOCKET_ERROR;
Disconnect;
end;
end;
end
else begin
Result := recv(FSocket, Buffer, BufferSize, 0);
if Result < 0 then begin
ErrorCode := WSAGetLastError;
if (ErrorCode = WSAEWOULDBLOCK) then begin
Result := 0;
end
else begin
Result := SOCKET_ERROR;
Disconnect;
end;
end;
end;
end;
如果server断开,它只是接不到数据,却不能判断已经断开
 
一般检测都是用Round Trip的,
 
我不是要超时关闭,我只要检测正常关闭
 
recv()返回错误就是断开,你用的是什么I/O模型?
 
我用的阻塞,帮忙顶啊!
 
接受答案了.
 
后退
顶部