简体   繁体   English

some.networks 中的“Socket.RemoteHost”返回 IP 而不是 ComputerName - Delphi

[英]"Socket.RemoteHost" in some networks returns IP instead of ComputerName - Delphi

I'm using "OnClientConnect" of TServerSocket to identify when a client connects to chat, and save the ComputerName of the client in a listbox to manage sending messages between clients.我正在使用 TServerSocket 的“OnClientConnect”来识别客户端何时连接到聊天,并将客户端的 ComputerName 保存在列表框中以管理客户端之间的消息发送。

The code is like this:代码是这样的:

    procedure TfrmAnaForm.ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
    var 
       ComputerName: string;
    begin    
        frmChatMain.lstChatUsers.Items.Add(Socket.RemoteHost);    
    end;

The problem is that Normally "Socket.RemoteHost" returns "ComputerName" of the client, but in some.networks, the code "Socket.RemoteHost" returns IP of client instead of "ComputerName" of client.问题是通常“Socket.RemoteHost”返回客户端的“ComputerName”,但在some.networks中,代码“Socket.RemoteHost”返回客户端的IP而不是客户端的“ComputerName”。

The RemoteHost property retrieves the client's IP address from the socket, and then performs a reverse DNS lookup of the client's hostname for that IP. If that lookup fails, RemoteHost returns a blank string, not the IP address. RemoteHost属性从套接字中检索客户端的 IP 地址,然后针对该 IP 执行客户端主机名的反向 DNS 查找。如果该查找失败, RemoteHost将返回一个空字符串,而不是 IP 地址。 The only way RemoteHost can return an IP address is if that is what the DNS system actually reported. RemoteHost可以返回 IP 地址的唯一方法是如果这是 DNS 系统实际报告的地址。

You should not be using the RemoteHost to uniquely identify clients, because 1) it is not guaranteed to even give you a value, and 2) it is not guaranteed to be unique, such as if multiple remote clients are connecting from the same computer.network.您不应该使用RemoteHost来唯一标识客户端,因为 1) 它甚至不能保证给您一个值,并且 2) 它不能保证是唯一的,例如多个远程客户端从同一台计算机连接。网络。 At the very least, you must use RemoteIP + RemotePort instead of RemoteHost to identify individual connections.至少,您必须使用RemoteIP + RemotePort而不是RemoteHost来识别各个连接。 Though, you really should be using the TCustomWinSocket object itself to identify unique connections.不过,您确实应该使用TCustomWinSocket object 本身来识别唯一连接。 Even better, require clients to login to your server with a unique ID, which you can then store inside the TCustomWinSocket.Data property so it follows the connection it belongs to.更好的是,要求客户端使用唯一 ID 登录到您的服务器,然后您可以将其存储在TCustomWinSocket.Data属性中,以便它遵循它所属的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM