Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1259
  • Last Modified:

Get IP Address of Computer running an application through Terminal Services

Hello all,

What I want to do is get the ip address of a thin client that is running an application through terminal services on a server.  I am using vb.net...  I used        
Dim ipE As IPHostEntry = Dns.GetHostEntry(sHOstName)

but this retrieves the ip address of the terminal server, and not the client.  We have a vb6 application that successfully gets the ip of the client, and that code is posted below.  The problem I have is that I don't know how to acheive this same thing in vb.net  THIS IS URGENT!  LOTS OF POINTS!

Here is the vb6 code that accomplishes what we want to accomplish

Public Function GetWTSQueryAddress() As String
    Dim RetVal As Long, lpBuffer As Long, count As Long
    Dim p As Long, i As Integer
    Dim uName As String
   ' Dim WTSQueryInfo() As WTS_SESSION_QUERY
    Dim WTSQueryAddress As WTS_CLIENT_ADDRESS
           
    Dim SesID As Long
    'SesID = GetWTSSessionID
           
    RetVal = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, _
                                   WTS_CURRENT_SESSION, WTSClientAddress, lpBuffer, count)
    If RetVal Then
        p = lpBuffer
        CopyMemory WTSQueryAddress, ByVal p, LenB(WTSQueryAddress)
               
        uName = ""
        For i = 2 To 5
            uName = uName & WTSQueryAddress.address(i)
            If i < 5 Then uName = uName & "."
        Next i
     Else
        'MsgBox "An error occurred calling WTSQuerySessionInformation.  "
        uName = ""
    End If
   
    GetWTSQueryAddress = uName
End Function
0
jwebster77
Asked:
jwebster77
  • 8
  • 7
1 Solution
 
YZlatCommented:
Dim ipend As Net.IPEndPoint = Client.Client.RemoteEndPoint
0
 
jwebster77Author Commented:
I have a class now called ClientConnection.vb with the code from the example you showed me.  I suppose I want to call PublicIPAddress() from my frmlogin form where I want to use the ip address?  How do I call it?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
YZlatCommented:
use notation ClassName.FUnctionName
0
 
YZlatCommented:
ClientConnection.PublicIPAddress()
0
 
jwebster77Author Commented:
That's not working.  The only thing I get is ClientConnection.Equals and clientconnection.referencequals
0
 
jwebster77Author Commented:
Here is my class...

Imports System.Net.Sockets
Imports System.Reflection

Public Class ClientConnection
    Implements IDisposable
    Private Client As TcpClient
    Private PublicIP As String

    ' TODO: Add code for PublicIPAddress property here

    Public Sub New(ByVal client As TcpClient)
        ' Validate parameters
        If client Is Nothing Then
            Throw New ArgumentNullException("client")
        End If
        Me.Client = client
    End Sub

#Region " IDisposable Support "
    Private disposedValue As Boolean ' To detect redundant calls
    Private Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: free unmanaged resources here
                If Not Client Is Nothing Then
                    Client.Close()
                    Client = Nothing
                End If
            End If
            ' TODO: free shared unmanaged resources      
        End If
        Me.disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code. Put cleanup code
        ' in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

    Public ReadOnly Property PublicIPAddress() As String
        Get
            If PublicIP = String.Empty Then
                Try
                    ' Get the clients IP address using Client property            
                    Dim ipend As Net.IPEndPoint = Client.Client.RemoteEndPoint
                    If Not ipend Is Nothing Then
                        PublicIP = ipend.Address.ToString
                    End If
                Catch ex As System.ObjectDisposedException
                    PublicIP = String.Empty
                Catch ex As SocketException
                    PublicIP = String.Empty
                End Try
            End If
            Return PublicIP
        End Get
    End Property
#End Region
End Class
0
 
YZlatCommented:
this worked for me:
 Dim cls As New ClientConnection
            cls.PublicIPAddress()
0
 
jwebster77Author Commented:
It's very close to working!   Here is my code now when I call it...

  Dim cls As New ClientConnection
            IPAddress = cls.PublicIPAddress

The cls in the line:  Dim cls As New ClientConnection is underlined and it says:

Argument not specified for parameter 'Client' of 'Public Sub New(Client As System.Net.Sockets.TcpClient)'

Thanks for your help thus far.  Any ideas on why I am getting this error.

jw
0
 
YZlatCommented:
means you'll neew an argument for Sub New:

 Dim cls As New ClientConnection(...argument here...)
            IPAddress = cls.PublicIPAddress
0
 
YZlatCommented:
you will need to pass it a TcpClient object
0
 
YZlatCommented:
0
 
jwebster77Author Commented:
          I ended up doing this to get the code to work...  It is still bringing back the IP Address of the server though and not the client connected to terminal services.  Very frustrating.  If anyone has any ideas, please let me know.

 Dim hi As New TcpClient
            Dim cls As New ClientConnection(hi)
            IPAddress = cls.PublicIPAddress
            GetTerminalInfo(IPAddress)
0
 
jwebster77Author Commented:
YZlat, I can't get this to work correctly.  It actually does not bring back an ip address.  If you can get it to work PLEASE let me know.  My email is jimwebster77@gmail.com!  I am going to repost the question to see if I can get anyone else to also respond.  Maybe I am doing something wrong, but I have not had any luck.
0
 
jwebster77Author Commented:

Hello all, I did this a bit different but it gives me the client IP, not the server IP so all is well.  I have a vb6 app that utilizes somes Terminal Services calls which works nice.  The calls feth the ip of the current client on that session, (not the ip of the server, which was the problem at hand by using the typical methodology in .net to get an ip address)  After the vb6 app grabs this ip address of the client I am simply inserting into a command and launching my .net applicationswitcher which can then obtain that ip address and run with it.  So, all is well...

If you have the need for the very same functionality it works very nice.  Email me at jimwebster77@gmail.com

Thanks to YZlat for all the efforts to help me with this.

JW
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now