Client and Server socket with Indy

Asked

Viewed 434 times

1

Dear, I am trying to create a program that will receive a list of products sold through a Socket connection. I can connect via Tcpclient, but I’m not able to send the list of commands, nor receive the result that comes from the server. In my test, I can get one line, but I need to receive several lines. I’m doing like this:

procedure TForm1.Button4Click(Sender: TObject);
var
  LLine : string;
begin
  try
    if not IdTCPClient1.Connected then
      IdTCPClient1.Connect;

    IdTCPClient1.SendCmd(Edit3.Text);

    //LLine := IdTCPClient1.IOHandler.ReadLn();
    Lline := IdTCPClient1.Socket.ReadString(6000);
    mmLog.Lines.Add(LLine);

  except
  on E:Exception do
      if ( E.Message <> 'Connection Closed Gracefully.') then
        mmLog.Lines.Insert(0, 'ERROR: ' + E.Message);
  end;
end;

I’m wearing Indy 10 and Delphi Tokyo.

1 answer

0

I prefer to use the standard socket component, but to TIndy I’ve used it as follows:

Considering that the server writes line by line, just inform in the first line the number of lines being sent.

procedure TFormulario.IdServidor1Execute(AContext: TIdContext);
begin
  AContext.Connection.IOHandler.WriteLn('3');
  AContext.Connection.IOHandler.WriteLn('MENSAGEM 1');
  AContext.Connection.IOHandler.WriteLn('MENSAGEM 2');
  AContext.Connection.IOHandler.WriteLn('MENSAGEM 3');
end;

Now imagine that in the customer you already have the solution. That is, instead of using ReadString use the ReadStrings that will return a mailing list:

var
  i: Integer;
  vTexto: TStringList;
  vTamanho: Integer;
begin
  vTexto := TStringList.Create;

  vTamanho := IdTCPClient1.IOHandler.ReadLn.ToInteger;
  IdTCPClient1.IOHandler.ReadStrings(vTexto, vTamanho);

  for i := 0 to Pred(vTexto.Count) do
    Memo1.Lines.Add(vTexto[i]);
...

In this case, there is a small modification on the Server side so that it always informs the number of messages it is sending in the package.

  • I understood the idea... The problem is that it freezes when it calls the line: vTamanho := Idtcpclient1.IOHandler.Readln.Tointeger;

  • What is sending in AContext.Connection.IOHandler.WriteLn('3'); ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.