我将我的接收数据的源码贴出来:
//**--监听客户端请求
procedure TNtxServerFrm.IdTCPServerExecute(AThread: TIdPeerThread);
var
sReadCmd,sCommand:String;
sSoftRegCode,sSoftRegPwd:String;
ResultValue:Boolean;
AQryCompany:TADOQuery;
ACmdCompany:TADOCommand; //更新资料
iRkey:Integer;
begin
try
//开始接收客户端请求的命令
sReadCmd:=Trim(AThread.Connection.ReadLn);
sCommand:=Copy(sReadCmd,1,6);
sSoftRegCode:=Copy(sReadCmd,8,16);
sSoftRegPwd:=Copy(sReadCmd,25,15);
AQryCompany:=TADOQuery.Create(AThread.Connection);
AQryCompany.Connection:=ADOCN;
AQryCompany.SQL.Text:='Select Company.Rkey,Company.isStatus, ' +
'Company.CanSendSmsQty + Company.DayPreSentSmsQty as CanSendSmsQty ' +
'From Company ' + 'Where SoftRegCode=' + QuotedStr(sSoftRegCode) +
' and SoftRegPwd=' + QuotedStr(sSoftRegPwd);
AQryCompany.Active:=True;
if AQryCompany.RecordCount<>1 then
begin
AQryCompany.Close;
AQryCompany.Free;
AQryCompany:=nil;
AThread.Connection.WriteLn('[NORG]'); //传送没有注册命令
end
else
begin
iRkey:=AQryCompany.FieldByName('Rkey').AsInteger;
AQryCompany.Close;
AQryCompany.Free;
AQryCompany:=nil;
if sCommand='--RM--' then //接收信息
begin
AThread.connection.WriteLn('[RMSC]');
ResultValue:=FReceiveSms(Athread,iRkey); //送线程和企业表中RKEY
End
else if ............其他请求....
except
on E:Exception do
LBLog.Items.Add(DateTimeToStr(now)+Athread.Connection.Socket.Binding.PeerIP + ' Y.接收客户端请求错误: '+E.Message);
end;
try
if AThread.Terminated=False then
begin
AThread.Terminate;
AThread.FreeOnTerminate:=True;
if Athread.Connection.Socket.Binding.PeerIP<>IdTCPClient.Host then
LBlog2.Items.Add(DateTimeToStr(now) + ' ' + Athread.Connection.Socket.Binding.PeerIP + ' 成功中断');
end;
except
on E:Exception do
LBLog.Items.Add(DateTimeToStr(now) + ' ' + Athread.Connection.Socket.Binding.PeerIP + ' Z.接收客户端请求错误: '+E.Message);
end;
end;
//**--客户端接收信息
function TNtxServerFrm.FReceiveSms(AThread:TIdPeerThread;iRkey:Integer):Boolean;
type //请求记录变量
MyRecord=Packed Record
CurRec:Integer;
TotRec:Integer;
MobileNo:String[15];
Content:String[180];
MobiSendTime:TdateTime;
ReceTime:TdateTime;
isYuE:String[1];
end;
type //接收记录变量
MyRecRecord=Packed Record
Flag:String[1];
AutoReturn:String[1];
MobileNo:String[15];
Content:String[180];
end;
var
MySendingRecord:MyRecord;
MyReceivingRecord:MyRecRecord;
AQryReceSms:TADOQuery;
AQryCompany:TADOQuery;
ACmdReceSms:TADOCommand;
ACmdSendSms:TADOCommand;
ACmdCompany:TADOCommand;
iCurRec,iTotRec:Integer;
begin
//建立查询控件
AQryReceSms:=TADOQuery.Create(AThread.Connection);
AQryReceSms.Connection:=ADOCN;
AQryCompany:=TADOQuery.Create(AThread.Connection);
AQryCompany.Connection:=ADOCN;
ACmdReceSms:=TADOCommand.Create(AThread.Connection);
ACmdReceSms.Connection:=ADOCN;
ACmdSendSms:=TADOCommand.Create(AThread.Connection);
ACmdSendSms.Connection:=ADOCN;
ACmdCompany:=TADOCommand.Create(AThread.Connection);
ACmdCompany.Connection:=ADOCN;
//查询企业
AQryCompany.Close;
AQryCompany.SQL.Text:='SELECT Rkey,DayPresentSmsQty, ' +
'CanSendSmsQty+DayPresentSmsQty as CanSendSmsQty ' +
'From Company Where Rkey= ' + IntToStr(iRkey);
//查询未接收的信息
AQryReceSms.Close;
AQryReceSms.SQL.Text:='Select ReceSms.* ' +
'From ReceSms ' +
'Where ReceSms.Company_Rkey = ' + IntToStr(iRkey) +
' and (Flag is null or Flag<>''3'' ) ';
AQryReceSms.Active:=True;
iCurRec:=1;
iTotRec:=AQryReceSms.RecordCount;
try
if AQryReceSms.RecordCount>0 then
begin
AQryReceSms.First;
while AQryReceSms.Eof=False do
begin
MySendingRecord.CurRec:=iCurRec; //当前记录
MySendingRecord.TotRec:=iTotRec; //总记录
MySendingRecord.MobileNo:=AQryReceSms.FieldByName('MobileNo').AsString;
MySendingRecord.Content:=AQryReceSms.FieldByName('Content').AsString;
MySendingRecord.MobiSendTime:=AQryReceSms.FieldByName
('MobiSendTime').AsDateTime;
MySendingRecord.ReceTime:=Now; //客户端接收时间
MySendingRecord.isYuE:='0';
AThread.Connection.OpenWriteBuffer(-1);
AThread.connection.WriteBuffer(MySendingRecord,SizeOf
(MySendingRecord),True);
AThread.Connection.CloseWriteBuffer;
//将接收信息 插入 到 ReceSmsSuc 表中
ACmdReceSms.CommandText:='Insert into ReceSmsSuc (MobileNo,Content,ClientReceTime,MobiSendTime,FispNo,Company_Rkey,Flag) ' +
'Values
ParMobileNo,
arContent,GetDate(),
arMobiSendTime,
arFispNo,
arCompany_Rkey,'+'''3'''+ ') ' ;
With ACmdReceSms do
begin
Parameters.ParamByName('ParMobileNo').Value:=AQryReceSms.FieldByName('MobileNo').AsString;
Parameters.ParamByName('ParContent').Value:=AQryReceSms.FieldByName('Content').AsString;
Parameters.ParamByName('ParMobiSendTime').Value:=AQryReceSms.FieldByName('MobiSendTime').AsDateTime;
Parameters.ParamByName('ParFispNo').Value:=AQryReceSms.FieldByName('FispNo').AsString;
Parameters.ParamByName('ParCompany_Rkey').Value:=AQryReceSms.FieldByName('Company_Rkey').AsInteger;
end;
ACmdReceSms.Execute;
//删除该笔已接收的资料
ACmdReceSms.CommandText:='Delete From ReceSms '+
'Where Rkey = ' + AQryReceSms.FieldByName('Rkey').AsString;
ACmdReceSms.Execute;
//接收Client请求命令
AThread.Connection.ReadBuffer(MyReceivingRecord,SizeOf(MyReceivingRecord));
AQryReceSms.Next;
iCurRec:=iCurRec + 1; //初步累计
end;
MySendingRecord.CurRec:=0; //当前记录
MySendingRecord.TotRec:=iTotRec; //总记录
AThread.Connection.OpenWriteBuffer(-1);
AThread.connection.WriteBuffer(MySendingRecord,SizeOf(MySendingRecord),True);
AThread.Connection.CloseWriteBuffer;
end
else
begin
MySendingRecord.CurRec:=0; //当前记录
MySendingRecord.TotRec:=iTotRec; //总记录
AThread.Connection.OpenWriteBuffer(-1);
AThread.connection.WriteBuffer(MySendingRecord,SizeOf(MySendingRecord),True);
AThread.Connection.CloseWriteBuffer;
end;
except
on E:Exception do
LBLog.Items.Add(DateTimeToStr(now) + ' ' + Athread.Connection.Socket.Binding.PeerIP + ' 接收信息错误: ' + E.Message);
end;
try
AQryReceSms.Close;
AQryReceSms.Free;
AQryReceSms:=nil;
AQryCompany.Close;
AQryCompany.Free;
AQryCompany:=nil;
ACmdReceSms.Free;
ACmdReceSms:=nil;
ACmdSendSms.Free;
ACmdSendSms:=nil;
ACmdCompany.Free;
ACmdCompany:=nil;
except
on E:Exception do
LBLog.Items.Add(DateTimeToStr(now) + ' ' + Athread.Connection.Socket.Binding.PeerIP + ' 释放变量错误: ' + E.Message);
end;
Result:=True;
end;
//**--企业接收信息---End
//释放内存,每2分钟执行一次
procedure TNtxServerFrm.TimerClearMemTimer(Sender: TObject);
begin
try
SetProcessWorkingSetSize(GetCurrentProcess,$FFFFFF,$FFFFFF);
application.ProcessMessages;
sleep(800);
LbLog.Items.Add(DateTimeToStr(Now) + ' 释放内存成功...!!');
except
on E:Exception do
begin
LbLog.Items.Add('释放内存错误:' + E.Message);
end;
end;
end;