tcplistener, using threaded functions, need to access client IP from called function

jeffulot43

Member
Joined
Sep 25, 2014
Messages
5
Programming Experience
5-10
Hello All!

I have a function that is doing fine receiving data. I can access the variable that holds the client's i address in the original form, but in an external called function I cannot get that value.

LISTENER:

Private Sub doListen()
Dim incomingClient As System.Net.Sockets.TcpClient
Do
incomingClient = listener.AcceptTcpClient 'Accept the incoming connection. This is a blocking method so execution will halt here until someone tries to connect.
'works here
Dim ipAddr As String = (IPAddress.Parse(CType(incomingClient.Client.RemoteEndPoint, IPEndPoint).Address.ToString())).ToString()
Dim connClient As New ConnectedClient(incomingClient, Me) 'Create a new instance of ConnectedClient (check its constructor to see whats happening now).
AddHandler connClient.dataReceived, AddressOf Me.messageReceived
clients.Add(connClient) 'Adds the connected client to the list of connected clients.
Loop
End Sub

Private Sub messageReceived(ByVal sender As ConnectedClient, ByVal message As String)
'does not work here
'Dim ipAddr As String = (IPAddress.Parse(CType(incomingClient.Client.RemoteEndPoint, IPEndPoint).Address.ToString())).ToString()
'MsgBox(ipaddr)
End Sub

I need to know how to access the ip address value here, and I need to capture the MAC address as well if I can.

Any help is greatly appreciated!

J
 
You have passed TcpClient reference (incomingClient) to ConnectedClient, so it is available there, if not you could make it available.
messageReceived will grant you ConnectedClient (sender), so you can ask that for its TcpClient reference.

If ConnectedClient is sealed for you to modify, and it otherwise don't expose the TcpClient (or its client ip), then you have no other option than to maintain a list yourself that maps each ConnectedClient to its underlying TcpClient/ip, eg using a Dictionary.
 
Thank you John. Could you help me with examples of that and maybe a reference to MAC address as well?

I have tried to reference it using client and connected client, but I'm getting the syntax wrong I'm sure.
 
Thanks John. I'm trying here, but I've not added a property before...---- It doesn't like the Mclient or TCPClient being used there.

Public Class ConnectedClient
Private mClient As System.Net.Sockets.TcpClient
Private mUsername As String
Private mParentForm As Form1
Private readThread As System.Threading.Thread
Private Const MESSAGE_DELIMITER As Char = ControlChars.Cr

Property TheIPAddress As String = (IPAddress.Parse(CType(mClient.Client.RemoteEndPoint, IPEndPoint).Address.ToString())).ToString()

Public Event dataReceived(ByVal sender As ConnectedClient, ByVal message As String)
.
.
.
 
It doesn't like the Mclient or TCPClient being used there.
Assign a value to the TheIPAddress when it is known, as I said from the constructor.

Why are you converting IPAddress to String, then parse it back to IPAddress, then convert that to String again?
 
OK, I got it working and showing me it has it in the constructor (in the Sub new()). Can you help me with the syntax of how to read it when I'm up in the MessageREceived Function?
 
Got it, I created:

Public Property TheIP() As String
Get
Return TheIPAddress
End Get
Set(ByVal value As String)
TheIPAddress = value
End Set
End Property

Thanks for your help.

J
 
Back
Top