Solved

Get IP Address of Computer running an application through Terminal Services

Posted on 2006-11-01
15
1,219 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
ID: 17850239
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17850250
Dim ipend As Net.IPEndPoint = Client.Client.RemoteEndPoint
0
 

Author Comment

by:jwebster77
ID: 17850451
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 35

Expert Comment

by:YZlat
ID: 17850466
use notation ClassName.FUnctionName
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17850471
ClientConnection.PublicIPAddress()
0
 

Author Comment

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

Author Comment

by:jwebster77
ID: 17850498
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
 
LVL 35

Expert Comment

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

Author Comment

by:jwebster77
ID: 17850790
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
ID: 17850928
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
ID: 17850940
you will need to pass it a TcpClient object
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 17850975
0
 

Author Comment

by:jwebster77
ID: 17851606
          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
ID: 17853130
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
ID: 17860633

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I need help making a collection from a class in VB.Net 3 34
Get Client IP on RDS - VB.NET 15 49
No Data for DropDown List 2 26
VB.NET 2008 Winforms Signing 13 20
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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