Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Receive a Line Terminated by CRLF

See more Socket/SSL/TLS Examples

Demonstrates Socket.ReceiveToCRLF, which receives text through a CRLF line terminator and returns the text including the terminator.

Background. Chilkat encodes CR followed by LF using StringCharset and searches the incoming byte stream for that exact sequence. This is convenient for line-oriented text protocols.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Socket;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  socket: TSocket;
  bTls: Boolean;
  maxWaitMs: Integer;
  command: string;
  responseLine: string;

begin
  success := False;

  socket := TSocket.Create;

  //  Connect to the server using TLS.  The 3rd argument enables the TLS handshake after the TCP
  //  connection succeeds; the 4th is the maximum time to wait, in milliseconds.
  bTls := True;
  maxWaitMs := 5000;
  success := socket.Connect('example.com',5000,bTls,maxWaitMs);
  if (success = False) then
    begin
      WriteLn(socket.LastErrorText);
      Exit;
    end;

  //  StringCharset controls the character encoding used when sending and receiving text.
  socket.StringCharset := 'utf-8';
  //  Send a request using a simple application-defined text protocol in which each message ends with a
  //  CRLF.
  command := 'STATUS' + #13#10;
  success := socket.SendString(command);
  if (success = False) then
    begin
      WriteLn(socket.LastErrorText);
      Exit;
    end;

  //  Receive text through a CRLF line terminator, returning the text including the terminator.  This is
  //  convenient for line-oriented text protocols in which each response is a CRLF-terminated line.
  responseLine := socket.ReceiveToCRLF();
  if (socket.LastMethodSuccess = False) then
    begin
      WriteLn(socket.LastErrorText);
      Exit;
    end;
  WriteLn(responseLine);


  socket.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.