我的问题是:在使用TserverSocket和TclientSocket进行通讯时,在服务器端我发送的信息是:
array  of  byte 类型的信息,可是我遇到了这样一个问题,在我发送信息之前,我取得信息的长度是127,可是到了客户端,信息读取的长度是23,我实在是搞不清楚这是为什么?我的客户端和服务器端是做在一起的,只是先想测试一下,然后再分开,不知哪位大虾可否指点一二!!!!????分  我会多给,我现在有5000多分,我不会吝啬的!!!!  急急急!!!!

解决方案 »

  1.   

    procedure TForm1.ServerSClientRead(Sender: TObject;
      Socket: TCustomWinSocket);type nowinfor = ( ISININFO,ISINCHATROOMINFO,ISOUTINFO,ISTRANSINFO,ISINSTALLNEWINFO,ISINVITATIONINFO,ISLOCKINFO,ISUNLOCKINFO,NONE);
            //用户注册请求处理子过程;
            procedure proininfo(inbuf:array of byte;len:integer;s:Tsocket);
            var    tempinf:POnlineinf;
                   templen1:integer;
                   tempQuery:TQuery;
                   tempchatroominf:PChatRoominf;
                   i:integer;
                   tempstr:string;
                   len3:integer;
                   templen,len1,len2:integer;
                   buf:array of byte;
                   k:integer;        begin
                    if inbuf[len-1]<>0 then exit;                tempinf:=new(POnlineinf);
                    templen1:=len-contrallen-1;
                    tempinf.username[0]:=char(lo(templen1));
                    copymemory(@tempinf.username[1],@inbuf[contrallen],templen1);
                    tempinf.soc:=s;
                    tempinf.ChatRoom:='';
                    Onlines.Add(tempinf);
                    edit1.Text:=inttostr(Onlines.Count);
                    rtalk.Lines.Add('用户名:'+tempinf.username+' 连接socket:'+inttostr(s)+'与服务器取得联系!');                tempQuery:=TQuery.Create(nil);
                    tempQuery.DatabaseName:='ChatRoom';
                    tempQuery.SQL.Clear;
                    tempQuery.SQL.Add('select * from ChatRoom');
                    tempQuery.Active:=true;
                    while Not tempQuery.Eof do
                    begin
                          tempchatroominf:=new(PChatRoominf);
                          tempstr:=tempQuery.FieldValues['ChatRoomName'];
                          len3:=length(tempstr);
                          tempchatroominf.ChatRoom[0]:=char(lo(len3));
                          copymemory(@tempchatroominf.ChatRoom[1],@tempstr[1],len3);
                          tempchatroominf.ID:='1'; //"1"用来代表企业聊天室
                          Onchatrooms.Add(tempchatroominf);
                          tempQuery.Next;
                    end;
                    tempQuery.Active:=false;
                    tempQuery.SQL.Clear;
                    tempQuery.SQL.Add('select * from PersonalityChatRoom');
                    tempQuery.Active:=true;
                    while Not tempQuery.Eof do
                    begin
                         tempchatroominf:=new(PChatRoominf);
                         tempstr:=tempQuery.FieldValues['ChatRoomName'];
                         len3:=length(tempstr);
                         tempchatroominf.ChatRoom[0]:=char(lo(len3));
                         copymemory(@tempchatroominf.ChatRoom[1],@tempstr[1],len3);
                         tempchatroominf.ID:='2'; //"2" 用来代表用户自己新建的聊天室
                         Onchatrooms.Add(tempchatroominf);
                         tempQuery.Next;
                   end;  //到此时,最新的聊天室列表已生成 ;
                   tempQuery.Close;
                   tempQuery.Free;
                   //接下来,要生成刷新聊天室列表的信息格式,以便发送到各个客户端进行刷新;
                   //edit3.Text:=Pchatroominf(Onchatrooms.Items[0]).chatroom;
                   setlength(buf,contrallen+sizeof(integer));
                   for i:=0 to contrallen-1 do
                       if ((i mod 2)=0) then buf[i]:=$D
                       else buf[i]:=$E;
                   Pinteger(@buf[contrallen])^:=Onchatrooms.Count;
                   //填充报文的房间列表内容:
                   for i:=0 to Onchatrooms.Count-1 do
                   begin
                         len1:=length(PChatRoominf(Onchatrooms.Items[i]).ChatRoom);
                         len2:=length(PChatRoominf(Onchatrooms.Items[i]).ID);
                         templen:=length(buf);
                         setlength(buf,templen+len1+1);
                         copymemory(@buf[templen],@PChatRoominf(Onchatrooms.Items[i]).ChatRoom[1],len1);
                         buf[templen+len1]:=0;
                         templen:=length(buf);
                         setlength(buf,templen+len2+1);
                         copymemory(@buf[templen],@PChatRoominf(Onchatrooms.Items[i]).ID[1],len2);
                         buf[templen+len2]:=0;
                    end;
                    //edit3.Text:=inttostr(buf[length(buf)-23]);                send(s,buf[0],len,0);
    end;
    这是生成报文,并且发送的代码,
      

  2.   

    D7的\Demos中有,自己看!
    给分给分·
      

  3.   

    procedure TForm1.ClientSRead(Sender: TObject; Socket: TCustomWinSocket);
    type nowinfor = ( ISLOCKINFO,ISUSERLISTINFO,ISROOMLISTINFO,ISRECEIVEINFO,ISBEINVITEDINFO,ISCANENTERINFO,ISFORBIDINFO,NONE);
             Procedure Proroomlistinf(inbuf:array of byte;len:integer);
           var
                tempchatroom:PClientchatroominf;
                chatroomnum:integer;
                basepos:integer;
                loopnum:integer;
                lastpos:integer;
                chatroomlen:integer;       begin
               while(Clientchatrooms.Count>0) do
                    begin
                    dispose(Clientchatrooms.First);
                    Clientchatrooms.Delete(0);
                    end;
               basepos:=contrallen;
               chatroomnum:=Pinteger(@inbuf[basepos])^;
               inc(basepos,sizeof(integer));
               for loopnum:=0 to chatroomnum-1 do
               begin
                     lastpos:=basepos;
                     while (basepos<len) and (inbuf[basepos]<>0) do
                            inc(basepos);
                    if basepos>=len then break;
                     if (basepos-lastpos)>255 then
                         begin
                              inc(basepos,3);
                              continue;
                         end;
                     tempchatroom:=new(PClientchatroominf);
                     chatroomlen:=basepos-lastpos;
                     tempchatroom.ChatRoom[0]:=char(lo(chatroomlen));
                     copymemory(@tempchatroom.ChatRoom[1],@inbuf[lastpos],basepos-lastpos);
                     inc(basepos);
                     lastpos:=basepos;
                     inc(basepos);
                     tempchatroom.ID[0]:=char(lo(basepos-lastpos));
                     copymemory(@tempchatroom.ID[1],@inbuf[lastpos],basepos-lastpos);
                     inc(basepos);
                     Clientchatrooms.Add(tempchatroom);
               end;
               //接着是生成聊天室列表树;
               CreateTreeNodes();
         end;。。省略
    var
           buf:array of byte;
           len:integer;
           i:  integer;
           nowtag:Boolean;
           tag:nowinfor;
    begin
            len:=Clients.Socket.ReceiveLength;
            setlength(buf,len);
            Clients.Socket.ReceiveBuf(Buf[0],len);
            if len<contrallen then exit;
            nowtag:=true;
            for i:=0 to contrallen-1 do
            begin
                  if ((i mod 2)=0 ) then nowtag:=nowtag and (buf[i]=$D)
                  else nowtag:=nowtag and (buf[i]=$E);
                  if not nowtag then break;
            end;
            if nowtag then  tag:=ISROOMLISTINFO  //刷新聊天室列表:DEDEDEDE...
    ........................省略   case tag of
            ISRECEIVEINFO: Proreceiveinf(buf,len);
            ISCANENTERINFO: Procanenterinf(buf,len);
            ISFORBIDINFO: Proforbidinf(buf,len);
            ISUSERLISTINFO: Prouserlistinf(buf,len);
            ISROOMLISTINFO: Proroomlistinf(buf,len);
            ISLOCKINFO: Prolockinf(buf,len);
            ISBEINVITEDINFO: Probeinvitedinf(buf,len);
            NONE: Procanenterinf(buf,len);//exit;
            end;
    end;--------这是客户端接收处理代码,在这段代码的事件处理主程序中的len:=Clients.Socket.ReceiveLength;
    这段代码所得出的长度并不与服务器端发来的长相同,长度减少
      

  4.   

    呵呵,自己好好调试,在大富翁上我不是说了吗,TCP是流,
      

  5.   

    length一般用来取字符串的长度,建议你两端都用固定的值,肯定能解决,我的一个朋友也遇到过这个问题
      

  6.   

    我还是没看明白你的代马。
    不过,你有没有看看在Send之前BUF的长度以及Len的值