1,316
社区成员
发帖
与我相关
我的任务
分享
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
int buf_len = 0 ;
buf_len =tlist->Count*sizeof(Infor);
ShowMessage(buf_len);
ClientSocket1->Socket->SendBuf(tlist,buf_len);//
}
Infor的定义
struct Infor{
char name[40];
char version[10];
char publish[10];
};
tlist是客户端的一TList类型的全局变量,里面有一些Infor对象。
服务器端的接收:
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
int rec_size = Socket->ReceiveLength(); //
ShowMessage(rec_size);
char *buf ; //定义接收的缓存,不知可不可以这样定义 ,如果 用 TList * buf ; int temp = Socket->ReceiveBuf(buf,rec_size); 就会报错
buf = new char[rec_size];
int temp = Socket->ReceiveBuf(buf,rec_size); //
ShowMessage(temp); //接收到的数据长度
memcpy(tlist,buf,rec_size); // 这样拷贝 不知可不可以?
Infor *temp_infor =NULL ;
for(int i=0;i<tlist->Count;i++){//取得 tlist中的每一个Infor 对象
temp_infor = (Infor*)tlist->Items[0]; // 这里出问题 ???
ShowMessage(temp_infor->name) ;
}
}
tlist也为一TList 对象,服务器端运行接收数据的时候,这行 “ temp_infor = (Infor*)tlist->Items[0]; ”总报错,说读内存错误,应该是没有把TList类型读出来,传过来的数据长度都是正确的。
希望高手指点,谢谢。
TList *pList=new TList();
const int send_buf_len =tlist->Count*sizeof(Infor);
const int recv_buf_len=send_buf_len;//(好像有个实际的发送长度,具体你看下)
int temp = Socket->ReceiveBuf(pList,recv_buf_len);