Click here to Skip to main content
16,015,697 members
Articles / Desktop Programming / Win32
Article

How to get the Client IP address or hostname of a Citrix session or Terminal Services Session?

Rate me:
Please Sign up or sign in to vote.
4.00/5 (7 votes)
5 May 2008BSD2 min read 105K   1.8K   17   10
How to get the Client IP address or hostname of a Citrix session or Terminal Services Session?

Introduction

Have you ever tried to get the Client IP address for a Citrix or terminal services session and got stuck? I have had several emails asking me on how to do it. Well I thought I might just blog about it. I can think four ways to do it and if you can add to the list then please do via comments. So let's get to the business right away.


Using the code

The simplest way is to use the MFCOM API to get a list of session and enumerate each session to get username and the Client IP address. Here is a code snippet.
VBScript
Set objFarm = CreateObject("MetaFrameCOM.MetaFrameFarm")
objFarm.Initialize(1)
For Each objSession In objFarm.Sessions
WScript.Echo "User name : " & objSession.UserName
WScript.Echo "IP Address: " & objSession.ClientAddress
Next 

You need to be an admin on XenApp (Formerly known as Presentation Sever) Farm to run MFCOM queries. You can read more and download the MFCOM example here.

2. Use WFAPI SDK WFEnumerateSessions method to get a list of all the sessions on a server and then use WFQuerySessionInformation to extract Client IP for each session on the server I have written a Sample program on how to do it. Follow this link to download WFAPI and Csharp .Net program which enumerates all sessions on a server and their Client IP address.

3. Use native terminal Services API. Similar to WFAPI use TSEnumerateSessions to get a list of sessions on a server and then use TSQuerySessionInformation to extract Client IP address each session on the server. To read more follow this link to download Terminal Services API sample example written in Csharp .Net.

4. You can also use ICO SDK and GetClientAddress API to get client IP within a ICO session The ICA Client Object is the framework that exposes the functionality of the Citrix ICA Win32 Client to third party applications. The ICA Client Object (ICO) SDK enables developers and administrators to modify the behavior and appearance of a Windows 32-bit Citrix ICA client. The SDK is a series of documents that detail available application programming interface (API) in the Citrix ICA client Follow this link to download an ICO example which illustrates the use of methods and properties available to get client network name and IP address information using Citrix ICO SDK.

Code snippet for ICO in JavaScript

JavaScript
function GetClientNetworkName(form)
    {
        form.netname.value = document.ICO1.GetClientNetworkName()
    }
    function GetAddrCnt(form)
    {
        form.addrcnt.value = document.ICO1.GetClientAddressCount()
    }
    function GetAddr(form)
    {
        form.addr.value = document.ICO1.GetClientAddress(0)
    } 



History

If you have any questions regarding Citrix MFCOM then drop me a line at my blog

Vishal Ganeriwala

http://community.citrix.com/blogs/citrite/vishalg/

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Team Leader Citrix Systems
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralActual client machine IP Pin
Stephen Drew3-Jun-09 6:16
Stephen Drew3-Jun-09 6:16 
GeneralLink Broken Pin
Pradeep Manohar10-Mar-09 11:24
Pradeep Manohar10-Mar-09 11:24 
Questiontest if citrix session before getting the client IP address [modified] Pin
Patricia Nassif17-Nov-08 2:58
Patricia Nassif17-Nov-08 2:58 
AnswerRe: test if citrix session before getting the client IP address Pin
jp2code8-Apr-09 11:14
professionaljp2code8-Apr-09 11:14 
QuestionHow to get the actual client IP? Pin
vrbear8-Sep-08 22:19
vrbear8-Sep-08 22:19 
GeneralAnother method Pin
mav.northwind6-May-08 7:19
mav.northwind6-May-08 7:19 
GeneralRe: Another method Pin
Vishal Ganeriwala6-May-08 9:29
Vishal Ganeriwala6-May-08 9:29 
GeneralRe: Another method Pin
mav.northwind6-May-08 22:09
mav.northwind6-May-08 22:09 
GeneralRe: Another method Pin
Jürgen Kliegl13-May-08 23:34
Jürgen Kliegl13-May-08 23:34 
Hello,

thanks for the good aricle.
Some tests with client-ip on a windows2K3 TS-Server via VPN

Only machinename tries to find the client-ip which I have in enterprise network and therefor failed:
ping byicak
Pinging byicak.BAYER-AG.COM [10.162.107.56] with 32 bytes of data:
Request timed out.

Pinging on my client-machine shows this result
ping byicak
Ping BYICAK.emea.healthcare.cnb [10.199.5.199] mit 32 Bytes Daten:
Antwort von 10.199.5.199: Bytes=32 Zeit<1ms TTL=128


Piniging on TS-Server with complete DNS-Name fails:
Ping BYICAK.emea.healthcare.cnb
Ping request could not find host BYICAK.emea.healthcare.cnb. Please check the name and try again.


Pinging the ip-address on TS-Server suceeds:
ping -a 10.199.5.199
Pinging vpn-10-199-5-199.bayer-ag.com [10.199.5.199] with 32 bytes of data:

This value is identical with the one I get with the following C++ snippet,
which is analogues to "3. Use native terminal Services API" I believe and which I use since NT4 Terminal-Server-Edition.

if (!WTSQuerySessionInformation(serverHandle, sessionID,
WTSClientAddress, &pInfo, &BytesReturned))
{
bError = false;
_ftprintf(stream, TEXT("%s"),"E 7\t\n");
}
else
{
BYTE * pByte;
PWTS_CLIENT_ADDRESS pClAddress = (PWTS_CLIENT_ADDRESS) pInfo;
switch ( pClAddress->AddressFamily ) {
case 0 :
tempString.Format( TEXT("%s"), pClAddress->Address );
pClientAddress = tempString.GetBuffer(20);
break;
case 2 :
pByte = &pClAddress->Address[2];
tempString.Format( TEXT("%u.%u.%u.%u"),
pByte[0], pByte[1], pByte[2], pByte[3] );
pClientAddress = tempString.GetBuffer(20);
break;
}
QuestionRe: Another method Pin
StevoM12-Jun-08 22:03
StevoM12-Jun-08 22:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.