可以获取非网络服务器的推荐人域名吗?

时间:2014-08-13 02:32:46

标签: 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)被绑定到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.

有可能这样做吗?我被困在这里。

1 个答案:

答案 0 :(得分:1)

不,一般情况下不可能这样做。

HTTP解决了客户端在发送给服务器的请求中包含Host:标头的问题,因此服务器可以告诉客户端打算连接到哪个主机名。如果没有这个,您只需要一个到特定地址的传入TCP连接,而不需要有关客户端如何获取地址的任何其他信息。