Download source code - Delphi and C++Builder

Client:

procedure TForm1.Button1Click(Sender: TObject);
var
  wsaData: TWSAData;
  client: TclTcpClientConnection;
  data: TStringStream;
begin
  WSAStartup($202, wsaData);
  client := nil;
  data := nil;
  try
    client := TclTcpClientConnection.Create();
 
    client.NetworkStream := TclNetworkStream.Create();
    client.TimeOut := 60000;
    client.Open('127.0.0.1', 2110);
    ShowMessage('Client connected');
 
    data := TStringStream.Create('Data to be sent');
    client.WriteData(data);
    data.Size := 0;
    client.IsReadUntilClose := True;
    client.ReadData(data);
 
    ShowMessage(data.DataString);
 
    client.Close(True);
    ShowMessage('Client disconnected');
  finally
    data.Free();
    client.Free();
    WSACleanup();
  end;
end;

Server:

procedure TForm1.Button1Click(Sender: TObject);
var
  wsaData: TWSAData;
  server: TclTcpServerConnection;
  data: TStream;
begin
  WSAStartup($202, wsaData);
  server := nil;
  data := nil;
  try
    server := TclTcpServerConnection.Create();
 
    server.NetworkStream := TclNetworkStream.Create();
    server.TimeOut := 60000;
    server.BatchSize := 8192;
    server.Open(2110);
    server.Accept();
    Memo1.Lines.Add('connected');
 
    data := TStringStream.Create('');
    server.ReadData(data);
    data.Position := 0;
    server.WriteData(data);
 
    Memo1.Lines.Add('read done');
 
    server.Close(False);
    Memo1.Lines.Add('closed');
  finally
    data.Free();
    server.Free();
    WSACleanup();
  end;
end;

Article ID: 68, Created On: 7/18/2013, Modified: 7/18/2013