0

I am writing a simple FTP service using TcpListener and would like to find out what hostname (domain with multiple subdomains) the incoming call is using.

Is this possible?

//Barebone TCPListener example
System.Net.Sockets.TcpListener listener;
listener = new TcpListener(IPAddress.Any, 21)

listener.Start();

TcpClient client = listener.AcceptTcpClient();

//Great, incoming... what domain are they using to call my service?

//This only gives me the local and remote IP..
//IPEndPoint LocalEndPoint = (IPEndPoint)client.Client.LocalEndPoint;
//IPEndPoint RemoteEndPoint = (IPEndPoint)client.Client.RemoteEndPoint;

Any pointers greatly appreciated.

Matt Davis
  • 45,297
  • 16
  • 93
  • 124
Thomas C
  • 27
  • 5

1 Answers1

3

I don't think TCP fundamentally has that information. That's why HTTP has the "Host" header so clients can specify it.

Essentially, making a TCP connection to a host name is equivalent to resolving the host name into an address, then making the connection as if there'd never been a name.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • So the information about the hostname is lost somewhere on the way? – Thomas C Oct 03 '12 at 10:53
  • @ThomasC: It's lost in that step 1 is "resolve host name to address" and step 2 is "connect to address". So at the *TCP* level the information is gone. I believe some FTP servers support a non-standard "Host" header, so you could do that *within your server code* but it's not part of `TcpClient`. It's in the higher level protocol. – Jon Skeet Oct 03 '12 at 10:57
  • Would the solution be to use Socket instead of TCP? – Thomas C Oct 03 '12 at 11:01
  • @ThomasC: No - it's still TCP, fundamentally. The real solution would be to use different IP addresses for different hosts... – Jon Skeet Oct 03 '12 at 11:08
  • ok, thanks. A bit wiser now, I wont try to find what is not there. Still wonder though, how it can be done :) – Thomas C Oct 03 '12 at 11:15
  • 1
    @ThomasC: Fundamentally, it can't. Suppose you're living in a house with several people, and you're all listed in the telephone directory. I look up the number of one of those people, then dial your phone number. Unless I then ask for the person, how would you expect to know who I looked up? It's fundamentally the same sequence of operations here. – Jon Skeet Oct 03 '12 at 11:18