Datasnap client IP within serverMethod

306 Views Asked by At

I'm trying to determine the origin of the client IP within any Tcomponent based method of a VCL forms TCP datasnap server - without any channels (besides from the client having to send it). I also looked for a way to reference the server's connection info to retrieve the client's IP, but without success.

Was hoping to see procedure/function work something like the following:

procedure TServerMethods1.someTask(userId, taskId :integer; data:string);// can be a function
var
  ip:string;
begin
  ip := (ServerContainer1.TDSServer.GetThreadSession).GetData('RemoteIP');// client IP
  // do stuff..
end;

TIA..

2

There are 2 best solutions below

3
Freddie Bell On BEST ANSWER

Based on your original question, and your subequent comments, this should answer your question.

procedure TServerContainer.DSServer1Connect(
  DSConnectEventObject: TDSConnectEventObject);
var
  user: String;
  ClientInfo: TdbxClientInfo;
  ipaddy: String;
begin

  // note: this procedure gets called immediately AFTER DSAuthenticationManager1UserAuthenticate
  user := TDSSessionManager.GetThreadSession.GetData('User');

  ClientInfo := DSConnectEventObject.ChannelInfo.ClientInfo;
  ipAddy := ClientInfo.IpAddress;
  TDSSessionManager.GetThreadSession.PutData('ipaddy', ipAddy);

end;

procedure TServerMethods1.someTask(userId, taskId :integer; data:string);// can be a function
var
  ip:string;
begin
  ip := TDSSessionManager.GetThreadSession.GetData('ipaddy')
  // do stuff..
end;
  
1
Hikaru  Fukushi On

try to use TDSSessionManager.GetThreadSession method instead of ServerContainer1.TDSServer.GetThreadSession.

(Edited)

This is a a simple implementation on the EchoString datasnap server method.

unit ServerMethodsUnit1;

interface

uses System.SysUtils, System.Classes, Datasnap.DSServer, Datasnap.DSAuth;

type
{$METHODINFO ON}
  TServerMethods1 = class(TComponent)
  private
  public
    function EchoString(Value: string): string;
  end;
{$METHODINFO OFF}

implementation


uses System.StrUtils, Datasnap.DSSession;

function TServerMethods1.EchoString(Value: string): string;
begin
  Result := 'Your IP address is ' + TDSSessionManager.GetThreadSession.GetData('RemoteIP') + ':' + TDSSessionManager.GetThreadSession.GetData('RemotePort');
end;

end.