indy的idTcpServer WriteBuffer 有内存泄露?
结构体:
TCCPacked = record
lenLow: Byte;
lenHigh: Byte;
OPR: Byte;
ERR: Byte;
CMD: Byte;
end;
服务端代码:
procedure TServerFrmMain.ServerExecute(AThread: TIdPeerThread);
var
TCCPACK :TCCpacked;
size: Integer;
dataarray: array of byte;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
AThread.Connection.ReadBuffer (TCCPACK, SizeOf (TCCPacked));
size := makeword(tccpack.lenlow,tccpack.lenhigh);
setlength(dataarray,size-3); //获取长度
AThread.Connection.ReadBuffer (dataarray[0], size-3);
AThread.Connection.WriteBuffer (TCCPACK, SizeOf (TCCPacked), true); //注释掉这句话就不会内存泄露
end;
end;
客户端代码:
发送部分:
procedure TClientFrmMain.ButtonSendClick(Sender: TObject);
var
CommBlock : TCCpacked;
s : string;
sl : TstringList;
dataarray : array of byte;
i ,size: integer;
begin
CommBlock.OPR := $01;
CommBlock.ERR := $00;
CommBlock.CMD := strtoint(cbb_cmd.Text);
s := EditMessage.Text ;
sl := TstringList.Create;
Geteveryword(s,sl,' ');
setlength(dataarray,sl.Count);
for i := 0 to sl.Count -1 do begin
dataarray[i] := strtoint( sl.Strings[i]);
end;
size := sl.Count + 3;
sl.Free;
CommBlock.lenLow := Lo(size); // assign the data
CommBlock.lenHigh := Hi(size);
//先发送数据头
Client.WriteBuffer (CommBlock, SizeOf (CommBlock), true);
//再发送Data部分
Client.WriteBuffer (dataarray[0] , length (dataarray), true);
// Client.WriteBuffer (CommBlock, SizeOf (CommBlock), true);
end;
接收部分:
procedure TClientHandleThread.HandleInput;
begin
ClientFrmMain.mmo1.Lines.Add('Server Return: ' + inttostr(cb.ERR));
end;
procedure TClientHandleThread.Execute;
begin
while not Terminated do
begin
if not ClientFrmMain.Client.Connected then
Terminate
else
try
ClientFrmMain.Client.ReadBuffer(CB, SizeOf (CB));
Synchronize(HandleInput);
except
end;
end;
end;
解决重赏!!
相关推荐
indy控件的应用
indy的简单应用,研究 INDY控件的应用,服务端反传给客户端一个文本文件的DEMO
INDY控件使用指南
使用TIdAntiFreeze对抗“冻结” Indy使用一个特殊的组件TIdAntiFreeze来透明地解决客户程序用户界面“冻结”的问题。TIdAntiFreeze在Indy内部定时中断对栈的调用,并在中断期间调用Application.ProcessMessages方法处理消息,而外部的Indy调用继续保存阻塞状态,就好像TIdAntiFreeze对象不存在一样。你只要在程序中的任意地方添加一个TIdAntiFreeze对象,就能在客户程序中利用到阻塞式Socket的所有优点而避开它的一些显著缺点。 Indy使用了线程技术 阻塞式Socekt通常都采用线程技术,Indy也是如此。从最底层开始,Indy的设计都是线程化的。因此用Indy创建服务器和客户程序跟在Unix下十分相似,并且Delphi的快速开发环境和Indy对WinSock的良好封装使得应用程序创建更加容易。 Indy服务器模型 一个典型的Unix服务器有一个或多个监听进程,它们不停地监听进入的客户连接请求。对于每一个需要服务的客户,都fork一个新进程来处理该客户的所有事务。这样一个进程只处理一个客户连接,编程就变得十分容易。 Indy服务器工作原理同Unix服务器十分类似,只是Windows不像Unix那样支持fork,而是支持线程,因此Indy服务器为每一个客户连接分配一个线程。 图1显示了Indy服务器的工作原理。Indy服务器组件创建一个同应用程序主线程分离的监听线程来监听客户连接请求,对于接受的每一个客户,都创建一个新的线程来为该客户提供服务,所有与这一客户相关的事务都由该线程来处理。 使用组件TIdThreadMgrPool,Indy还支持线程池。 线程与Indy客户程序 Indy客户端组件并未使用线程。但是在一些高级的客户程序中,程序员可以在自定义的线程中使用Indy客户端组件,以使用户界面更加友好。
delphi IdTCPServer文件传输
delphi IdTCPServer文件传输 双向传输
delphi INDY控件使用指南
不错的Indy 资料。CSDN上有人上传过了。他说的十分。我下了,看了看写的不错,还整理了下。要想学习Indy9的话可以下来看看。
IdTCPClient和IdTCPServer传输字符串流WriteStream
IdTCPClient和IdTCPServer传输字符串流WriteStream


网络通信/分布式开发

