Socket Error #0

Giganews Newsgroups
Subject: Socket Error #0
Posted by:  Ron (ron@nospam.simdata.com.au)
Date: Fri, 28 May 2004

I have discovered an issue with Indy 9.0.14 where you can end up with an
socket error of 0.  I have attached my fix with a comment that describes the
problem.

Ron

procedure TIdTCPConnection.WriteBuffer(const ABuffer; AByteCount: Integer;
const AWriteNow: boolean = false);
var
  LBuffer: TIdSimpleBuffer;
  nPos, nByteCount: Integer;
begin
  if (AByteCount > 0) and (@ABuffer <> nil) then begin
    // Check if disconnected
    CheckForDisconnect(True, True);
    if connected then begin
      if (FWriteBuffer = nil) or AWriteNow then begin
        LBuffer := TIdSimpleBuffer.Create; try
          LBuffer.WriteBuffer(ABuffer, AByteCount);
          if Assigned(Intercept) then begin
            LBuffer.Position := 0;
            Intercept.Send(LBuffer);
            AByteCount := LBuffer.Size;
          end;
          nPos := 1;
          repeat
//RWB: move Antifreeze call before Send()  because the user program  may
have message handlers that will reset
//    the Stack error and you end up with a Socket Error #0
//
            TIdAntiFreezeBase.DoProcess(False);

            nByteCount := IOHandler.Send(PChar(LBuffer.Memory)[nPos - 1],
LBuffer.Size - nPos + 1);
            // Write always does someting - never retuns 0
            // TODO - Have a AntiFreeze param which allows the send to be
split up so that process
            // can be called more. Maybe a prop of the connection,
MaxSendSize?
//RWB dont do after Send:    TIdAntiFreezeBase.DoProcess(False);
            FClosedGracefully := nByteCount = 0;
            // Check if other side disconnected
            CheckForDisconnect;

Replies