2014-03-30 25 views
0

我從客戶端向服務器發送兩個數據包。我遇到的問題是在服務器上讀取的數據使兩個字符串的長度與發送的最長字符串相同。例如:發送的UDP數據包沒有被服務器正確讀取

如果串1是:1234
和字符串2:abcdefghi

服務器將讀取

1234efghi 
abcdefghi 

它應該只是顯示

1234 
abcdefghi 

我的代碼:

byte[] toSendUser = new byte[1024]; 
byte[] toSendPass = new byte[1024]; 

String name = "1234"; 
String password = "abcdefghi"; 

    toSendUser = name.getBytes(); 
     toSendPass = password.getBytes(); 
     DatagramPacket packSend = new DatagramPacket (toSendUser, toSendUser.length, ipConn, 9876); 
     connection.send(packSend); 

     DatagramPacket packSendtwo = new DatagramPacket (toSendPass, toSendPass.length, ipConn, 9876); 
     connection.send(packSendtwo); 

它可能是服務器問題,或者是我的客戶端中的代碼錯誤?

+0

哪裏是關於Linux和/或Unix的問題? –

+0

向我們顯示*服務器*代碼。 – EJP

回答

1

在Java中的DatagramPackets不斷縮小到目前爲止收到的最小尺寸。你必須創建一個新的每receive(),或至少每次重置長度在下一個之前receive().

相關問題