Going nutz with sockets

  • Thread starter Thread starter Sueffel
  • Start date Start date
S

Sueffel

Boy, someday I may get good at this stuff! Until then, I have a "TCP
server" that accepts multiple connections. That all works wonderfully!
YEAH!!!! I have a TCPClient that is the client connection to my server, my
question is, how do I get the client's IP from tthe TCPClient so I can know
who is logged in? I have spent most of the day looking for this on-line and
can't find nuttin yet.

Thanks
Sueffel
 
Sueffel said:
my question is, how do I get the client's IP from tthe TCPClient so I can know
who is logged in?

Do your self a favor, and inherit the TcpClient into a new class (I called
mine TcpClientEx). This way you can expose access to the underlying socket,
without going through the pain of implementing sockets directly. After you
do this, you can then use the TcpClientEx's Client (socket) property as a
means to the IP address.

NOTE: the TcpClient (at least in Fx v1.0) has a bug, it doesn't close
connections properly. Implementing your own close method is also *highly*
recommended.

Below is our TcpClientEx implementation as well as a sample listener method,
which has been modified to use the TcpClientEx Class.

HTH,
Jeremy
______________________________

Public Class TcpClientEx
Inherits Net.Sockets.TcpClient

Sub New()
Call MyBase.New()
End Sub

Sub New(ByVal LocalEP As Net.IPEndPoint)
Call MyBase.New(LocalEP)
End Sub

Sub New(ByVal HostName As String, ByVal Port As Integer)
Call MyBase.New(HostName, Port)
End Sub

Public Shadows Property Client() As Net.Sockets.Socket
Get
Return MyBase.Client
End Get
Set(ByVal Value As Net.Sockets.Socket)
MyBase.Client = Value
End Set
End Property

Public ReadOnly Property IsActive() As Boolean
Get
Return Me.Active
End Get
End Property

Private Function IsConnected() As Boolean
If Me.Client.Connected = False Then
Return False
Else
Dim bState As Boolean = Me.Client.Poll(1,
System.Net.Sockets.SelectMode.SelectRead)

If bState And (Me.Client.Available = 0) Then
Return False
Else
Return True
End If
End If
End Function
End Class


Protected Sub ListenerThread()
Dim Listener As TcpListener = New TcpListener(Me.Port)
Dim tcpClient As New Common.TcpClientEx()
Dim ClientThread As Thread

Listener.Start()

Try
While True
If Listener.Pending Then
tcpClient.Client = Listener.AcceptSocket
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf
TelnetClient), tcpClient)
tcpClient = New Common.TcpClientEx()
End If

If SERVER_KILL Then
Return
End If

'// wait for next attempt
Thread.CurrentThread.Sleep(100)
End While

Catch Ex As Exception
'TODO: handle thread exception
Finally
Listener.Stop()
End Try
End Sub
 
I guess that didn't answer your question:

After you have an instance of the TcpClientEx class, you can do this:

Dim ip As Net.IPEndPoint = CType(tcpClient.Client.RemoteEndPoint,
Net.IPEndPoint)
MsgBox(ip.Address)


I am just going from memory here, so that line may need some tweaking.

HTH,
Jeremy
 
Jeremy Cowles said:
can

Do your self a favor, and inherit the TcpClient into a new class (I called
mine TcpClientEx). This way you can expose access to the underlying socket,
without going through the pain of implementing sockets directly. After you
do this, you can then use the TcpClientEx's Client (socket) property as a
means to the IP address.

NOTE: the TcpClient (at least in Fx v1.0) has a bug, it doesn't close
connections properly. Implementing your own close method is also *highly*
recommended.

Below is our TcpClientEx implementation as well as a sample listener method,
which has been modified to use the TcpClientEx Class.

HTH,
Jeremy
______________________________

Public Class TcpClientEx
Inherits Net.Sockets.TcpClient

Sub New()
Call MyBase.New()
End Sub

Sub New(ByVal LocalEP As Net.IPEndPoint)
Call MyBase.New(LocalEP)
End Sub

Sub New(ByVal HostName As String, ByVal Port As Integer)
Call MyBase.New(HostName, Port)
End Sub

Public Shadows Property Client() As Net.Sockets.Socket
Get
Return MyBase.Client
End Get
Set(ByVal Value As Net.Sockets.Socket)
MyBase.Client = Value
End Set
End Property

Public ReadOnly Property IsActive() As Boolean
Get
Return Me.Active
End Get
End Property

Private Function IsConnected() As Boolean
If Me.Client.Connected = False Then
Return False
Else
Dim bState As Boolean = Me.Client.Poll(1,
System.Net.Sockets.SelectMode.SelectRead)

If bState And (Me.Client.Available = 0) Then
Return False
Else
Return True
End If
End If
End Function
End Class


Protected Sub ListenerThread()
Dim Listener As TcpListener = New TcpListener(Me.Port)
Dim tcpClient As New Common.TcpClientEx()
Dim ClientThread As Thread

Listener.Start()

Try
While True
If Listener.Pending Then
tcpClient.Client = Listener.AcceptSocket
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf
TelnetClient), tcpClient)
tcpClient = New Common.TcpClientEx()
End If

If SERVER_KILL Then
Return
End If

'// wait for next attempt
Thread.CurrentThread.Sleep(100)
End While

Catch Ex As Exception
'TODO: handle thread exception
Finally
Listener.Stop()
End Try
End Sub

That's pretty nifty, now the part I've been struggling with, how do I
translate a remoteendpoint to IPAddress?

Thanks again,
Sueffel
 
Back
Top