Solved

Get IP Address of Computer running an application through Terminal Services

Posted on 2006-11-01
15
1,215 Views
Last Modified: 2008-01-09
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
Comment
Question by:jwebster77
  • 8
  • 7
15 Comments
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
Dim ipend As Net.IPEndPoint = Client.Client.RemoteEndPoint
0
 

Author Comment

by:jwebster77
Comment Utility
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
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
use notation ClassName.FUnctionName
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
ClientConnection.PublicIPAddress()
0
 

Author Comment

by:jwebster77
Comment Utility
That's not working.  The only thing I get is ClientConnection.Equals and clientconnection.referencequals
0
 

Author Comment

by:jwebster77
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 35

Expert Comment

by:YZlat
Comment Utility
this worked for me:
 Dim cls As New ClientConnection
            cls.PublicIPAddress()
0
 

Author Comment

by:jwebster77
Comment Utility
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
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
means you'll neew an argument for Sub New:

 Dim cls As New ClientConnection(...argument here...)
            IPAddress = cls.PublicIPAddress
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
you will need to pass it a TcpClient object
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
Comment Utility
0
 

Author Comment

by:jwebster77
Comment Utility
          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
 

Author Comment

by:jwebster77
Comment Utility
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
 

Author Comment

by:jwebster77
Comment Utility

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now