Simple Server Question

  • Thread starter Thread starter C# Learner
  • Start date Start date
C

C# Learner

I'm writing a server which accepts connections with the following line:

TcpClient client = listener.AcceptTcpClient();

Next, I'd like to be to determine the client's IP address.

How can I do this? No relevant property/method in TcpClient exists.
 
Try
Socket s = tcpClient.Client;
IPEndPoint ep = s.RemoteEndPoint as IPEndPoint;
IPAddress address = ep.Address

--
Jared Parson [MSFT]
(e-mail address removed)

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
Jared said:
Try
Socket s = tcpClient.Client;
IPEndPoint ep = s.RemoteEndPoint as IPEndPoint;
IPAddress address = ep.Address

Hi Jared,

'Client' doesn't appear to be a property of 'TcpClient'. What is
'tcpClient' an instance of in the above code?

Thanks.
 
Sorry. Didn't read the documentation close enough. Apparently the
TcpClient.Client property is protected. You can try sub classing TcpClient
to get to the Client property. Outside of reflection I don't see another
way to get to that property.

--
Jared Parson [MSFT]
(e-mail address removed)

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
Jared said:
Sorry. Didn't read the documentation close enough. Apparently the
TcpClient.Client property is protected. You can try sub classing TcpClient
to get to the Client property. Outside of reflection I don't see another
way to get to that property.

Ah... it seems a real nuisance that it's protected.
 
Jared said:
Sorry. Didn't read the documentation close enough. Apparently the
TcpClient.Client property is protected. You can try sub classing TcpClient
to get to the Client property. Outside of reflection I don't see another
way to get to that property.

So I guess it isn't possible to determine the client's IP address when
using TcpListener.AcceptTcpClient(), then...
 
Chris said:
client.RemoteEndPoint..Address.ToString() should give it to you.

Hi,

'TcpClient' doesn't have a property called 'RemoteEndPoint'.

BTW, I'm using 'TcpListener.AcceptSocket()' instead of
'TcpListener.AcceptTcpClient()' now, so my question no longer needs an
answer.

Thanks for the replies.
 
C# Learner said:
'TcpClient' doesn't have a property called 'RemoteEndPoint'.

BTW, I'm using 'TcpListener.AcceptSocket()' instead of
'TcpListener.AcceptTcpClient()' now, so my question no longer needs an
answer.

Indy exposes all this for you. :)

www.indyproject.org

Its free too.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
 
Chad said:
Indy exposes all this for you. :)

www.indyproject.org

Its free too.

Hi Chad,

Yeah, Indy was the first thing that crossed my mind when I started this
project, but I decided to try using the lower-level .NET sockets for the
learning experience (my HTTP knowledge was a bit stale and re-inventing
the wheel is a good way for me to learn :-).

Cheers.
 
Back
Top