Jump to content
Alex Joseph

Fetch server IP in android client app

Recommended Posts

I have firedac datasnap server connected to mssql database server. I am using dbx connection from my firemonkey client app that is running on android mobile.

Since I have different datasnap servers using the same App, how is it possible to acquire the IP address of the datasnap server with a parameter from client side.

Can someone help me on this issue.

Share this post


Link to post
Guest

you can implement a property in your App Server to, when one connection is opened, you catch the ip from database server.

or ... in you express-connection you dont have It?

how you app find the DataSnap server...address?

for example, in Firebird we have  System table to public access and it's possible look the table when stay the connection details-info.

MSSQL have it? then, a "Select" do the magic!

Share this post


Link to post

Hope this attached image will help you to understand my connection params.

Our servers are all stand alone.

Thank you

IMG_2481.JPG

Share this post


Link to post
Guest
Posted (edited)

hi @Alex Joseph

you can use "PrintScreen" key for "take a shotscreen" is better than take a picture like this, ok. later, just "paste it" here on post ... the upload is automatic.

 

let's try help you for sure!

 

Edited by Guest

Share this post


Link to post
Guest
Posted (edited)

maybe help you:

  • When your DSServer is connected, you can use "OnConnect" event to "catch" some info about this connection.
    • here, you can verify another infos, like you DB infos, etc...
    • you can use other events in your components in this unit!
  • Then, you can create a method on your "ServerMethods" unit to send this infos to user for example.

 

image.thumb.png.3d69fcb00b19137b650f61e76a3c24ed.png

 

// unit ServerContainerUnit1;
...
procedure TServerContainer1.DSServer1Connect(DSConnectEventObject: TDSConnectEventObject);
begin
	// lMyIP_DSServer is a var exported on "unit ServerMethodsUnit1"
  ServerMethodsUnit1.lMyIP_DSServer := DSConnectEventObject.ChannelInfo.Info; // see on Help about "TDBXChannelInfo" to know what info is contained here
end;

 

// unit ServerMethodsUnit1;
...
var
  lMyIP_DSServer: string;

implementation
...

function TServerMethods1.fncMyIpOnDSServer: string;
begin
  Result := lMyIP_DSServer;
end;

 

//unit uFormClient;
...
procedure TfrmFormClient.Button1Click(Sender: TObject);
var
  lSMC: TServerMethods1Client;
begin
  lSMC := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
  try
    SQLConnection1.GetServerMethodNames(ListBox1.Items);
    //
    ShowMessage(                                                  { }
      'Calling my "fncMyIpOnDSServer" function on DS Server = ' + { }
      lSMC.fncMyIpOnDSServer                                      { }
      );
  finally
    lSMC.Free;
  end;
end;

 

hug

Edited by Guest

Share this post


Link to post

Hi,

This is working fine. But It is giving the Android device IP.  I think I have to save the IP address of DatasnapServer 

in a file     SERVERIP.INI   in the device itself. 

 

From client side I should be able to read  the IP address from  SERVERIP.INI file  and  pass

it to a string variable Vserverip

 

SqlConnection1Beforeconnect(Sender: Tobject);

begin

 sqlconnection1.params.values['HostName'] :=Vserverip;

end

 

Read /Write INI files from android client side should solve my problem.

Sorry for poor english.

 

 

 

 

 

Share this post


Link to post
Guest

This should give the IP from Server, or not? (where is running the App Server...)

This is not your DS Server unit?

// unit ServerContainerUnit1;
...
procedure TServerContainer1.DSServer1Connect(DSConnectEventObject: TDSConnectEventObject);
begin
	// lMyIP_DSServer is a var exported on "unit ServerMethodsUnit1"
  ServerMethodsUnit1.lMyIP_DSServer := DSConnectEventObject.ChannelInfo.Info; // see on Help about "TDBXChannelInfo" to know what info is contained here
end;

 

-------------------------------------

This is not your Client App on Android?

//unit uFormClient;
...
procedure TfrmFormClient.Button1Click(Sender: TObject);
var
  lSMC: TServerMethods1Client;
begin
  lSMC := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
  try
    SQLConnection1.GetServerMethodNames(ListBox1.Items);
    //
    ShowMessage(                                                  { }
      'Calling my "fncMyIpOnDSServer" function on DS Server = ' + { }
      lSMC.fncMyIpOnDSServer                                      { }
      );
  finally
    lSMC.Free;
  end;
end;

 

hug

Share this post


Link to post

DS Server Unit is correct. My Datasnap server is running on windows 2012 server. Server IP is 192.168.103.57

My client App is on Zebra Android Mobile device.

Please see the files attached. Hope this will  help you.

 

AndClient.rar

DSServer.rar

Share this post


Link to post
Guest
Posted (edited)

try this change:

 

image.thumb.png.bf877e8a2fc131e956de2594b11f1780.png

 

 

my DS ServerContainer unit (Server project):

unit ServerContainerUnit1;

interface

uses
  System.SysUtils,
  System.Classes,
  Datasnap.DSTCPServerTransport,
  Datasnap.DSServer,
  Datasnap.DSCommonServer,
  IPPeerServer,
  IPPeerAPI,
  Datasnap.DSAuth;

type
  TServerContainer1 = class(TDataModule)
    DSServer1: TDSServer;
    DSTCPServerTransport1: TDSTCPServerTransport;
    DSServerClass1: TDSServerClass;
    procedure DSServerClass1GetClass(DSServerClass: TDSServerClass; var PersistentClass: TPersistentClass);
    procedure DSServer1Connect(DSConnectEventObject: TDSConnectEventObject);
  private
    { Private declarations }
  public
  end;

var
  ServerContainer1: TServerContainer1;

implementation

{$R *.dfm}

uses
  ServerMethodsUnit1;

procedure TServerContainer1.DSServer1Connect(DSConnectEventObject: TDSConnectEventObject);
begin
  // ServerMethodsUnit1.lMyIP_DSServer := DSConnectEventObject.ChannelInfo.Info;
  //ServerMethodsUnit1.lMyIP_DSServer := DSConnectEventObject.ConnectProperties.Properties.Text;
  ServerMethodsUnit1.lMyIP_DSServer := DSConnectEventObject.ConnectProperties.Properties.Values['HostName'];  // use "THIS"
end;

procedure TServerContainer1.DSServerClass1GetClass(DSServerClass: TDSServerClass; var PersistentClass: TPersistentClass);
begin
  PersistentClass := ServerMethodsUnit1.TServerMethods1;
end;

end.

 

my FormMain Client unit (Client DS)

procedure TfrmFormClient.FormCreate(Sender: TObject);
begin
  SQLConnection1.Connected := false;
  // my params values for tests
  SQLConnection1.Params.Values['HostName'] := '192.168.43.171'; // my MSWindows hosts where run my DS Server app
  SQLConnection1.Params.Values['Port'] := '5000';
  //
  SQLConnection1.Connected := true;
end;

procedure TfrmFormClient.Button1Click(Sender: TObject);
var
  lSMC: TServerMethods1Client;
begin
  lSMC := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
  try
    SQLConnection1.GetServerMethodNames(ListBox1.Items);
    SQLConnection1.GetCommandTypes(ListBox2.Items);

    //
    ShowMessage(                                                  { }
      'Calling my "fncMyIpOnDSServer" function on DS Server = ' + { }
      lSMC.fncMyIpOnDSServer                                      { }
      );
  finally
    lSMC.Free;
  end;
end;

 

hug

Edited by Guest

Share this post


Link to post

hi,

I tried , but was not solving the problem.

I made a small unit to save the IP Address in a txt file

and on datamodulebefore create event

load the ip from txt file.

Thank you very much for helping me.

 

procedure TFrmParam.BtnLoadClick(Sender: TObject);
var
   TextFile : TStringList;
   FileName : string;
begin
  try
      textFile := TStringList.Create;
      try
        FileName := Format('%sIPFile.txt',[GetHomePath]);
        if FileExists(FileName) then
        begin
           textFile.LoadFromFile(FileName);
           Memo1.Lines :=textfile;
        end
        else
        begin
          showMessage('File not exists, Create New File');
          TextFile.Text := '0.0.0.0';
          TextFile.SaveToFile(FileName);
       end;
      finally
       textFile.Free;
      end;
  except
    on E : Exception do ShowMessage('ClassError: '+e.ClassName+#13#13+'Message: '+e.Message);
  end;

end;

procedure TFrmParam.BtnSaveClick(Sender: TObject);
var
   TextFileS : TStringList;
   FileName : string;
begin
  try
    textFileS := TStringList.Create;
    try
      if memo1.Text <> '' then
      begin
        FileName := Format('%sIPFile.txt',[GetHomePath]);
        TextFileS.Text := Memo1.Text;
        TextFileS.SaveToFile(FileName);
      end;
    finally
      textFileS.Free;
    end;
  except
      on E : Exception do ShowMessage('ClassError: '+e.ClassName+#13#13+'Message: '+e.Message);
  end;
end;

 

 

procedure TDataModuleFDClient.DataModuleCreate(Sender: TObject);
var Lmemstream : Tmemorystream;
server : Tobject;
TextFile : TStringList;
FileName : string;
serverip : string;
begin
  serverip :='0.0.0.0';
  textFile := TStringList.Create;
  try
    FileName := Format('%sIPFile.txt',[GetHomePath]);
    if FileExists(FileName) then
    begin
       textFile.LoadFromFile(FileName);
       serverip :=textfile.Strings[0];
    end;
  finally
   textFile.Free;
  end;

  SQLConnection1.Connected :=false;
  sqlconnection1.params.values['HostName'] :=serverip;
  SQLConnection1.Connected :=true;

end;
 

Share this post


Link to post
Guest
Posted (edited)

my sample DS projects: Server and Client VCL

test in MSWindows and later do the changes to Android!

  1. I use RAD Studio 10.3.3 Arch, if your is another, try open the forms and units
    1. for open 2 project, just open the "ProjectGroup1.groupproj"
  2. DS Server running on MSWindows
    1. See "DSTCPServerTransport1" component and change the "Port" for your system test
  3. DS Client running on MSWindows (because is VCL) and later create a FMX and use my code
    1. See the "OnCreate" event to config your ServerIP and Port

VCL_DataSnap_Server.zip

Edited by Guest

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×