サーバーの切断に小さな問題があります(つまり、サーバーアプリケーションから切断したいときだけです(server.active = false)。これが私の簡単なコードです:
type
PClient = ^TClient;
type
TClient = record
Name: string;
AContext: TIdContext;
end;
clients: TThreadList;
SERVER: TIdTCPServer;
procedure TX.SERVERConnect(AContext: TIdContext);
var
NewClient: PClient;
s:string;
begin
s := AContext.Connection.socket.ReadLn();
GetMem(NewClient, SizeOf(TClient));
NewClient.name:=s;
NewClient.AContext := AContext;
AContext.data := TObject(NewClient);
try
clients.LockList.Add(NewClient);
finally
clients.UnlockList;
end;
AContext.Connection.socket.writeln('E:');//answer to client - "all right"
End;
procedure TX.SERVERDisconnect(AContext: TIdContext);
var
AClient: PClient;
begin
AClient := PClient(AContext.data);
try
clients.LockList.Remove(AClient);
finally
clients.UnlockList;
end;
FreeMem(AClient);
AContext.data := nil;
end;
クライアントにデータを送信する場合にのみ機能する必要があるため、onconnectプロシージャで1つのデータ行のみを読み取ります。これにはログイン名が含まれています。
私のコードでデータを送信する手順は次のようになります(それは良いですか?):
var
procedure TX.send(what: string; where: string);
i, ile: integer;
s: string;
Aclient: PClient;
list: tlist;
begin
list:= SERVER.Contexts.LockList;
try
for i := 0 to list.Count - 1 do
with TIdContext(list[i]) do
begin
AClient := PClient(data);
if where = ActClient^.name then
Connection.IOHandler.writeln(what);
end;
finally
SERVER.Contexts.UnlockList;
end;
end;
それはうまくいくように見えます-つまり。しかし、SERVER.active:= falseでサーバーを無効にしたい場合、アプリケーションがフリーズしますか?クライアントなどを解放しようとしましたが、悪いコードでは機能しません。
誰かが私を助けて、このコードのためにサーバーを停止する方法を教えてもらえますか?
Artik