3
votes

I'm trying to get a client's IP address and other client information using DSServer's onconnect event with the following code.

My problem is that DSConnectEventObject.ChannelInfo is nil every time. Additionally, I can't resolve the IP addresses.

Please help me. Thank you.

procedure TWebModule1.DSServer1Connect(DSConnectEventObject: TDSConnectEventObject);
var
     ci: TDBXClientInfo;
begin
     ci := DSConnectEventObject.ChannelInfo.ClientInfo;
     AddLog(Format('Client %s Connected IP: %s, Port: %s', 
         [ci.Protocol, ci.IpAddress, ci.ClientPort])
     );
end;
4

4 Answers

2
votes

As mentioned previously, this is a bug in DataSnap. It was working fine in XE2 but the error came in somewhere between XE3 and XE5. It has been reported in QC reports #121931 and #126164. Luckily, the client connection properties are available in the Session object - see below :

var
  Session: TDSSession;
  Protocol, IpAddress, AppName: string;
begin
  Session := TDSSessionManager.GetThreadSession;
  Protocol := Session.GetData('CommunicationProtocol');
  IpAddress := Session.GetData('RemoteIP');
  AppName := Session.GetData('RemoteAppName');
end;
1
votes

how about this?

in WebMoudle unit,

save IP:

implementation
threadvar remoteIP: string;

procedure TWebModule1.WebModuleBeforeDispatch(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  remoteIP := Request.RemoteAddr;
end;

procedure TWebModule1.DSServer1Connect(DSConnectEventObject: TDSConnectEventObject);
begin
  TDSSessionManager.GetThreadSession.PutData('RemoteAddr', remoteIP);
end;

procedure TWebModule1.DSServer1Disconnect(DSConnectEventObject: TDSConnectEventObject);
begin
  remoteIP := '';
end;
0
votes

DataSnap REST ISAPI dll


in WebMoudle unit,

save IP:

TDSSessionManager.GetThreadSession.PutData('RemoteAddr',Request.RemoteAddr);


in other unit,

get IP:

uIP := TDSSessionManager.GetThreadSession.GetData('RemoteAddr');


0
votes

This is Bug.
You can do it code, below:

procedure TWebModule.DSServerConnect(
  DSConnectEventObject: TDSConnectEventObject);
var _Session: TDSSession;
begin
  try
    if Assigned(DSConnectEventObject.ChannelInfo) then
    begin
      _Session := TDSSessionManager.GetThreadSession;
      if Assigned(_Session) then
      begin
        if _Session.GetData('RemoteAddr') = '' then
          _Session.PutData('RemoteAddr', DSConnectEventObject.ChannelInfo.Info);
      end;
    end;
  except
  end;
end;