gpt4 book ai didi

delphi - 如何区分来自具有相同 IP 地址的多个客户端的连接?

转载 作者:行者123 更新时间:2023-12-03 15:47:56 25 4
gpt4 key购买 nike

如果客户端具有相同的 IP 和端口,识别客户端的正确方法是什么?如果它们仅通过 LAN 连接,例如ip:198.162.1.1 端口:2015。如果它们具有相同的 IP,如何使用其唯一 ID 检测哪个客户端已断开连接?

TClient = class(TIdServerContext)
private
public
PeerIP : String;
procedure SendMessage(cIP, mStr : String);
end;

procedure TClient.SendMessage(cIP, mStr : String);
var
Context: TClient;
List: TList;
I: Integer;
begin
List := Form1.IdTCPServer1.Contexts.LockList;
try
for I := 0 to List.Count-1 do
begin
Context := TClient(List[I]);
if (Context.PeerIP = cIP) then
begin
Connection.IOHandler.WriteLn(mStr);
Break;
end
end;
finally
Form1.IdTCPServer1.Contexts.UnlockList;
end;
end;

我只存储客户端 IP 并将其用作 ID。

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
begin
with TClient(AContext) do
begin
if AContext.Connection.Connected then
begin
PeerIP := Connection.Socket.Binding.PeerIP;
end;
end;
end;

也许像ClientID := Connection.Socket.Binding.Handle;

procedure TForm1.IdTCPServer1Disconnect(AContext: TIdContext);
begin
//Connection.Socket.Binding.Handle; ??
end;

最佳答案

PeerIP 本身不足以唯一地识别客户端。想象一下当路由器一侧运行的多个客户端连接到路由器另一侧运行的同一服务器时会发生什么。从服务器的角度来看,客户端将具有相同的 PeerIP(路由器的 IP)。您需要将每个客户端的 PeerIP 和 PeerPort 放在一起。

  TClient = class(TIdServerContext)
public
PeerIP : String;
PeerPort : TIdPort;
procedure SendMessage(cIP: string; cPort: TIdPort; mStr : String);
end;

procedure TClient.SendMessage(cIP: string; cPort: TIdPort; mStr : String);
var
Context: TClient;
List: TList;
I: Integer;
begin
List := Form1.IdTCPServer1.Contexts.LockList;
try
for I := 0 to List.Count-1 do
begin
Context := TClient(List[I]);
if (Context <> Self) and (Context.PeerIP = cIP) and (Context.PeerPort = cPort) then
begin
Context.Connection.IOHandler.WriteLn(mStr);
Break;
end
end;
finally
Form1.IdTCPServer1.Contexts.UnlockList;
end;
end;

procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
with TClient(AContext) do
begin
PeerIP := Connection.Socket.Binding.PeerIP;
PeerPort := Connection.Socket.Binding.PeerPort;
end;
end;

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
begin
...
end;

或者根本不依赖 IP/端口。编写自己的唯一ID,例如要求每个客户端都使用UserID登录服务器。

  TClient = class(TIdServerContext)
public
UserID : String;
procedure SendMessage(cUser, mStr : String);
end;

procedure TClient.SendMessage(cUser, mStr : String);
var
Context: TClient;
List: TList;
I: Integer;
begin
List := Form1.IdTCPServer1.Contexts.LockList;
try
for I := 0 to List.Count-1 do
begin
Context := TClient(List[I]);
if (Context <> Self) and (Context.UserID = cUser) then
begin
Context.Connection.IOHandler.WriteLn(mStr);
Break;
end
end;
finally
Form1.IdTCPServer1.Contexts.UnlockList;
end;
end;

procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
with TClient(AContext) do
begin
// this is just for demonstration. Obviously, you
// should implement a real protocol with authentication,
// duplicate login detection, etc...
UserID := Connection.IOHandler.ReadLn;
end;
end;

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
begin
...
end;

关于delphi - 如何区分来自具有相同 IP 地址的多个客户端的连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28824410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com