2011-04-13 12 views
1

我想通過TCP/IP套接字使用Java服務器和黑莓客戶端發送600個記錄的列表。但是每當它到達第63條記錄時,它就會停止,奇怪的是,如果我只發送200條記錄,它們就會發送正常。無法通過TCP套接字發送一個大列表 - 黑莓

我一直無法理解爲什麼會發生,只有63條記錄aprox的等於爲4KB,基本上它發送:

  • 與總記錄數的整數發送 併爲每記錄
  • 與琴絃
  • 字符串
  • 一個字符串結束的長度的整數 「$$$」

由於我需要發送整個600我已經試圖關閉InputStreamReader並重新打開它,也重置它,但沒有任何結果。

其他人是否經歷過這種行爲?先謝謝了。

EDIT

這裏,接收的代碼:

private String readfromserver() throws IOException { 
    int len=_in.read(); // receives the string length 

    if (len==0)  // if len=0 then the string was empty 
     return ""; 
    else { 
      char[] input = new char[len+1]; 

      for (int i = 0; i < len; ++i) 
       input[i] = (char)_in.read(); 

      StringBuffer s = new StringBuffer(); 
      s.append(input); 

      return s.toString(); 
    }  
} 

private void startRec(String data) throws IOException 
{ 
    boolean mustcontinue=true; 
    int len=_in.read();  // read how many records is about to receive 

    if (len==0) { 
     scr.writelog("There is no data to receive"); 
    } 
    else { 
     for(int i=0; i<len; i++) 
      if (mustcontinue) { 
       mustcontinue=mustcontinue && showdata(readfromserver()); 
        } 
       else {     
        scr.writelog("Inconsistency error #19");      
       }   
    }   
} 

功能showdata只示出了在所述的LabelField接收字符串。

在服務器中的代碼:

try { 
    _out.write(smultiple.size()); // send the number of records 
    _out.flush(); 

    for (int x=0; x<smultiple.size(); x++) 
    { 
    int l=smultiple.elementAt(x).length(); 
    _out.write(l); // send string length 
     if (l>0) 
     _out.write(smultiple.elementAt(x));   // send string 

    } 
    _out.flush(); 
} catch (Exception e) { 
    principal.dblog(e.toString()); 
} 

smultiple是包含字符串,每個人都已經終止$$$載體。

謝謝。

+0

這可能與您的問題沒有直接關係,但您不需要發送* * *字符串的長度*和*字符串終止符。如果您要發送的字符串包含「$$$」,該怎麼辦? – 2011-04-13 05:35:46

+1

你可以在你的問題中包含相關的黑莓代碼嗎? – 2011-04-13 06:03:11

+0

由於我發送字符串的長度,我只使用終止符來驗證我收到的字符串是否正常,所以我使用它:) – Cesar 2011-04-13 11:23:23

回答

2

我想200進行精細和600 -not,因爲後者數爲大於255 :-)您的代碼

int len=_in.read(); 

可能讀取一個字節不是整數(4個字節)

+0

+ 1好抓! – 2011-04-14 22:56:06

+0

非常好的接收,謝謝! +1 – Cesar 2011-04-15 01:22:05

相關問題