Link to home
Start Free TrialLog in
Avatar of Allan_Fernandes
Allan_Fernandes

asked on

IdTCPClient and IdTCPServer exchanging data on desktops and not Android

In below code I am using IdTCPClient and IdTCPServer.
This works fine from one desktop to another.

When Android Mobile is Client and Desktop is Server it does not transfer any data
  even though the connection takes place.

// Client side

procedure TForm9.Button1Click(Sender: TObject);
var
  MIRec: TSendRec;
  ms: TMemoryStream;
begin

  MIRec.SONo := '' ;
  MIRec.Text := 'Hello World';

  MIRec.FlStream := TFileStream.Create(System.IOUtils.TPath.GetDocumentsPath+sysPathDelim+'Source.txt', fmOpenRead );
  try
    IdTCPClient1.Connect;
    try
      IdTCPClient1.IOHandler.WriteLn(MIRec.SONo);
      IdTCPClient1.IOHandler.WriteLn(MIRec.Text);
      try
        IdTCPClient1.IOHandler.LargeStream := True;
        IdTCPClient1.IOHandler.Write(MIRec.FlStream, 0, True);
      finally
      end;
    finally
      IdTCPClient1.Disconnect;
    end;
  finally
    MIRec.FlStream.Free;
  end;

end;

Open in new window

//Server side

procedure TForm9.IdTCPServer1Execute(AContext: TIdContext);
var
  MIRec: TSendRec;
begin
  MIRec.SONo := AContext.Connection.IOHandler.ReadLn;
  MIRec.Text := AContext.Connection.IOHandler.ReadLn;
  MIRec.FlStream := TFileStream.Create('C:\Dest.txt', fmCreate);
  try
    try
      AContext.Connection.IOHandler.LargeStream := True;
      AContext.Connection.IOHandler.ReadStream( MIRec.FlStream, -1, False);
    finally
    end;
    TThread.Synchronize(nil,
      procedure
      begin
        Memo1.Lines.Add(MIRec.SONo);
        Memo1.Lines.Add(MIRec.Text);
      end);
  finally
    MIRec.FlStream.Free;
  end;
end;

Open in new window


using Seattle
I have set Permissions 'Internet' and 'Access network state' On
ASKER CERTIFIED SOLUTION
Avatar of Jackie Man
Jackie Man
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You stick with Indy.... Please try using Delphi native THTTPClient /server components. Daniele Teti show some examples in a Delphi Cookbook...
Avatar of Allan_Fernandes
Allan_Fernandes

ASKER

That is bad news that Sockets are not permitted in Android 6 above. I want my application scalable.
Is Ftp also depricated ?

I was avoiding Indy. I have been using basic TServerSocket/TClientSocket until now but that don't work in Firemonkey so am forced to use Indy. Anyways I got the problem sorted out by making LargeStream = False. There was some issue with older version of Indy's Android Int64 handling on Android. Courtesy (Remy Lebeau)

I saw the example in the Delphi Cookbook using THTTP components. How convenient would it be to send binary data files across with this system. I have noticed that HTTP makes changes for catering to JSON strings.
Is there any other option, I just want to have the freedom to send data like I do across two desktops with sockets, wherever they are across the globe.
Indy is fully supported on Android, and especially on Android 6 (not counting OpenSSL, which is a separate issue).  The problem with TStream transmissions is due to a logic bug in how Indy's handles the transmission of Int64 integers, which affects the TIdIOHandler.Write(TStream) and TIdIOHandler.ReadStream() methods when LargeStream is True.  That bug was fixed a week ago, so make sure you are using an up-to-date version of Indy.
Respected Remy, your advice is the final truth for me. I am not countering what you said.

My confusion started when Jackie informed that 'this kind of file transfer has been stopped' it was not meant to be with Indy components surely. I understood it as, basic Socket transfers have been stopped and I have to move onto higher HTTP protocols. I currently am working on Android 4 therefore could not test with 6.

I am most happy to continue with the Indy Sockets as they give me total flexibility to send Binary data at fasted speeds and without any overheads.
Allan,

"If Android 6 or above, it is not supported to do this kind of file transfer" is the completely wrong answer to accept.  I don't know if Experts Exchange allows you to change your accepted answer or not, though.

And also, "I understood it as, basic Socket transfers have been stopped and I have to move onto higher HTTP protocols" is also completely wrong.

You are getting bad information.
Only a system app can have permission in Android 6 or above and the same app should have no problem in Android 5 or 4.

https://forums.embarcadero.com/thread.jspa?messageID=771791
Jackie,

"Only a system app can have permission in Android 6 or above" - that is completely wrong.  Non-system apps have access to TCP, they only require INTERNET permission.  Indy works just fine on Android 6 and above.

As for the link you posted, that is an earlier discussion describing a similar TStream send/receive crash before the bug THAT HAS ALREADY BEEN FIXED was identified.  The bug is very isolated (sending/receiving Int64 integers), and is NOT a deterrent from using Indy (or TCP) in general on Android 6 and later.

Please, people, get your facts straight before posting mis-information!
Thanks Remy for Stepping in else I would have scrapped the plan of working which suited me best and that I was most comfortable with.
If the data to be transferred is stored inside the container of your own app, but not elsewhere, you won't run into permission problem.
Thanks for the Tip Jackie. Will do that.