“Socket.RemoteHost”在某些网络中返回 IP 而不是 ComputerName - Delphi
我使用 TServerSocket 的“OnClientConnect”来识别客户端何时连接到聊天,并将客户端的 ComputerName 保存在列表框中以管理客户端之间的消息发送。
代码是这样的:
procedure TfrmAnaForm.ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
var
ComputerName: string;
begin
frmChatMain.lstChatUsers.Items.Add(Socket.RemoteHost);
end;
问题是,通常“Socket.RemoteHost”返回客户端的“ComputerName”,但在某些网络中,代码“Socket.RemoteHost”返回客户端的IP而不是客户端的“ComputerName”。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RemoteHost
属性从套接字检索客户端的 IP 地址,然后对该 IP 的客户端主机名执行反向 DNS 查找。如果查找失败,RemoteHost
将返回空字符串,而不是 IP 地址。RemoteHost
可以返回 IP 地址的唯一方法是 DNS 系统是否实际报告了该地址。您不应该使用
RemoteHost
来唯一标识客户端,因为 1) 它甚至不能保证为您提供一个值,2) 不能保证它是唯一的,例如多个远程客户端从同一计算机/网络进行连接。至少,您必须使用RemoteIP
+RemotePort
而不是RemoteHost
来识别各个连接。不过,您确实应该使用TCustomWinSocket
对象本身来识别唯一的连接。更好的是,要求客户端使用唯一的 ID 登录到您的服务器,然后您可以将其存储在TCustomWinSocket.Data
属性中,以便它遵循其所属的连接。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. The only wayRemoteHost
can return an IP address is if that is what the DNS system actually reported.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. At the very least, you must useRemoteIP
+RemotePort
instead ofRemoteHost
to identify individual connections. Though, you really should be using theTCustomWinSocket
object itself to identify unique connections. Even better, require clients to login to your server with a unique ID, which you can then store inside theTCustomWinSocket.Data
property so it follows the connection it belongs to.