c# - 可以获取非网络服务器的 Referrer 域名吗?

标签 c# tcpclient tcpserver

是否可以获得客户的推荐域名?我使用的来源:http://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C

示例代码如下:

服务器程序

使用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);
}    
}

}

/* 客户端程序 */ 使用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);
    }
}

}

我的观点是,让我们看看是否有3个域(server1.com、server2.com、server3.com)绑定(bind)到172.21.5.99,如何获取客户端连接的是哪个域?

想要执行如下操作:

假设客户端的rand为1,服务器端的结果为:

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

假设客户端的rand是2,服务器端的结果是:

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

假设客户端的rand是3,服务器端的结果是:

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

可以这样做吗?我被困在这里了。

最佳答案

不,这通常是不可能做到的。

HTTP 通过客户端在发送到服务器的请求中包含 Host: header 来解决此问题,以便服务器可以知道客户端打算连接到哪个主机名。如果没有这个,您只有一个到特定地址的传入 TCP 连接,而没有任何有关客户端如何获取该地址的其他信息。

关于c# - 可以获取非网络服务器的 Referrer 域名吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25277147/

相关文章:

Node.js:客户端在 TCP 连接关闭时不会死掉

c# - 使用 TcpClient 进行通信的更快方式?

C# - 删除 WPF DataGrid 中的右边缘行边框

c# - 删除行后 XtraGrid 行索引不匹配

android - 当服务器连接丢失时,如何自行停止和重启客户端的服务?

c# - 如何向服务器发送 "hello"并回复 "hi"?

c# - TCP 服务器和客户端 C# 最佳实践

c# - 在C#中杀死TCP服务器的TCP客户端线程

c# - 在一个 MVC View 中引用多个模型

c# - Task.WaitAll() 没有按预期工作