2

Is it possible to get the client's referrer domain name? I used source: http://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C

Example codes are below:

Server Program

use ip: 172.21.5.99 (server1.com, server2.com, server3.com)

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

public class serv {
public static void Main() {
try {
    IPAddress ipAd = IPAddress.Any;
     // use local m/c IP address, and 
     // use the same in the client

/* Initializes the Listener */
    TcpListener myList=new TcpListener(ipAd,8001);

/* Start Listeneting at the specified port */        
    myList.Start();

    Console.WriteLine("The server is running at port 8001...");    
    Console.WriteLine("The local End point is  :" + 
                      myList.LocalEndpoint );
    Console.WriteLine("Waiting for a connection.....");

    Socket s=myList.AcceptSocket();
    Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

    byte[] b=new byte[100];
    int k=s.Receive(b);
    Console.WriteLine("Recieved...");
    for (int i=0;i<k;i++)
        Console.Write(Convert.ToChar(b[i]));

    ASCIIEncoding asen=new ASCIIEncoding();
    s.Send(asen.GetBytes("The string was recieved by the server."));
    Console.WriteLine("\nSent Acknowledgement");
/* clean up */            
    s.Close();
    myList.Stop();

}
catch (Exception e) {
    Console.WriteLine("Error..... " + e.StackTrace);
}    
}

}

/* Client Program */ use ip 231.21.5.1

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;


public class clnt {

public static void Main() {
    Random rnd = new Random();
    int rand = rnd.Next(1, 4);
    try {
        TcpClient tcpclnt = new TcpClient();
        Console.WriteLine("Connecting.....");


        if (rand == 1)
          tcpclnt.Connect("server1.com",8001);
        if (rand == 2)
          tcpclnt.Connect("server2.com",8001);
        if (rand == 3)
          tcpclnt.Connect("server3.com",8001);
        // use the ipaddress as in the server program but random hostname

        Console.WriteLine("Connected");
        Console.Write("Enter the string to be transmitted : ");

        String str=Console.ReadLine();
        Stream stm = tcpclnt.GetStream();

        ASCIIEncoding asen= new ASCIIEncoding();
        byte[] ba=asen.GetBytes(str);
        Console.WriteLine("Transmitting.....");

        stm.Write(ba,0,ba.Length);

        byte[] bb=new byte[100];
        int k=stm.Read(bb,0,100);

        for (int i=0;i<k;i++)
            Console.Write(Convert.ToChar(bb[i]));

        tcpclnt.Close();
    }

    catch (Exception e) {
        Console.WriteLine("Error..... " + e.StackTrace);
    }
}

}

My point is, let see if there are 3 domains (server1.com, server2.com, server3.com) are BINDED to 172.21.5.99, how to get which domain is the client connected with?

Wanting to do like below:

Assume client's rand is 1, result in the server-side will be :

The server is running at port 8001...
Waiting for a connection.....
Connection accepted from 231.21.5.1 through our server1.com.

Assume client's rand is 2, result in the server-side will be :

The server is running at port 8001...
Waiting for a connection.....
Connection accepted from 231.21.5.1 through our server2.com.

Assume client's rand is 3, result in the server-side will be :

The server is running at port 8001...
Waiting for a connection.....
Connection accepted from 231.21.5.1 through our server3.com.

Is it possible to do so? Im stuck here.

1 Answers1

2

No, this is not possible to do in general.

HTTP solves this problem by the client including a Host: header in the request sent to the server, so the server can tell which hostname the client intends to connect to. Without this, you just have an incoming TCP connection to a specific address without any other information about how the client got the address.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • thank you for your response. yes, generally its not possible, just wondering if there's another way around, but thank you anyway – Marhazk Spaika Aug 13 '14 at 03:02