作者: 110302124 时间: 2015-6-16 14:00
这是蓝牙线程中开启的等待客户端连接的代码
procedure TServerConnectionTH.Execute;
var
str,Msg: string;
err:boolean;
label reconn;
begin
try
reconn:
ClientSocket := nil;
err:=false;
while not Terminated and (ClientSocket = nil) do
begin
self.parent.addlog('等待连接...');
try
ClientSocket := FServerSocket.Accept();
except
on E : Exception do
begin
parent.addlog('err:'+e.Message);
end;
end;
self.parent.addlog('连接成功...');
end;
self.parent.addlog('收到clientsocket');
if(ClientSocket <> nil) then
begin
while not Terminated do
begin
try
self.parent.addlog('读取数据...');
FData := ClientSocket.ReadData;
self.parent.addlog('读到数据'+inttostr(length(FData)));
except
err:=true;
self.parent.addlog('读数据异常!');
ClientSocket.DisposeOf();
break;
end;
if length(FData) > 0 then
begin
str:=TEncoding.UTF8.GetString(FData);
parent.revstrs.Add(str);
end;
sleep(100);
end;
end;
if err and not terminated then
begin
parent.addlog('异常重连');
goto reconn;
end;
except
on E : Exception do
begin
Msg := E.Message;