我在從netcat或我的客戶端將UDP數據包發送到偵聽廣播UDP數據包的UDP服務器時遇到了一些問題。問題是我無法重新初始化緩衝區socket.receive(packet);
並且當您檢查我的控制檯輸出時,您將看到數據包被髮送或接收兩次或甚至更多次並且最令人討厭的是當我發送一個數據包時首先是更大的長度,下一個更小的是以前的的一部分! (問題在控制檯輸出上標有HERE)我的客戶端和服務器位於同一LAN上。如何重新初始化數據包的緩衝區?
客戶端代碼:
DatagramSocket socket = new DatagramSocket();
socket.setBroadcast(true);
byte[] buf = ("Hello from Client").getBytes();
byte[] buf2 = ("omg").getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, getBroadcastAddress(UDPConnection.context), Server.SERVERPORT);
DatagramPacket packet2 = new DatagramPacket(buf2, buf2.length, getBroadcastAddress(UDPConnection.context), Server.SERVERPORT);
Log.d("UDP", "C: Sending: '" + new String(buf) + "'");
socket.send(packet);
socket.send(packet2);
Server代碼:
void run(){
MulticastSocket socket = new MulticastSocket(SERVERPORT);
socket.setBroadcast(true);
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
while(true){
Log.d("UDP", "S: Receiving...");
socket.receive(packet);
//socket.setReceiveBufferSize(buf.length);
packet.setData(buf);
Log.i("BUFFER_packet",packet.getLength()+"");
Log.i("BUFFER_socket",socket.getReceiveBufferSize()+"");
Log.d("UDP", "S: From: " + packet.getAddress().getHostAddress());
Log.d("UDP", "S: Received: "+getRidOfAnnoyingChar(packet));
Log.d("UDP", "S: Done.");
}
}
//this method is getting rid of the "questionmark in a black diamond" character
public String getRidOfAnnoyingChar(DatagramPacket packet){
Log.i("UDP","Inside getridofannoyingchar method.");
String result = new String(packet.getData());
char[] annoyingchar = new char[1];
char[] charresult = result.toCharArray();
result = "";
for(int i=0;i<charresult.length;i++){
if(charresult[i]==annoyingchar[0]){
break;
}
result+=charresult[i];
}
return result;
}
控制檯:
11-27 18:15:27.515: D/UDP(15242): S: Connecting...
11-27 18:15:27.519: I/ServerIP(15242): ::
11-27 18:15:27.519: I/LocalIP(15242): 192.168.0.4
11-27 18:15:27.523: D/UDP(15242): S: Receiving...
11-27 18:15:28.031: D/UDP(15242): C: Connecting...
11-27 18:15:28.039: I/BroadcastIP(15242): 192.168.0.255
11-27 18:15:28.042: I/BroadcastIP(15242): 192.168.0.255
11-27 18:15:28.070: D/UDP(15242): C: Sending: 'Hello from Client'
11-27 18:15:28.074: I/BUFFER_packet(15242): 1024
11-27 18:15:28.074: I/BUFFER_socket(15242): 110592
11-27 18:15:28.074: D/UDP(15242): S: From: 192.168.0.4
11-27 18:15:28.074: I/UDP(15242): Inside getridofannoyingchar method.
11-27 18:15:28.078: I/BUFFER_packet(15242): 1024
11-27 18:15:28.078: I/BUFFER_socket(15242): 110592
11-27 18:15:28.078: D/UDP(15242): S: From: 192.168.0.4
11-27 18:15:28.078: I/UDP(15242): Inside getridofannoyingchar method.
11-27 18:15:28.085: D/UDP(15242): S: Received: Hello from Client <------------HERE
11-27 18:15:28.085: D/UDP(15242): S: Done.
11-27 18:15:28.085: D/UDP(15242): S: Receiving...
11-27 18:15:28.085: D/UDP(15242): S: Received: Hello from Client <------------HERE
11-27 18:15:28.085: D/UDP(15242): S: Done.
11-27 18:15:28.085: D/UDP(15242): S: Receiving...
11-27 18:15:28.085: I/BUFFER_packet(15242): 1024
11-27 18:15:28.085: I/BUFFER_socket(15242): 110592
11-27 18:15:28.085: D/UDP(15242): S: From: 192.168.0.4
11-27 18:15:28.085: I/UDP(15242): Inside getridofannoyingchar method.
11-27 18:15:28.089: D/UDP(15242): S: Received: omglo from Client <------------HERE
11-27 18:15:28.089: D/UDP(15242): S: Done.
11-27 18:15:28.089: D/UDP(15242): S: Receiving...
11-27 18:15:28.089: I/BUFFER_packet(15242): 1024
11-27 18:15:28.089: I/BUFFER_socket(15242): 110592
11-27 18:15:28.089: D/UDP(15242): S: From: 192.168.0.4
11-27 18:15:28.089: I/UDP(15242): Inside getridofannoyingchar method.
11-27 18:15:28.089: D/UDP(15242): S: Received: omglo from Client <------------HERE
11-27 18:15:28.089: D/UDP(15242): S: Done.
11-27 18:15:28.089: D/UDP(15242): S: Receiving...
11-27 18:15:28.089: D/UDP(15242): C: Sent.
11-27 18:15:28.089: D/UDP(15242): C: Done.
任何幫助將不勝感激! :)
PS。控制檯中可能會有一些輸出,如完成/發送/連接/接收,但未添加到我的示例代碼中,但所有Received:/ BUFFER_packet/_socket/From都存在。
感謝這個宏偉而詳細的答案!根據你寫的內容,我在這裏找到了一個使用ByteArrayOutputStream的示例:http://homepages.uel.ac.uk/2795l/pages/javaapps.htm 它現在可以工作得很好:) – Miky
不客氣:)請記住, ByteArrayOutputStream也只是使用對數組的引用,所以每次讀取數據包時都不需要重新初始化它,只需調用'reset',它就會再次從數組的開頭進行寫操作。 – AntonyM