目前有这样一个项目。我用delphi开发前台,别人在外地用c开发后台(服务器端)。
前台和后台的通讯有这样一个问题请教。
   客户端程序用idTCPclient,发送一个结构体数据给服务器端,例如
type
  Tcommad = record
         commandid:integer;
         command :string;
         date1:string;
         date2:string;
end代码如下procedure TForm1.button1click(sender:Tobject);
var clientcommand:Tcommand;
begin
  clientcommand.id:=1001;
  clientcommand.command:='delete'
  date1:='a';
  date2:='b';
  idtcpclient1.connect();
  idTcpclient.WriteBuffer(clientcommand,sizeof(clientcommand),True);
  idTcpclient.Disconnect;
end;服务器端收到我的命令也回复同样结构的一个结构体的数据包给我,用c写的代码如下typedef struct _IOMan
{ int commandid
  char  command
  char  date1
  char  date2}IOMAN,*PIOMAN;
 IOMAN iodata;
char * sendbuf;
sendbuf=(char *)&iodata;
当字符发送出去
sendto(sendbuf,sizeof(IoMan));请问我在客户端的接收数据的代码可以这样写吗?并可以得到结构体中的数据吗?procedure TForm1.idTcpServer1Execute(AThread:TidPeerThread);
var
  servercommand:Tcommand;
begin
 Athread.Connection.readBuffer(servercommand,sizeof(servercommand));
//分解结构体数据 
 edit1.Text:=inttostr(servercommand.id);
 edit2.Text:=servercommand.command;
 edit3.text:=servercommand.date1;
end;服务器发的数据已经转换为了字符的。我是不是也要用指针来进行内存拷贝来还原结构体的数据呢呢。
如果按我写的代码不行请帮我写一下代码要怎么解决。

解决方案 »

  1.   

    元素不要用变长的
    string改成string[255]或array [0..254] of Char之类的
      

  2.   

    agree with  sephil(NAILY Soft) 或者 你定義PChar指針 不過同時需要傳送其長度接收包到緩沖 然後用 強行類型轉換 就ok...
      

  3.   

    你得不到结构体中的数据.
    可以简单的把接收的数据写入到一个buffer中,然后强制转换,得到buffer中指定位置的数据。
      

  4.   

    同意以上
    在结构体中发送或接收字符串时,要用定长的字符串,或者用字符数组。直接的 string 在对方是解不到数据的
      

  5.   

    应该这样定义结构:
    type
      Tcommad =packed record
             commandid:integer;
             command :char;
             date1:char;
             date2:char;
    end
    第一点:结构对齐为1字节,要使用packed关键字
    第二点:String是引用类型,其内存变量是个指针,指向从系统堆分配的内存块。接受字符串需使用结构,整数,字符等值变量