Is it possible to retrieve the underlying hostname/port from a new TcpClient?
TcpListener listener = new TcpListener(IPAddress.Any, port);
TcpClient client = listener.AcceptTcpClient();
// get the hostname
// get the port
I've routed around in client.Client
(a System.Net.Socket
), but can't find anything out in there either. Any ideas?
Thanks all.
The TcpClient class provides simple methods for connecting, sending, and receiving stream data over a network in synchronous blocking mode. In order for TcpClient to connect and exchange data, a TcpListener or Socket created with the TCP ProtocolType must be listening for incoming connection requests.
SIP server, and client-program. The SIP server may use port 5010 to listen and establish multiple connections. The client connects from his pc to the server via port 5010. When a packet is received on that port, an event must be triggered on the client PC.
The TcpListener class provides simple methods that listen for and accept incoming connection requests in blocking synchronous mode. You can use either a TcpClient or a Socket to connect with a TcpListener. Create a TcpListener using an IPEndPoint, a Local IP address and port number, or just a port number.
A TCP listener provides TCP server socket support at a specific port within the node. The socket will accept connections and receive messages from a TCP client application. The TCP client application will send messages to the TCP listener in an XML format and ASCII delimited messages.
Untested, but I would try the following:
TcpListener listener = new TcpListener(IPAddress.Any, port);
TcpClient client = listener.AcceptTcpClient();
IPEndPoint endPoint = (IPEndPoint) client.Client.RemoteEndPoint;
// .. or LocalEndPoint - depending on which end you want to identify
IPAddress ipAddress = endPoint.Address;
// get the hostname
IPHostEntry hostEntry = Dns.GetHostEntry(ipAddress);
string hostName = hostEntry.HostName;
// get the port
int port = endPoint.Port;
If you can make do with the IPAddress, I would skip the reverse DNS-lookup, but you specifically asked for a hostname.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With