对于不同的内容我连续两次SendBuf,第一次很正常可以发出去,但第二次SendBuf返回的值就是0,写法如下
ClientSocket1.Socket.SendBuf( Sgip_Submit_StoI1, SizeIni1 )
其中Sgip_Submit_StoI1是个类型两次发送的只是Sgip_Submit_StoI1内容的不同而已,第二次发送时ClientSocket1.Socket.SendBuf( Sgip_Submit_StoI1, SizeIni1 ) 的值竟然是0,而sizeof( Sgip_Submit_StoI1 )和SizeIni1都是有值的并且一样;如果只发送一次就是正确的,不管是只发送前半部分还是后半部分,就是两部分一起依次发送就出问题,这是为什么

解决方案 »

  1.   

    这样写:send (clientsocket1.socket.socketHandle,sgip_submit_stoil,sizeinil,0);
      

  2.   

    用send (clientsocket1.socket.socketHandle,sgip_submit_stoil,sizeinil,0);返回的值是-1
      

  3.   

    DELPHI自带的帮助写得很明白的,呵呵。。  For non-blocking sockets, the data is sent to the WinSock DLL which has it's 
    own internal buffers.   If the WinSock can accept additional data, SendBuf returns immediately with the number of bytes queued.    If the WinSock internal buffer space is not able to accept the buffer being sent, SendBuf returns -1 and no data is queued at all. In this case, wait a bit for the WinSock to have a chance to send out already-queued data; then try again.
      

  4.   

    请教halfdream(哈欠)那我是不是要wait a bit,怎样wait a bit呢
      

  5.   

    用WaitForData(TimeOut: Integer)timeout应该是多少??
      

  6.   

    可以这样WAIT A BIT呵呵。。
    procedure MyWait(ms:Cardinal);
    var
      btime:Cardinal;
    begin
      btime:=GetTickCount;
      while GetTickCount-btime< ms do
        Application.ProcessMessages;
    end;
      

  7.   

    我使用
    Stream := TWinSocketStream.Create(ClientSocket1.Socket, 60000);
    Stream.WaitForData(60000);
    就回说必须要设置socket我阻塞式,但如果把socket设成阻塞式服务器就收不到信息,怎样在socket是非阻塞式的情况下wait a bit 呢
      

  8.   

    我用过delay(10000);还是不行
    procedure Delay(msecs:integer);
    var
    FirstTickCount:longint;
    begin
            FirstTickCount:=GetTickCount;
            repeat
                    Application.ProcessMessages; 
            until ((GetTickCount-FirstTickCount) >= Longint(msecs));
    end;
      

  9.   

    TWinSocketStream.Create它只是针对阻塞SOCKET用的,其实也就是select IO API函数。
    还有。。如果SOCKET对端没有接收数据,本地缓冲队列不会清出空间,就还是需继续等待再发送。。
      

  10.   

    代码(希望有人看完,太长了=="):
    procedure TFrmMain.Timer2Timer(Sender: TObject);
    var i,temp,t:integer;
    mobphone,sfdm,id,price,msg,Src_id,query,stmp,xtmp:string;
    SGIP_SUBMIT_tag,SGIP_SUBMIT_tag1,SGIP_SUBMIT_tag2: TSGIP_SUBMIT_tag;
    SGIP_Submit_StoI,SGIP_Submit_StoI1,SGIP_Submit_StoI2: TSgip_Submit_StoI;
    SizeIni1,SizeIni2,zflx,sendcount: integer;
    FV_Date1_S,FV_Date2_S:string;
    abc:longword;
    StartTickCount:longint;begin
    if(clientsocket1.Active)then begin
            try
                adoquery1.SQL.Clear;
                query:='select * from sendquery where msg_status=0 and (left(mobphone,3)=''130'' or left(mobphone,3)=''131'' or left(mobphone,3)=''133'') order by time limit 0,'+trim(tps.text);
                adoquery1.sql.Text:=query;
                adoquery1.open;
            except
                memo1.Lines.Add('&acute;ò&iquest;&ordf;&Ecirc;&yacute;&frac34;&Yacute;&iquest;&acirc;&Ecirc;§°&Uuml;&pound;&iexcl;');
                exit;
            end;
            //memo1.Lines.add(adoquery1.RecordCount);
            if(adoquery1.RecordCount>0) then begin
                initsucceed:=false;
                bSgip_Init( ClientSocket1.Socket );
                repeat
                    if runflag then exit;
                    Application.ProcessMessages;
                until (initsucceed);
                revcount:=0;
                for i:=0 to ADOQuery1.RecordCount-1  do begin
                            sendcount:=ADOQuery1.RecordCount;
                            if(not clientsocket1.Active)then break;
                            mobphone:=adoquery1.FieldByName('mobphone').AsString;
                            sfdm:=adoquery1.FieldByName('sfdm').AsString;
                            id:=adoquery1.FieldByName('id').AsString;
                            zflx:=adoquery1.FieldByName('zflx').AsInteger;
                            price:=adoquery1.FieldByName('price').AsString;
                            msg:=adoquery1.FieldByName('msg').AsString;
                            Src_id:=adoquery1.FieldByName('src_id').AsString;
                            if(length(msg)>140) then begin
                            sendcount:=sendcount+1;
                            temp:=0;
                            for t:=0 to 140 do begin
                              if (ord(msg[t])>128) then begin
                                temp:=temp+1;
                              end;
                            end;
                            if temp mod 2=0 then begin
                              stmp:=copy(msg,1,140);
                              xtmp:=copy(msg,141,length(msg));
                            end
                            else begin
                              stmp:=copy(msg,1,139);
                              xtmp:=copy(msg,140,length(msg));
                            end;
                            //定义发送第一个包以及发送
                            FillChar( SGIP_SUBMIT_tag1, SizeOf( SGIP_SUBMIT_tag1 ), 0 );
                            with SGIP_SUBMIT_tag1 do begin
                             strpcopy( SPNumber, Src_id ); //array[0..5] of char;   
                          strpcopy( ChargeNumber, '' ); //:array[0..20] of char;                
                          UserCount := adoquery1.FieldByName('destusr_tl').AsInteger; //byte
                              if((UserCount=null)or(UserCount<1))then UserCount:=1;
                              strpcopy( UserNumber, '86'+mobphone ); //:array[0..20] of char;  
                           strpcopy( CorpId, eCorpId.Text ); //:array[0..20] of char;  
                           strpcopy( ServiceType, sfdm );  
                           GivenValue          :='0';
                              AgentFlag           :=0;
                              MOrelatetoMTFlag    :=2;
                              FeeType:=zflx; 
                           strpcopy( FeeValue, price ); //:array[0..5] of char;   
                           Priority := adoquery1.FieldByName('msg_level').AsInteger;  
                           if((Priority=null)or(Priority<0))then Priority:=0;
                                 strpcopy( ExpireTime, '' ); //:array[0..16] of char;  
                             strpcopy( ScheduleTime, '' ); //:array[0..16] of char;  
                             ReportFlag := adoquery1.FieldByName('registered_delivery').AsInteger; 
                                 if((ReportFlag=null)or(ReportFlag<0)or(ReportFlag>3))then ReportFlag:=2;
                                 P_pId :=0; //byte
                                 TP_udhi := 0; 
                             MessageCoding := 15;
                                 MessageType:=0;
                                 MessageLength := htonl(length(stmp)); //byte
                                 strpcopy( MessageContent, stmp );
                            end;
                            try
                                    DateTimeToString(FV_Date1_S,'mmddhhnnss',now);
                                    DateTimeToString(FV_Date2_S,'zzz',now);
                                    SizeIni1 := Sizeof( Sgip_Submit_StoI1 );
                                    FillChar( Sgip_Submit_StoI1, SizeIni1, 0 );
                                    Move( SGIP_SUBMIT_tag1, Sgip_Submit_StoI1.body, SizeOf( TSGIP_SUBMIT_tag ) );
                                    abc:=strtoint64('3027'+trim(eCorpId.Text));                                with Sgip_Submit_StoI1 do begin                                        head.MessageLength := htonl( SizeOf( Sgip_Submit_StoI1 ) -(161-ntohl(Sgip_Submit_StoI1.body.MessageLength)) );
                                            head.CommandId := htonl( SGIP_SUBMIT );
                                            head.SNumber1:=abc;
                                            head.SNumber2:=htonl(StrToInt(FV_Date1_S));
                                            head.SNumber3:=htonl(StrToInt(FV_Date2_S));
                                    end;                                
                                    if ClientSocket1.Socket.SendBuf( Sgip_Submit_StoI1, SizeIni1 ) <> SizeIni1 then begin
                                            memo1.Lines.Add( '发送错误‘);
                                            exit;
                                    end;
                                    memo1.Lines.Add( '发送成功' );
                                    delay(1);
                            except
                                    on e: exception do begin
                                            memo1.Lines.Add( '异常' + e.Message );
                                    end;
                            end;
      

  11.   

    //定义发送第二个包以及发送
                            delay(20000);
                            FillChar( SGIP_SUBMIT_tag2, SizeOf( SGIP_SUBMIT_tag2 ), 0 );
                            with SGIP_SUBMIT_tag2 do begin
                              strpcopy( SPNumber, Src_id ); //array[0..5] of char; 
                           strpcopy( ChargeNumber, '' ); //:array[0..20] of char;                       
                           UserCount := adoquery1.FieldByName('destusr_tl').AsInteger; //byte
                              if((UserCount=null)or(UserCount<1))then UserCount:=1;
                              strpcopy( UserNumber, '86'+mobphone ); //:array[0..20] of char;  
                           strpcopy( CorpId, eCorpId.Text ); //:array[0..20] of char;  
                           strpcopy( ServiceType, sfdm );
                              GivenValue          :='0';
                              AgentFlag           :=0;
                              MOrelatetoMTFlag    :=2;
                              FeeType:=zflx;                                 
                          strpcopy( FeeValue, price ); //:array[0..5] of char;                   
                             Priority := adoquery1.FieldByName('msg_level').AsInteger;                                  
                          if((Priority=null)or(Priority<0))then Priority:=0;
                             strpcopy( ExpireTime, '' ); //:array[0..16] of char;                  
                          strpcopy( ScheduleTime, '' ); //:array[0..16] of char;  
                          ReportFlag := adoquery1.FieldByName('registered_delivery').AsInteger; 
                             if((ReportFlag=null)or(ReportFlag<0)or(ReportFlag>3))then ReportFlag:=2;
                             TP_pId := 0; //byte              
                             TP_udhi := 0; //byte
                          MessageCoding := 15; //byte
                             MessageType:=0;
                             MessageLength := htonl(length(xtmp)); //byte             
                             strpcopy( MessageContent, xtmp ); //:array[0..MSG_LENGTH-1] of char; 
                         end;
                            try
                                    DateTimeToString(FV_Date1_S,'mmddhhnnss',now);
                                    DateTimeToString(FV_Date2_S,'zzz',now);
                                    SizeIni2 := Sizeof( Sgip_Submit_StoI2 );
                                    FillChar( Sgip_Submit_StoI2, SizeIni2, 0 );
                                    Move( SGIP_SUBMIT_tag2, Sgip_Submit_StoI2.body, SizeOf( TSGIP_SUBMIT_tag ) );
                                    abc:=strtoint64('3027'+trim(eCorpId.Text));                                with Sgip_Submit_StoI2 do begin                                   head.MessageLength := htonl( SizeOf( Sgip_Submit_StoI2 )-(161-ntohl(Sgip_Submit_StoI2.body.MessageLength)));
                                       head.CommandId := htonl( SGIP_SUBMIT );
                                       head.SNumber1:=abc;
                                       head.SNumber2:=htonl(StrToInt(FV_Date1_S));
                                       head.SNumber3:=htonl(StrToInt(FV_Date2_S));
                                    end;                                                               if ClientSocket1.Socket.SendBuf( Sgip_Submit_StoI2, SizeIni2 ) <> SizeIni2 then begin
                                            memo1.Lines.Add( '发送错误’);
                                            exit;
                                    end;
                                    memo1.Lines.Add( '发送成功' );
                                    adoquery2.SQL.Clear;
                                    adoquery2.sql.Add('update sendquery set msg_status=1,msg_id='''+inttostr(ntohl(Sgip_Submit_StoI.Head.SNumber1))+inttostr(ntohl(Sgip_Submit_StoI.Head.SNumber2))+inttostr(ntohl(Sgip_Submit_StoI.Head.SNumber3))+''' where id='+id);
                                    
                                    adoquery2.ExecSQL;                                delay(1);
                            except
                                    on e: exception do begin
                                            memo1.Lines.Add( '异常' + e.Message );
                                    end;
                            end;
    //定义发送第二个包以及发送结束
                            end;
                            ADOQuery1.Next;
                end;
                 
            end;
            adoquery1.Close;
    end;end;其实发送的两个包只是类型中的MessageContent不同而已