By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,572 Members | 1,516 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,572 IT Pros & Developers. It's quick & easy.

Going nutz with sockets

P: n/a
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
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.558 / Virus Database: 350 - Release Date: 1/2/2004
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Sueffel" <so*****@somewhere.com> wrote in message
news:OH**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #2

P: n/a
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

Nov 20 '05 #3

P: n/a

"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message
news:Lp******************@twister.tampabay.rr.com. ..
"Sueffel" <so*****@somewhere.com> wrote in message
news:OH**************@TK2MSFTNGP10.phx.gbl...
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


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

Thanks again,
Sueffel
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.558 / Virus Database: 350 - Release Date: 1/2/2004
Nov 20 '05 #4

P: n/a
"Sueffel" <so*****@somewhere.com> wrote in message
news:OW**************@TK2MSFTNGP11.phx.gbl...
That's pretty nifty, now the part I've been struggling with, how do I
translate a remoteendpoint to IPAddress?


see my 2nd post

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.