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