Client IP

huhhuhhuh

Active member
Joined
Feb 28, 2006
Messages
42
Programming Experience
Beginner
Suppose I've accepted a request using
VB.NET:
tcpclient=tcplistener.accepttcpclient

How do I get the IP address of the client/source of the request? What format should I store it in?
 
Cast the TcpClient.Client.RemoteEndPoint to an IPEndPoint to read out the Address and Port
 
broadcast... funny :)
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] tcp [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Net.Sockets.TcpClient()
tcp.Connect([/SIZE][SIZE=2][COLOR=#800000]"www.microsoft.com"[/COLOR][/SIZE][SIZE=2], 80)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ip [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Net.IPEndPoint = [/SIZE][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][SIZE=2](tcp.Client.RemoteEndPoint, Net.IPEndPoint)
MsgBox(ip.Address.ToString & "  " & [SIZE=2]ip.Port.ToString)[/SIZE]
[/SIZE]
 
VB.NET:
Dim mytcpclient As TcpClient = mylistener.AcceptTcpClient
Dim ip As Net.IPEndPoint = DirectCast(tcpclient.Client.RemoteEndPoint, Net.IPEndPoint)
Dim strip As String = ip.Address.ToString & " " & ip.Port.ToString

This is what I tried, but
VB.NET:
tcpclient.Client
is unacceptable because it is supposedly Protected.

And what is Directcast?
 
Oops, my bad. But it still returns the same message, indicating mytcpclient is Protected. When I look at all the methods available to mytcpclient after mytcpclient., I can't seem to find Client anywhere.

So DirectCast isn't broadcast.
 
Strange, it was tested fine in VS2005 and these codes are the same in .Net 1 & 2. I will check with VS2003 in the evening.
 
Thanks, maybe it has something to do with my declaration?

I declared

Public tcpclient as tcpclient

at the class level, then

Dim mytcpclient As TcpClient = mylistener.AcceptTcpClient
Dim ip As Net.IPEndPoint = DirectCast(mytcpclient.client.RemoteEndPoint, Net.IPEndPoint)
Dim strip As String = ip.Address.ToString & " " & ip.Port.ToString

in a listen() method

Maybe this has relevance?
 
You're right, it doesn't work in VS2003/Net1.1. Here is an easy workaround, create your own class and inherit from TcpClient to access its protected property, expose the IP in your own property. Here is how:
VB.NET:
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] example()
[/SIZE][SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][SIZE=2] myTcp [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] myTcpClient
  myTcp.Connect("www.microsoft.com", 80)
[/SIZE][SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][SIZE=2] ip [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Net.IPEndPoint = myTcp.remoteIP
  MsgBox(ip.Address.ToString & " " & ip.Port.ToString)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] myTcpClient
[/SIZE][SIZE=2][COLOR=#0000ff]Inherits[/COLOR][/SIZE][SIZE=2] Net.Sockets.TcpClient
[/SIZE][SIZE=2][COLOR=#0000ff]  Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ReadOnly [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] remoteIP() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Net.IPEndPoint
[/SIZE][SIZE=2][COLOR=#0000ff]  Get
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]    Return [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Client.RemoteEndPoint, Net.IPEndPoint)
[/SIZE][SIZE=2][COLOR=#0000ff]  End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]  End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class
[/COLOR][/SIZE]
 
As the above work in that use of the TcpClient, I found that for your use with the TcpListener requires a little more code, this is tested and works for VS2003:
VB.NET:
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] tcpex2()
[/SIZE][SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][SIZE=2] ip [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Net.IPAddress = Net.IPAddress.Parse("127.0.0.1")
[/SIZE][SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][SIZE=2] tcpL [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Net.Sockets.TcpListener(ip, 8000)
  tcpL.Start()
[/SIZE][SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][SIZE=2] so [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Net.Sockets.Socket = tcpL.AcceptSocket
  tcpL.Stop()
[/SIZE][SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][SIZE=2] myTcp [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] myTcpClient(so)
[/SIZE][SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][SIZE=2] remIP [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Net.IPEndPoint = myTcp.remoteIP
  MsgBox(remIP.Address.ToString & " " & remIP.Port.ToString)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] myTcpClient
[/SIZE][SIZE=2][COLOR=#0000ff]Inherits[/COLOR][/SIZE][SIZE=2] Net.Sockets.TcpClient
[/SIZE][SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]()
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] so [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Net.Sockets.Socket)
[/SIZE][SIZE=2][COLOR=#0000ff]  Me[/COLOR][/SIZE][SIZE=2].Client = so
[/SIZE][SIZE=2][COLOR=#0000ff]  Me[/COLOR][/SIZE][SIZE=2].Active = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ReadOnly [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] remoteIP() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Net.IPEndPoint
[/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]  Return [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Client.RemoteEndPoint, Net.IPEndPoint)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class
[/COLOR][/SIZE]
 
@JohnH
can u give example(vb .net 2005) server - client,when the server got client connected it will display the client ip and remote port
 
Sabour,
As I said in first reply in .Net 2.0 you can directly access the public Client property without digging in the socket, which gives code:
VB.NET:
Dim ip As Net.IPAddress = Net.IPAddress.Parse("127.0.0.1")
Dim tcpL As New Net.Sockets.TcpListener(ip, 8000)
tcpL.Start()
Dim tcpclient As Net.Sockets.TcpClient = tcpL.AcceptTcpClient()
Dim remIP As Net.IPEndPoint = DirectCast(tcpclient.Client.RemoteEndPoint, Net.IPEndPoint)
MsgBox(remIP.Address.ToString & " " & remIP.Port.ToString)
 
i have error when integrated those code :
VB.NET:
ex.Message = "Unable to cast object of type 'System.Net.Sockets.AcceptAsyncResult' to type 'System.Net.Sockets.TcpClient'."

how to implement into this code?
VB.NET:
Listening_Port = CInt(txtListeningPort.Text)
                TCPL = New TcpListener(Net.IPAddress.Any, Listening_Port)
                TCPL.Start()
                ListBox1.Items.Add("Waiting Connection From Client PC...")
 
                TCPL.BeginAcceptTcpClient(AddressOf OnConnect, Nothing)
 
Back
Top