IndyServer为每个客户端建立一个线程,然后在线程里read数据。
我们一般这样写服务器端的接收程序:
if not AThread.Terminated and AThread.Connection.Connected then //注意这里
begin
AThread.Connection.ReadBuffer(Command,SizeOf(TCommand));
if Command='1' then
……
注意我标注的地方:
if not AThread.Terminated and AThread.Connection.Connected then //注意这里
程序再运行到这里的时候,AThread.Connection.Connected 已经为false,则线程自动退出了,连接断开了。
示例:
if not AThread.Terminated and AThread.Connection.Connected then //注意这里
begin
AThread.Connection.ReadBuffer(Command,SizeOf(TCommand));
if Command='Server Quit' then
begin
Command := 'Client Quit';
AThread.Connection.WriteBuffer(Command,Sizeof(Command));
AThread.Connection.Disconnect;
end;