473,474 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Going nutz with sockets

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
4 1509
"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
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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
4
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
3
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly...
3
by: J C | last post by:
Hi, I'm using UDPClient to make a simple DNS server. I notice that intermittently and unpredictibly I get: Unhandled Exception: System.Net.Sockets.SocketException: An existing connection...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.