发送的数据分析
ServerSocket端
record型数据
字段名            字节数  属性
Msg_Id              8     integer
Destnation_id      21     Octet String
Service_id          10    Octet String
TP_pid              1     Integer
TP_udhi             1     Integer
Msg_Fmt             1     Integer
Srcterminal_id      21    Octet String
Registered_Delivery 1     Integer
Msg_length          1     Integer
Msg_Content         1     Octet String
Reserve             8     Octet String
记录型数据依次对应以下数据     
十六进制
89 5 23
59 0 0 0     05 0 0 0         17 0 0 0
//////
不用管
77 b2 1 0 2b 87 3 0
是ascii吗,不太知道
'08585'
30 38 35 38 35 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 00 00'13654668189'
31 33 36 35 34 36 36 38 31 38 39 0 0 0 0 0 0 0 0 0 004'DYTT'
44 59 54 540 0 0 0 0 0 0 0
clientServer端部分代码
type
  TRecInfo = record
      Total_Length : integer;
      Command_ID : integer;
      Sequence_ID : integer;
      Msg_Id : int64;
      Destnation_id : string[21];
      Service_id : string[10];
      TP_pid : byte;
      TP_udhi : byte;
      Msg_Fmt : byte;
      Srcterminal_id : String[21];
      Registered_Delivery : byte;
      Msg_length : byte;
      Msg_Content : string[20];
      Reserve : string[8];
end;
procedure TShortMsgForm.ClientSocketRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  RecInfo:^TRecInfo;
begin
  RecInfo:=Pointer(socket.ReceiveText);
  edit1.text:=inttostr(RecInfo^.Total_Length);
  edit2.text:=inttostr(RecInfo^.Command_ID);
  edit3.text:=inttostr(RecInfo^.Sequence_ID);
  edit4.Text:=inttostr(RecInfo^.Msg_Id);
  edit5.Text:=RecInfo^.Destnation_id;
  edit6.Text:=RecInfo^.Service_id;
  edit7.Text:=Inttostr(RecInfo^.TP_pid);
  edit8.text:=Inttostr(RecInfo^.TP_udhi);
  edit9.Text:=Inttostr(RecInfo^.Msg_Fmt);
  edit10.Text:=RecInfo^.Srcterminal_id;
  edit11.Text:=Inttostr(RecInfo^.Registered_Delivery);
  edit12.Text:=Inttostr(RecInfo^.Msg_length);
  edit13.Text:=RecInfo^.Msg_Content;
  edit14.Text:=RecInfo^.Reserve;
end;
通过以上过程,integer,byte得到数据一致,而字符串得到的数据和发送的数据不一致,如何解决

解决方案 »

  1.   

    接收如上面的,发送是从服务器接收的,可能用sendbuf();吧,也是record型
      

  2.   

    用SendBuf()不能直接发送纪录类型的数据,你必须自定义数据传输双方的协议。
      

  3.   

    发送方,协议已经定好了,是用C写的,记录型结构已经给出,我现在用delphi主要负责接受,integer,byte可以,string不行,可能是ascii,十六进制的问题,怎么解决呢
      

  4.   

    将string[21]之类的改为: array [0..21 - 1] of Char;
      

  5.   

    还有字节数好像不对:
    8bit  ==> Byte or Char
    16bit ==> Word
    32bit ==> Integer or Cardinal
    64bit ==> Int64还有定义时,结构里面的域一定要和C/C++一样,不能顺序不同
      

  6.   

    socket.ReceiveText这句有错误,要用读内存流的方式来处理。具体方法我不记得了,但你的问题我遇到过。
      

  7.   

    应该不是使用ReceiveText,你这样用:var
      Data: TRecvInfo;
    begin
      FillChar(Data, SizeOf(Data), 0); 
      Socket.ReceiveBuf(Data, SizeOf(Data));
      ShowMessage(Data.Msg_ID);
    end;
      

  8.   

    以下就是发送端协议定义的
    字段名            字节数  属性
    Msg_Id              8     integer
    Destnation_id      21     Octet String
    Service_id          10    Octet String
    TP_pid              1     Integer
    TP_udhi             1     Integer
    Msg_Fmt             1     Integer
    Srcterminal_id      21    Octet String
    Registered_Delivery 1     Integer
    Msg_length          1     Integer
    Msg_Content         1     Octet String
    Reserve             8     Octet String
      

  9.   

    Msg_Id              8     integer即是8位的话,那应该是char or unsigned char
    你看C是怎么定义的?
    或者你把C的定义贴出来
      

  10.   

    你可以定义一个过程将接收的数据包分接开来,就可以了
    如下:
     procedure Excode;
     var
     ...
       Data:string[21];
     begin
       ...
       for i:=0 to 20 do
          Data[i]:=DateRev[length of Msg_id-1 to length of Msg_Id-1]; 
       ....
      //以下的一样
     end;
      

  11.   

    sorry 写错了
     Data[i]:=DateRev[length of Msg_id-1 + length of Msg_Id-1];
      

  12.   

    to:copy_paste(木石三) 
    关键是serversocket也是我用delphi定义的,用ReceiveBuf、ReceiveText都没问题,可serversocket是用C写的, 而且发送的一堆,ascii,十六进制,什么的,接收就有问题了
    记录型数据依次对应以下数据     
    十六进制
    89(10进制) 5(10进制)     23(10进制)
    59 0 0 0(16进制)           05 0 0 0(16进制)      17 0 0 0(16进制)
    //////
    不用管
    77 b2 1 0 2b 87 3 0
    是ascii吗,不太知道
    '08585'(10进制)
    30 38 35 38 35 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 00 00'13654668189'
    31 33 36 35 34 36 36 38 31 38 39 0 0 0 0 0 0 0 0 0 004'DYTT'
    44 59 54 54(ascii对应'DYTT')0 0 0 0 0 0 0 0
      

  13.   

    我知道你用D来写服务端没问题,我是问你它的C/C++那结构的描述。不然,你给我那些数据,我又不是专搞数据分析的,我怎么知道它的定义。D接受的数据长度要和C里面相同,不然没法知道它确切意思。
      

  14.   

    TRecInfo = record
    改成
    TRecInfo = packed record
      

  15.   

    to: halfdream(哈欠) 
    改成packed,好想对了,我再看看
      

  16.   

    to: halfdream(哈欠) 
    大多数,接收的都对了,可还有个别的有问题,说明这方法是可行的
      

  17.   

    谢谢各位,尤其是copy_paste(木石三),很热心