線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1645
推到 Plurk!
推到 Facebook!

TClientSocket接收的問題

答題得分者是:暗黑破壞神
李國維
高階會員


發表:42
回覆:287
積分:235
註冊:2003-02-07

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-11-09 12:13:50 IP:219.84.xxx.xxx 未訂閱
請問一下各位大大們: 小弟需要撰寫Client的程式,用來接收Server端的資料,因為Server端的程式是無法更改且寫好的.小弟只能負責收資料.然而server端的通訊協定為: 先傳送陣列大小(4bytes),傳完陣列大小後再傳送陣列資料(小弟只能傻傻的收)    再用TClientSocket的時候發現ㄌㄧ各問題就是TClientSocket好像沒辦法接收8192的資料 所以小弟就分開接收但是會發現資料會錯亂 附上接收的Code煩請大大們幫忙解決
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
      TCustomWinSocket *Socket)
{
 int i,j;
 byte tmpbuf[8192];
 static int iSize;
 static int iPos;
 static int iCount=0;
 switch (iCount)
 {
        case 0:
                Socket->ReceiveBuf(tmpbuf,4);
                for (j=3;j >-1 ; j --)
                 stmp =IntToHex(tmpbuf[j],2);
                iSize = StrToInt("0x" ? stmp);
                iCount = 1;
                iPos =0;
                break;
        case 1:
                if (iSize > 8192)
                {
                 Socket->ReceiveBuf(tmpbuf,8192);
                 iSize -=8192;
                 for ( i = 0 ; i < 8192 ; i ??,iPos??)
                   globalbuf[iPos] = tmpbuf[i];
                }  
                else
                {
                 Socket->ReceiveBuf(tmpbuf,iSize);
                 for ( i = 0 ; i < iSize ; i??,iPos??)
                   globalbuf[iPos] = tmpbuf[i];
                 iCount =0;
                }
                break;
 }
}
 
小弟在預覽文章的時候發現 加號沒有顯示所以小弟用?代替
暗黑破壞神
版主


發表:9
回覆:2301
積分:1627
註冊:2004-10-04

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-11-09 13:26:56 IP:221.169.xxx.xxx 未訂閱
1.已知要收多大。 那就在外面記下來。到裡面去慢慢算。
int maxSize = 8192;    void foo()
{
int recsize = Socket->ReceiveBuf(tmpbuf,iSize);
maxSize -= recsize; // 利用 maxSize 是不是 0 來判斷你這個是不是全部傳完了
}
叫出 Help 你可以看到這些說明 Reads up to Count bytes from the socket connection into the Buf parameter. int __fastcall ReceiveBuf(void *Buf, int Count); Description Use ReceiveBuf to read from the socket connection in the OnSocketEvent event handler of a Windows socket object or in the OnRead or OnClientRead event handler of a socket component. ReceiveBuf returns the number of bytes actually read. If no bytes are read, ReceiveBuf returns -1.
李國維
高階會員


發表:42
回覆:287
積分:235
註冊:2003-02-07

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-11-11 10:49:01 IP:219.84.xxx.xxx 未訂閱
感謝暗黑破壞神: 不過我放棄用TClientSocketㄌ.我改用TIdTCPClient來使用.
引言: 1.已知要收多大。 那就在外面記下來。到裡面去慢慢算。
int maxSize = 8192;    void foo()
{
int recsize = Socket->ReceiveBuf(tmpbuf,iSize);
maxSize -= recsize; // 利用 maxSize 是不是 0 來判斷你這個是不是全部傳完了
}
叫出 Help 你可以看到這些說明 Reads up to Count bytes from the socket connection into the Buf parameter. int __fastcall ReceiveBuf(void *Buf, int Count); Description Use ReceiveBuf to read from the socket connection in the OnSocketEvent event handler of a Windows socket object or in the OnRead or OnClientRead event handler of a socket component. ReceiveBuf returns the number of bytes actually read. If no bytes are read, ReceiveBuf returns -1.
系統時間:2024-05-19 4:17:50
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!