大家帮我看看这一个问题是怎么处理的
原来的传输方法是用结构体胡静态数组,可是我想帮它转换成动态数组是要怎么转换?这个是原来的代码
===========================================================
type //发送包定义
PTMsgPack = ^TMsgPack;
TMsgPack = packed record
CMD : integer; //包类型
Content: Array[0..5000] of char; //包内容
end;
=============================================================客户端发送===========================================
StrPCopy(MsgPack.Content,Str);
Buffer := RawToBytes(MsgPack, sizeof(MsgPack));
IdTCPClient1.IOHandler.Write(Buffer);
============================================================服务端接收===========================================
AContext.Connection.IOHandler.ReadBytes(Buffer,sizeof(MsgPack));
BytesToRaw(Buffer,MsgPack, sizeof(MsgPack));
S:=string(MsgPack.Content);
========================================================如果帮结构体改成
type //发送包定义
PTMsgPack = ^TMsgPack;
TMsgPack = packed record
CMD : integer; //包类型
Content: Array of char; //包内容
end; 这样是要怎么做呢?
原来的传输方法是用结构体胡静态数组,可是我想帮它转换成动态数组是要怎么转换?这个是原来的代码
===========================================================
type //发送包定义
PTMsgPack = ^TMsgPack;
TMsgPack = packed record
CMD : integer; //包类型
Content: Array[0..5000] of char; //包内容
end;
=============================================================客户端发送===========================================
StrPCopy(MsgPack.Content,Str);
Buffer := RawToBytes(MsgPack, sizeof(MsgPack));
IdTCPClient1.IOHandler.Write(Buffer);
============================================================服务端接收===========================================
AContext.Connection.IOHandler.ReadBytes(Buffer,sizeof(MsgPack));
BytesToRaw(Buffer,MsgPack, sizeof(MsgPack));
S:=string(MsgPack.Content);
========================================================如果帮结构体改成
type //发送包定义
PTMsgPack = ^TMsgPack;
TMsgPack = packed record
CMD : integer; //包类型
Content: Array of char; //包内容
end; 这样是要怎么做呢?
解决方案 »
- ADOQuery导致内存溢出
- mid子窗口的排列与调用,即把已经存在的10个mid子窗口之中一个调到最前面来,怎么写?
- 硬盘号,网卡号,CPU号,字符串加密
- 1000分征集解决方案(关于MIDAS & WIN2000 -> SOLARIS)!
- 我的机子怎么啦,中毒了吗?
- delphi dbgrid按条件求和有一个问题
- 字符串问题
- createsemaphorek跟ReleaseSemaphore的问题
- 大家好!有多余的OICQ给小弟一个好吗?
- 简单索引使用问题
- TComboBox 默认下拉
- 求助!使用listview的virtual功能显示数据库数据时 在listview中移动鼠标的话 显示的数据会自己变化 最后所有的数据都变成一样的了 该如何解决
PTMsgPack = ^TMsgPack;
TMsgPack = packed record
CMD : integer; //包类型
CSize : Integer; //Array Size
Content: Array of char; //包内容
end;When you call SetLength on Contents, you should set the length to CSize member.
2. Another approach could be to use streamsprocedure Send(CMD : Integer) // Just for sample
var
stream : TStringStream;
begin
stream := TStringStream.Create;
try
//Do your text handling here and once done
IdTCPClient1.WriteInteger(CMD);
IdTCPClient1.WriteInteger(stream.Size);
IdTCPClient1.OpenWriteBuffer;
IdTCPClient1.WriteStream(stream);
IdTCPClient1.CloseWriteBuffer;
finally
FreeAndNil(stream);
end;
......
end;
...
Hope it helps.//Ali
PTMsgPack = ^TMsgPack;
TMsgPack = packed record
CMD : integer; //包类型
CSize : Integer; //Array Size
Content: Array of char; //包内容
end;如果用上面的这种方法,后面的发送和接收要怎么写呢?
我试了好多写法,可是一读地址就出现内存错误,能不能给一个示例呢?
Content: Array of char;
是个指针。这个Content成员用之前你需要分配内存(SetLength 函数)。为了避免内存异常你需要先从Socket上读取Content的大小
Size=.......
SetLength(MsgPack.Content,Size);
AContext.Connection.IOHandler.ReadBytes(Buffer,sizeof(MsgPack));
为什么不想用Stream的理由是什么?//Ali