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断开,它只是接不到数据,却不能判断已经断开
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断开,它只是接不到数据,却不能判断已经断开