473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to obtain host and port from TcpClient...

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
Oct 21 '08 #1
5 7258
puzzlecracker wrote:
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.

--
J.
Oct 21 '08 #2
On Tue, 21 Oct 2008 10:00:55 -0700, Jeroen Mostert <jm******@xs4all.nl>
wrote:
puzzlecracker wrote:
>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. There's no reflection needed, nor should it
necessarily be a long-term maintenance problem, at least not with respect
to the retrieval of the Socket itself (the design flaw itself could lead
to a maintenance problem, but that's different :) ).

Of course, that assumes that the OP really means that he's got a TcpClient
in hand, and for some reason wants the IPEndPoint. But his original post
isn't very clear on which IPEndPoint he wants (local or remote), nor on
whether he wants it for the purpose of creating a TcpClient, or getting it
from a TcpClient.

It's possible there's a different, more appropriate answer to the question.

Pete
Oct 21 '08 #3
Peter Duniho wrote:
On Tue, 21 Oct 2008 10:00:55 -0700, Jeroen Mostert <jm******@xs4all.nl>
wrote:
>puzzlecracker wrote:
>>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.

--
J.
Oct 21 '08 #4
On Oct 21, 12:07 pm, puzzlecracker <ironsel2...@gmail.comwrote:
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
Oct 21 '08 #5
On Oct 21, 4:01*pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Oct 21, 12:07 pm, puzzlecracker <ironsel2...@gmail.comwrote:
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
Yea, I used Socket and found out the inf using
client.RemoteEndPoint.ToString();
Oct 21 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Alexander N. Spitzer | last post by:
If I have a machine with 3 virtual IP addresses (192.168.1.), how can I start 3 instances of the same RMI application (each started with different properties/configs), each listening on the port...
0
by: Johann Blake | last post by:
I am using the TcpClient to connect to a web site. I then open a NetworkStream and read the contents that are being sent back. The problem is that I have no idea when the remote host is finished...
6
by: MajorTom | last post by:
Hello, I getting this error An established connection was aborted by the software in your host machine here: TcpClient smtpSocket = new TcpClient(_serverSmtp, _portSmtp) port = 25 (smtp...
1
by: gallaczmit | last post by:
Will this code give me a true view of a computer's status? All I am looking for is to see if the computer is reachable or not. My end goal is to get a list of IP addresses from a MS SQL Server,...
2
by: jasonsgeiger | last post by:
From: "Factor" <jasonsgeiger@gmail.com> Newsgroups: microsoft.public.in.csharp Subject: Multiple Clients, One port Date: Wed, 19 Apr 2006 09:36:02 -0700 I'm been working with sockets for a...
5
by: zxo102 | last post by:
Hi, I am doing a small project using socket server and thread in python. This is first time for me to use socket and thread things. Here is my case. I have 20 socket clients. Each client send a...
1
by: LEM | last post by:
Hi all, When a client tries to connect to a server, a random port number is assigned to the client that will be used for the communication with the server (typically the next free port above...
0
by: Uncle Leo | last post by:
Hi, I've written an application that uses a TcpClient to listen on a port. Clients on other computers can then connect and start talking to each other. The TcpClient listens on port 47141....
15
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello everyone, I have this code for TCPListenPort. The code works fine, but my manager is asking me to establish multiple connections to the same port. How can i acheive that below is my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.