3
识别客户端是否具有相同的IP和端口的正确方法是什么?如果它们仅通过LAN连接,例如ip:198.162.1.1 port:2015.如果他们具有相同的IP,如何使用其唯一ID检测哪个客户端已断开连接?如何区分具有相同IP地址的多个客户端的连接?
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;