Recent

Author Topic: TCP With Indy  (Read 15297 times)

facom4ever

  • New Member
  • *
  • Posts: 15
TCP With Indy
« on: February 09, 2012, 05:22:13 pm »
Hi boys & girls,

I am using Indy IdTCPServer & IdTCPClient sockets.

I try sending text from Client to Server like this :

  with IdTCPClient do
  begin
    Connect;
    IOHandler.WriteLn('TRANSFERTFILE');
    IOHandler.WriteLn('test.txt');
    IOHandler.WriteLn('content');
    Disconnect;
  end;

And onExecute even server side I try to read the messages and create a file with content :

  with AContext.Connection do
  begin
    Test := IOHandler.ReadLn;

    if SameText(Test, 'TRANSFERTFILE') then
    begin
      Test := IOHandler.ReadLn;
      AssignFile(MyFile, Test);
      ReWrite(MyFile);
      Test := IOHandler.ReadLn;
      WriteLn(MyFile, Test);
      CloseFile(MyFile);
    end;

    Disconnect;
  end;   


The problem is that the server reads TRANSFERTFILE, and that is all. It doesn't try to read the next lines, and strange it doesnt create my file even calling it TRANSFERTFILE.

Isn't that weird ? Or am I missing something ?

Thanks mates

everton

  • Jr. Member
  • **
  • Posts: 89
Re: TCP With Indy
« Reply #1 on: February 09, 2012, 06:22:52 pm »
Probably is getting out in that if.

Try to do Trim in the response.

facom4ever

  • New Member
  • *
  • Posts: 15
Re: TCP With Indy
« Reply #2 on: February 10, 2012, 08:38:31 am »
It seems that it enters in the if...

But Test variable is always equald transfertfile even after 2nd readln

JD

  • Hero Member
  • *****
  • Posts: 1913
Re: TCP With Indy
« Reply #3 on: February 10, 2012, 10:32:29 am »
Hi there.

You're just sending strings from the client to the server.

Suggest that you read http://delphi.about.com/od/indy/Internet_Direct_Indy_knowledge_base.htm to get an overview of how to use Indy
Linux Mint - Lazarus 4.6/FPC 3.2.2,
Windows - Lazarus 4.6/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

facom4ever

  • New Member
  • *
  • Posts: 15
Re: TCP With Indy
« Reply #4 on: February 10, 2012, 02:06:52 pm »
I don't know, the way it works is a bit strange.

The client send (it sends one time each, checked with tcpdump)
writeln(TRANSFERTFILE)
writeln(dr.txt)
writeln(contenu)

The server onexecute event reads the data and puts in a memo. Here are the results...
TRANSFERTFILE
TRANSFERTFILE
dr.txt
TRANSFERTFILE
dr.txt
contenu
TRANSFERTFILE
dr.txt
contenu
TRANSFERTFILE
dr.txt
contenu
TRANSFERTFILE
dr.txt
contenu

Why it doesn't simple get ?
TRANSFERTFILE
dr.txt
contenu
« Last Edit: February 10, 2012, 03:25:42 pm by facom4ever »

facom4ever

  • New Member
  • *
  • Posts: 15
Re: TCP With Indy
« Reply #5 on: February 13, 2012, 10:00:58 am »
Any idea :(

Now it reads twice each message...
« Last Edit: February 13, 2012, 12:13:41 pm by facom4ever »

facom4ever

  • New Member
  • *
  • Posts: 15
Re: TCP With Indy
« Reply #6 on: February 13, 2012, 11:32:00 am »
Honestly I don't understand.

I just want to exchange some simple text, why is it so complicated :( ? On windows with delphi it was so easy, even on very old versions.


From client:
procedure TForm1.Button3Click(Sender: TObject);
begin
  IdTCPClient1.Host := Edit1.Text;
  IdTCPClient1.Connect;
  IdTCPClient1.IOHandler.WriteLn('TRANSFERTFILE');
  IdTCPClient1.IOHandler.WriteLn('dr.txt');
  IdTCPClient1.IOHandler.WriteLn('contenu');
  IdTCPClient1.Disconnect;
end;

On the server:
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
  Cmd : String;
begin
  with AContext.Connection.IOHandler do
  begin
    Test := ReadLn;
    Cmd := Fetch(Test);

    if Cmd = 'TRANSFERTFILE' then
    begin
      Test := ReadLn;
      Memo1.Lines.Add(Fetch(Test));
      Test := ReadLn;
      Memo1.Lines.Add(Fetch(Test));
    end;
  end;
  AContext.Connection.Disconnect;
end;         

And I get this :
dr.txt
contenu
dr.txt
contenu

It is not logical I think
« Last Edit: February 13, 2012, 12:17:05 pm by facom4ever »

joseme

  • Full Member
  • ***
  • Posts: 128
    • Logosoft sistemas
Re: TCP With Indy
« Reply #7 on: February 13, 2012, 12:21:06 pm »
May be this can help you.
Code: [Select]
function DownloadFile(OrigFile, DestFile: String): Boolean;
var f:tfilestream;
  IdHTTP1: TIdHTTP;
begin
  IdHTTP1 := TIdHTTP.Create(nil);
  f:=tfilestream.create(destFile, fmcreate);
  try
    idhttp1.head(origFile);   // To know if the file already exists
    idhttp1.get(origFile, f); //if file exists, download it
  except
    ShowMessage('El archivo '+origFile+' does not exist or is not downloadable');
    Result := False;
  end;
    freeandnil(f);
    IdHTTP1.Destroy;
end;

function SendFile(OrigFile, DestFile: String): Boolean;
var
  IdFTP1: TIdFTP;
begin
  Result := False;
  IdFTP1 := TIdFTP.Create;
  IdFTP1.Username:= 'name' ;
  IdFTP1.Password:= 'pass';
  IdFTP1.Host:= 'ftp.abcd.com';
//  IdFTP1.Passive:= True;
  try
    IdFTP1.Connect;
    IdFTP1.Put(OrigFile, destFile, False);
    Result := True;
  finally
    IdFTP1.Quit;
    IdFTP1.Destroy;
  end;
end;
un aporte a la comunidad:
http://pascalylazarus.blogspot.com/

facom4ever

  • New Member
  • *
  • Posts: 15
Re: TCP With Indy
« Reply #8 on: February 13, 2012, 04:12:28 pm »
Thank you :) at least I may send files....

But I still don't get why I receive twice all messages from the client that sends only one time  >:(.

That's absolutly insane. It's like there was two threads from de same client.

facom4ever

  • New Member
  • *
  • Posts: 15
Re: TCP With Indy
« Reply #9 on: February 14, 2012, 09:58:31 am »
I think I will simply give up doing networking applications with Lazarus on Mac :(

So many time juste to fail to figure out how to send text messages betwin 2 terminals looks S.F.

I don't understand why on Windows with VisualStudio or Delphi it was so easy, and now on MAc with Lazarus it looks impossible and unreliable.

Thanks anyway for the help.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TCP With Indy
« Reply #10 on: February 14, 2012, 01:29:50 pm »
http://wiki.lazarus.freepascal.org/Indy_with_Lazarus

Quote
What works, what not
Indy9:
Windows should largely work. Lots of demoes ported.
Linux not or barely. No hope at non Linux/x86 (non-Kylix) targets.
Indy10:
Windows and Unix: clients work and servers should work fine in principle.
The main work for native Unix RTL done. Servers Clients seem to work with FreeBSD and OS X

I might be interested at the Indy 10. If the use code really is that simple, it is million years ahead of Synapse that i have trouble with.

facom4ever

  • New Member
  • *
  • Posts: 15
Re: TCP With Indy
« Reply #11 on: February 15, 2012, 10:38:59 am »
Have you tried Indy10 ?

The installation at least works on Mac 10.6.8

 

TinyPortal © 2005-2018