TServerSocket的多线程 长连接下面的程序能实现吗?
我现在做的程序,会不停收到客户段重新连接上来的信息,
然后又重复发送数据给客户端,内存消耗会不断的加大,怎么解决这个问题
我的客户端数量最多80个,一般30个。
下面我贴出代码
inherited FreeOnTerminate := True;
CoInitialize(nil);
Query1 := TAdoquery.Create(nil);
Query2 := TAdoquery.Create(nil);
Query1.Connection := Form1.ADOConnection1;
Query2.Connection := Form1.ADOConnection1;
while not Terminated and ClientSocket.Connected do
try
SocketStream := TWinSocketStream.Create(ClientSocket, 60000);
try
FillChar(struPrisoner, SizeOf(TPrisoner), 0);
if SocketStream.Read(struPrisoner, SizeOf(TPrisoner)) = 0 then
begin
// If we didn't get any data after 60 seconds then close the connection
ClientSocket.Close;
Terminate;
end;
if ClientSocket.Connected then begin //正常的处理
sleep(1);
b := false;
//**************************** protocol ******************************************//
StrCopy(@ArrRoom, @struPrisoner.szRoom);
szPrison := ArrRoom;
if struPrisoner.iCommand = 7 then
begin
//查询数据库内容发送给客户端
Query1.Next;
end;
icommand := 17;
ClientSocket.SendBuf(icommand, SizeOf(integer)); end; if struPrisoner.iCommand = 14 then
begin
//查询数据库内容发送给客户端
ClientSocket.SendBuf(struPrisonInfo, sizeof(TPrisonInfo));
end;
{此处处理协议部分}
//**************************** protocol ******************************************//
if b = true then begin //接受close命令后 b=true
sleep(1);
ClientSocket.Close;
Terminate;
end;
end;
finally
SocketStream.Free;
end;
except
//HandleException;
ClientSocket.Close;
Query1.Free;
Query2.Free;
Terminate;
end;
CoUninitialize;
我现在做的程序,会不停收到客户段重新连接上来的信息,
然后又重复发送数据给客户端,内存消耗会不断的加大,怎么解决这个问题
我的客户端数量最多80个,一般30个。
下面我贴出代码
inherited FreeOnTerminate := True;
CoInitialize(nil);
Query1 := TAdoquery.Create(nil);
Query2 := TAdoquery.Create(nil);
Query1.Connection := Form1.ADOConnection1;
Query2.Connection := Form1.ADOConnection1;
while not Terminated and ClientSocket.Connected do
try
SocketStream := TWinSocketStream.Create(ClientSocket, 60000);
try
FillChar(struPrisoner, SizeOf(TPrisoner), 0);
if SocketStream.Read(struPrisoner, SizeOf(TPrisoner)) = 0 then
begin
// If we didn't get any data after 60 seconds then close the connection
ClientSocket.Close;
Terminate;
end;
if ClientSocket.Connected then begin //正常的处理
sleep(1);
b := false;
//**************************** protocol ******************************************//
StrCopy(@ArrRoom, @struPrisoner.szRoom);
szPrison := ArrRoom;
if struPrisoner.iCommand = 7 then
begin
//查询数据库内容发送给客户端
Query1.Next;
end;
icommand := 17;
ClientSocket.SendBuf(icommand, SizeOf(integer)); end; if struPrisoner.iCommand = 14 then
begin
//查询数据库内容发送给客户端
ClientSocket.SendBuf(struPrisonInfo, sizeof(TPrisonInfo));
end;
{此处处理协议部分}
//**************************** protocol ******************************************//
if b = true then begin //接受close命令后 b=true
sleep(1);
ClientSocket.Close;
Terminate;
end;
end;
finally
SocketStream.Free;
end;
except
//HandleException;
ClientSocket.Close;
Query1.Free;
Query2.Free;
Terminate;
end;
CoUninitialize;
解决方案 »
- BPL 如何调用主程序里面的全局变量
- 局域网聊天..客户端之间的通信出现了十分让人郁闷的问题..
- 麻烦问下:怎么知道自己在论坛里的可用分数?
- 急:向串口发送数据----在线等待--解决马上给分
- FastReport或ReportMachine 如何实现即打即停功能,请给出源码。感激!
- 用Delphi+Access的问题,进来看看===,在线,给分
- TreeNode颜色控制
- 重装系统后无法进入DATA MODULE
- 按下按钮相当于 按了 Ctrl+F3建 功能
- 100分请教高手
- pagecontrol的动态嵌套创建
- query 返回 多个结果,怎么汇总 到一个里变量,直接或间接的方法都可以,比方说query结果有只有3行呢?就一列的情况下
//
//动态创建释放建议参考如下代码
aQuery := TADOQuery.Create(nil)
try
//
finally
aQuery.Free;
end;