Simple Server Question

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.
 
J

Jared Parsons [MSFT]

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
 
C

C# Learner

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.
 
J

Jared Parsons [MSFT]

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
 
C

C# Learner

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.
 
C

C# Learner

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...
 
C

C# Learner

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

Chad Z. Hower aka Kudzu

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/
 
C

C# Learner

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top