tidtcpserver는 때때로 소켓에서 데이터를 읽지 않습니다
-
19-09-2019 - |
문제
이 사이트의 다른 예와 매우 유사한 TidtcPserver (Delphi 2009 및 Indy 10)의 OneXecute에 다음 코드가 있습니다.
Socket := AContext.Connection.Socket;
if Socket.CheckForDataOnSource(10) then
begin
if not Socket.InputBufferIsEmpty then
begin
Socket.InputBuffer.ExtractToBytes(RawBytes, -1, False, -1);
SetLength(Buffer, Length(RawBytes));
Move(RawBytes[0], Buffer[1], Length(RawBytes));
// Do stuff with data here...
end;
end;
AContext.Connection.CheckForGracefulDisconnect;
checkfordataonsource (10)가 false를 반환하기 때문에 때때로 데이터를 읽지 않습니다. 그러나 해당 라인에서 디버거를 중지하면 입력 부퍼의 바이트에서 보낸 데이터를 볼 수 있습니다. 내가해야 할 다른 설정 일이나 항상이를 강요하는 다른 방법이 있습니까? 이 코드는 여러 번 실행되지만 항상 CheckfordataOnsource (10)에서 실패합니다.
또한 부수적으로, 나는 일부 사람들이 acontext.connection.socket.socket 대신 acontext.connection.iohandler를 잡고 위의 코드와 동일한 작업을 수행하는 곳에서 인디 코드를 알 수 있습니다. 사용.
감사
브루스
해결책
코드는 다음과 같아야합니다.
var
IO: TIdIOHandler.
Buffer: RawByteString;
begin
IO := AContext.Connection.IOHandler;
if IO.InputBufferIsEmpty then
begin
IO.CheckForDataOnSource(10);
if IO.InputBufferIsEmpty then Exit;
end;
IO.InputBuffer.ExtractToBytes(RawBytes, -1, False, -1);
// or: IO.ReadBytes(RawBytes, -1, False);
SetLength(Buffer, Length(RawBytes));
BytesToRaw(RawBytes, Buffer[1], Length(RawBytes));
// Do stuff with Buffer here...
end;
다른 팁
코드가 다음과 같이 보일 것 같습니다.
Socket := AContext.Connection.Socket;
Socket.CheckForDataOnSource(10);
if not Socket.InputBufferIsEmpty then
begin
Socket.InputBuffer.ExtractToBytes(RawBytes, -1, False, -1);
SetLength(Buffer, Length(RawBytes));
Move(RawBytes[0], Buffer[1], Length(RawBytes));
// Do stuff with data here...
end;
AContext.Connection.CheckForGracefulDisconnect;
당신이 어떤 iohandler를 잡는지는 중요하지 않으므로 일반적인 것은 이동처럼 보입니다.
내 자신의 질문에 대답하는 것에 대해 죄송하지만 누군가에게는 간절할 수 있습니다 ... 아마도.
제휴하지 않습니다 StackOverflow