Click to See Complete Forum and Search --> : client ipaddress??on tcplistener.accepttcpclient


namsun
April 3rd, 2004, 10:09 PM
i want to check client ipadress, on tcplistener.accepttcpclient.

dim client1 as tcpclient
dim listener as tcplistener
dim address1 As IPAddress

listener = new tcplistener(--,--)
listener.start()
client1=listener.accepttcpclient

address1=client1.tcpclient.client.RemoteEndPoint.Address????
-----failed------

how can i get client1.....Adress?

Craig Gemmill
April 3rd, 2004, 10:25 PM
' Using the RemoteEndPoint property.
Console.WriteLine("I am connected to ")
Console.WriteLine(IPAddress.Parse(CType(s.RemoteEndPoint, IPEndPoint).Address.ToString()))
Console.WriteLine("on port number ")
Console.WriteLine(CType(s.RemoteEndPoint, IPEndPoint).Port.ToString())

' Using the LocalEndPoint property.
Console.WriteLine("My local IpAddress is :")
Console.WriteLine(IPAddress.Parse(CType(s.LocalEndPoint, IPEndPoint).Address.ToString()))
Console.WriteLine("I am connected on port number ")
Console.WriteLine(CType(s.LocalEndPoint, IPEndPoint).Port.ToString())


EDIT: Ok, this is not going to let me paste the entire link w/o formating, so I had to give you a google search. Just click the first result:
http://www.google.com/search?q=Socket.RemoteEndPoint+Property

namsun
April 4th, 2004, 01:12 AM
dim client1 as tcpclient
dim listener as tcplistener
dim str_ip as String
dim s As Socket

listener = new tcplistener(--,--)
listener.start()
client1=listener.accepttcpclient

s = client1 ----Fualt----- ???
str_ip = CType(s.RemoteEndPoint, IPEndPoint).Address.ToString

Still fault.
please advise how tcpclient is changed to socket.

Craig Gemmill
April 4th, 2004, 10:03 AM
My fault for not looking closer, just assumed you were using AcceptSocket.

Here is how to do it:
http://www.codeproject.com/managedcpp/get_the_ip_out_tcpclient.asp

Here is the vb.net port, let me know if it works ok:


Public Class MyNetworkStream
Inherits System.Net.Sockets.NetworkStream
'
'You have to create a derived class to expose protected members
'
Public Sub New(ByVal s As System.Net.Sockets.Socket)
MyBase.New(s)
End Sub

Public Function MySocket() As System.Net.Sockets.Socket
Return MyBase.Socket
End Function

Public Function IPAddress() As String
Dim soc As System.Net.Sockets.Socket = Socket
Dim Endp As System.Net.EndPoint = soc.RemoteEndPoint

Return Endp.ToString
End Function
End Class


and use it like this:

Dim client1 As System.Net.Sockets.TcpClient
Dim listener As System.Net.Sockets.TcpListener
Dim netStream As System.Net.Sockets.NetworkStream
Dim myStream As MyNetworkStream

listener = New System.Net.Sockets.TcpListener(---,---)
listener.Start()
client1 = listener.AcceptTcpClient

'
'Get the underlying network stream from the TcpClient
'
netStream = client1.GetStream()
'
'Cast the networkstream to the derived class, mynetworkstream
'
myStream = DirectCast(netStream, MyNetworkStream)
'
'Return IP address
'
Debug.WriteLine("Client IP: " & myStream.IPAddress)

namsun
April 5th, 2004, 06:01 AM
thanks
i failed at
myStream = DirectCast(netStream, MyNetworkStream)
with system.invalidcastexception.

i tried cType for DirectCast, but same error.

help me again.

namsun
April 6th, 2004, 01:42 AM
Thanks to Craig Gemmill suggestion, i get it.

quote------------
http://www.codeproject.com/managedc...t_tcpclient.asp
thanks for the description Adam Jones 17:11 17 Nov '03
Now I finally know where MS hid it :)
fyi,you can also get at the protected Socket memeber via reflection

NetworkStream myStream = myTcpClient.GetStream();
Type streamType = myStream.GetType();
PropertyInfo pi = streamType.GetProperty("Socket", BindingFlags.NonPublic | BindingFlags.Instance);
Socket soc = (Socket)pi.GetValue(this.netstream, null);
EndPoint endp = soc.RemoteEndPoint;
String HostAddress = endp.ToString();
Adam
unquote--------------------------

Imports System.Net.Sockets
Imports System.Net
Imports System.Reflection

dim listener as TcpListener
dim client1 As TcpClient
dim netStream As NetworkStream
dim streamType As Type
dim pInfo As System.Reflection.PropertyInfo
dim soc As Socket
dim strRmtIPAdrss, strRmtPort As String

listener = new TcpListener(--,--)
listener.start()
client1=listener.AcceptTcpclient

netStream = client.GetStream()
streamType = netStream.GetType
pInfo = streamType.GetProperty("Socket", BindingFlags.NonPublic Or BindingFlags.Instance)
soc = CType(pInfo.GetValue(netStream, Nothing), Socket)
strRmtIPAdrss = CType(soc.RemoteEndPoint, IPEndPoint).Address.ToString()
strRmtPort = CType(soc.RemoteEndPoint, IPEndPoint).Port.ToString()
MsgBox(strRmtIPAdrss & "-" & strRmtPort)

i dont know why, but it does.
thanks again.

Craig Gemmill
April 6th, 2004, 02:00 AM
Thanks for following up with that... very interesting.

So much for protecting members ;)