Delphi IdTCPClient[2] Write、WriteLn、ReadLn - 字符串的写入和接收

Delphi IdTCPClient[2] Write、WriteLn、ReadLn  - 字符串的写入和接收

1、Write  //使用连接发送字符串

procedure TIdTCPConnection.Write(const AOut: string);
var
  LOutLen: Integer;
Begin
  LOutLen := Length(AOut);
  if LOutLen > 0 then begin
    WriteBuffer(Pointer(AOut)^, LOutLen);    
  end;
End;//Write
  • Write是用于通过连接发送AOut中指定的字符串的过程。当AOut为空字符串时,不执行任何操作。
  • Write调用WriteBuffer以从缓冲区执行输出。
  • Write不会向输出缓冲区添加行尾序列。

2、WriteLn   //将字符串值写入对等连接。

procedure TIdTCPConnection.WriteLn(const AOut: string = '');
begin
  Write(AOut + EOL);
end;

WriteLn是用于将AOut中指定的字符串值发送到对等连接的过程。WriteLn在发送到对等连接时,将EOL字符序列附加到AOut中的值。

  CHAR0 = #0;
  BACKSPACE = #8;
  LF = #10;
  CR = #13;
  EOL = CR + LF;
  TAB = #9;
  CHAR32 = #32;

与Write的区别可以简单理解为,WriteLn 是带了回车换行的信息  -- 滔Roy  2022.11.21

3、ReadLn   //从Indy缓冲区读取一行字符串。

function TIdTCPConnection.ReadLn(ATerminator: string = LF;
 const ATimeout: Integer = IdTimeoutDefault; AMaxLineLength: Integer = -1): string;
var
  LInputBufferSize: Integer;
  LSize: Integer;
  LTermPos: Integer;
begin
  if AMaxLineLength = -1 then begin
    AMaxLineLength := MaxLineLength;
  end;
  // User may pass '' if they need to pass arguments beyond the first.
  if Length(ATerminator) = 0 then begin
    ATerminator := LF;
  end;
  FReadLnSplit := False;
  FReadLnTimedOut := False;
  LTermPos := 0;
  LSize := 0;
  repeat
    LInputBufferSize := InputBuffer.Size;
    if LInputBufferSize > 0 then begin
      LTermPos :=
        MemoryPos(ATerminator, PChar(InputBuffer.Memory) + LSize, LInputBufferSize - LSize);
      if LTermPos > 0 then begin
        LTermPos := LTermPos + LSize;
      end;
      LSize := LInputBufferSize;
    end;//if
    if (LTermPos - 1 > AMaxLineLength) and (AMaxLineLength <> 0) then begin
      if MaxLineAction = maException then begin
        raise EIdReadLnMaxLineLengthExceeded.Create(RSReadLnMaxLineLengthExceeded);
      end else begin
        FReadLnSplit := True;
        Result := InputBuffer.Extract(AMaxLineLength);
        Exit;
      end;
    // ReadFromStack blocks - do not call unless we need to
    end else if LTermPos = 0 then begin
      if (LSize > AMaxLineLength) and (AMaxLineLength <> 0) then begin
        if MaxLineAction = maException then begin
          raise EIdReadLnMaxLineLengthExceeded.Create(RSReadLnMaxLineLengthExceeded);
        end else begin
          FReadLnSplit := True;
          Result := InputBuffer.Extract(AMaxLineLength);
          Exit;
        end;
      end;
      // ReadLn needs to call this as data may exist in the buffer, but no EOL yet disconnected
      CheckForDisconnect(True, True);
      // Can only return 0 if error or timeout
      FReadLnTimedOut := ReadFromStack(True, ATimeout, ATimeout = IdTimeoutDefault) = 0;
      if ReadLnTimedout then begin
        Result := '';
        Exit;
      end;
    end;
  until LTermPos > 0;
  // Extract actual data
  Result := InputBuffer.Extract(LTermPos + Length(ATerminator) - 1);
  // Strip terminators
  LTermPos := Length(Result) - Length(ATerminator);
  if (ATerminator = LF) and (LTermPos > 0) and (Result[LTermPos] = CR) then begin
    SetLength(Result, LTermPos - 1);
  end else begin
    SetLength(Result, LTermPos);
  end;
end;//ReadLn

参数:

  • ATerminator  //表示特定协议的行尾符号。如果需要数据,并且不需要自定义的行尾符号,则可以使用默认的ATerminator值(#0)。A终端值包括ATimeout  //超时(毫秒)。默认值IdTimeoutInfinite。
    • #0-默认换行符(#10)
    • LF-默认换行符(#10)
    • CR-回车(#13)
    • EOL-行尾(回车+换行)

其他:如果缓冲区中没有终止符(ATerminator),ReadLn将通过ReadFromStack方法从TCP堆栈中读取更多字节。使用自定义超时(ATimeout)值更改超时发生前等待对等连接响应的毫秒数。ATimeout的默认值为IdTimeoutInfinite。如果对等连接过早关闭或发生超时,ReadLn将返回空字符串(“”)。当ReadLn中出现超时条件时,ReadLnTimedOut属性设置为True。

 

 

 

 

创建时间:2022.11.21  更新时间:2022.11.21

posted on 2022-11-21 11:05  滔Roy  阅读(923)  评论(0编辑  收藏  举报

导航