【发布时间】:2014-01-22 05:51:05
【问题描述】:
TIdTCPServer 组件有问题。我用它来读取远程服务器发送的数据。
下面是我使用的代码:
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
const
START_PACKET = #11;
END_PACKET = #10;
var
IO : TIdIOHandler;
c : Char;
a : AnsiString;
begin
a := '';
IO := AContext.Connection.IOHandler;
while (IO.InputBuffer.Size > 0) do
begin
c := IO.ReadChar;
if c = START_PACKET then
begin
repeat
c := IO.ReadChar; //(TEncoding.ASCII);
a := a + c;
until (c = END_PACKET) or (IO.InputBufferIsEmpty);
end;
end;
if a <> '' then
begin
//let's send replay to server
IO.Write(CreateReply(a));
//now we need to save what we received to database
//I use critical section
try
EnterCriticalSection(LockDB);
with DataModule2.results do
begin
Close;
Params[0].AsDateTime := Today;
Params[1].AsString := a;
ExecSQL;
end;
finally
LeaveCriticalSection(LockDB);
end;
end;
end;
问题是,一旦我的TIdTCPServer 获取了一些数据,它就会启动一个无限循环并占用 100% 的 CPU。
我在这里做错了什么?
【问题讨论】: