how to obtain host and port from TcpClient...

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
P

puzzlecracker

It looks like i need to get IPEndPoint first, but I cannot figure out
from msdn the eventual obtainment of machine name and port number.
Please suggest a solution.

Thanks
 
puzzlecracker said:
It looks like i need to get IPEndPoint first, but I cannot figure out
from msdn the eventual obtainment of machine name and port number.
Please suggest a solution.
There is no solution. You should be the one responsible for creating the
TcpClient, so you also know the endpoint. Conversely, if you're just using a
TcpClient created somewhere else, you have no business with the endpoint the
TcpClient is abstracting over.

Alright, so there is one solution: use reflection to read the TcpClient's
private members, in particular the Socket it encapsulates. There is no
reason why such code shouldn't break with the next release of the framework,
though. Only use it for an incident.

Another solution is to forego the (rather weak) abstraction provided by
TcpClient altogether and use Socket directly. Socket has a .RemoteEndpoint
property.
 
Peter said:
puzzlecracker said:
It looks like i need to get IPEndPoint first, but I cannot figure out
from msdn the eventual obtainment of machine name and port number.
Please suggest a solution.
There is no solution. You should be the one responsible for creating
the TcpClient, so you also know the endpoint. Conversely, if you're
just using a TcpClient created somewhere else, you have no business
with the endpoint the TcpClient is abstracting over.

Alright, so there is one solution: use reflection to read the
TcpClient's private members, in particular the Socket it encapsulates.
There is no reason why such code shouldn't break with the next release
of the framework, though. Only use it for an incident. [...]

Actually...

While I agree with your general comments about whether one should need
the IPEndPoint at all, you can retrieve the Socket for the TcpClient
with the public Client property.

Oops. Well, at least it invalidates my central point *completely*, so I can
just say I was utterly wrong. This saves us a lot of tedious discussion over
details.
 
It looks like i need to get IPEndPoint first, but I cannot figure out
from msdn the eventual obtainment of machine name and port number.
Please suggest a solution.

Thanks

TcpClient.Client.LocalEndPoint & RemoteEndPoint
 
Back
Top