我是菜鸟,我是这样处理的[
]
procedure TDataServerThread.Execute;
var
Buffer : array[0 .. Buffer_length] of Char;
i:integer;
begin
pub_Edit_Recorder_count:=0;
pub_Insert_Recorder_count:=0;
Pub_FieldsList := TStringList.Create; //表头
fm_Main.UserCount.Caption:=IntToStr(StrToInt(fm_Main.UserCount.Caption)+1);
while not Terminated and ClientSocket.Connected do //线程循环,除非线程终止或客户断连接
try
pub_Stream := TWinSocketStream.Create(ClientSocket, 60000);
FillChar(Buffer, Buffer_length, 0);
i:=0;
try
if pub_Stream.WaitForData(WateFor_Time) then //等待WateFor_Time秒,读取数据。
begin
try
i:=Pub_Stream.Read(Buffer, Buffer_length);
except
Pub_LogStr:='不能读该用户数据,该用户可能已经断线了。';
Synchronize(WriteLog);
ClientSocket.Close;
end;
if i > 0 then { 如果能在WateFor_Time秒里读到数据 }
begin
try
ProcessTheRequest(Buffer) //自定义过程处理用户请求
except
pub_Logstr:='无名错误!!!';
Synchronize(WriteLog);
end;
end
else
ClientSocket.Close; { 关闭客户端连接}
end
else
ClientSocket.Close; { 60秒钟如客户无响应,KILL他的连接 }
finally
Pub_Stream.Free;
end;
except
HandleException;
Pub_FieldsList.Free;
end;
Pub_LogStr:='修改记录:'+Inttostr(pub_Edit_Recorder_count)+' 条,新增记录:'+Inttostr(pub_Insert_Recorder_count)+'条。';
Synchronize(WriteLog);
end;