2013-10-17 76 views
0

我發送字節數組到服務器,服務器應該接收我發送的數據。但服務器花費很長時間才能收到數據。服務器在Inputstream中等待。發送字節到服務器

如果我通過轉換爲字節發送字符串數據,然後服務器將收到。我不知道發生了什麼事。請幫幫我。

客戶:

void Send(byte []arr) 
{ 
    //Other code 

    String s=new String(arr); 
    byte []msgByte=s.getBytes(); 
    try 
    { 
     outStream.write(msgByte); 
    } 
    catch(Exception e) 
    {} 

    //Other Code 
} 

服務器:

InputStream inStream1=connection.openInputStream(); 
BufferedReader bReader1=new BufferedReader(new InputStreamReader(inStream1)); 
String lineRead=bReader1.readLine(); 
System.out.println(lineRead); 
inStream1.close(); 
+0

你嘗試關閉outStream的outStream? – Neeraj

+1

或沖洗它? – Neeraj

+0

謝謝....它正在工作... – user2889055

回答

0

你需要在郵件的末尾添加 '\ n'(換行符),因爲你希望閱讀並且在寫入消息之後還需要刷新流,因爲默認情況下系統不會自動刷新它,並且刷新也取決於所使用的OutputStream的類型。

void Send(byte[] arr) { 
    // Other code 

    String s = new String(arr) + "\n"; // appending '\n' 
    byte[] msgByte = s.getBytes(); 
    try { 
     outStream.write(msgByte); 
    } catch (Exception e) { 
    } 

    outStream.flush(); //flushing the stream 
    // Other Code 
}  
+0

謝謝...它正在工作... – user2889055

0

嘗試關閉使用outStream.close()