Java:可以发送和接收UDP数据包,但接收到的数据乱七八糟
编辑:已解决(请参见注释) 我正在尝试编写一个非常基本的程序/系统,其中一个程序发送UDP数据包,其中包含一个带有单词iwas和一个位数的字符串,例如“iwas2”。第二个程序接收数据包(然后将其内容写入向量)。Java:可以发送和接收UDP数据包,但接收到的数据乱七八糟,java,sockets,udp,Java,Sockets,Udp,编辑:已解决(请参见注释) 我正在尝试编写一个非常基本的程序/系统,其中一个程序发送UDP数据包,其中包含一个带有单词iwas和一个位数的字符串,例如“iwas2”。第二个程序接收数据包(然后将其内容写入向量)。 发送程序似乎工作正常,但接收程序却不太正常。当我启动接收程序时,它会在发送程序启动/开始发送时立即接收数据包(并且不会接收数据包/之前会将其他内容误解为数据包),但接收到的内容与发送的内容不匹配,或者对我来说毫无意义。例如,发送方发送“iwas1”,接收方理解“iwas1”[B@6a2
发送程序似乎工作正常,但接收程序却不太正常。当我启动接收程序时,它会在发送程序启动/开始发送时立即接收数据包(并且不会接收数据包/之前会将其他内容误解为数据包),但接收到的内容与发送的内容不匹配,或者对我来说毫无意义。例如,发送方发送“iwas1”,接收方理解“iwas1”[B@6a2bcfcb”(根据wireshark的说法,发送方数据包的数据部分实际上是长度为5字节的“iwas1”,所以这似乎不是问题。)
我想问题很可能是我如何处理/解释接收到的字节,但不知道具体在哪里。谢谢您的帮助。在line
String teststring=new String(empfangpaket.getData().toString());
换成
String teststring=新字符串(empfangpaket.getData());
您正在打印Byte[].toString()的结果,而不是您期望的结果。它现在可以工作,因为我将“String teststring=new String(empfangpaket.getData().toString());”更改为“String teststring=new String(empfangbytearray,0,empfangpaket.getLength());”
//code of sender-programm
import java.io.IOException;
import java.net.*;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class umgebung {
public static void main(String[] args) throws IOException, InterruptedException {
//try {
//byte[] buffer = new byte[65508];
//InetAddress address = InetAddress.getByName("jenkov.com");
//DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, 9000);
//Integer inteins = new Integer(5);
Random zahlgen = new Random();
int aktwetter = 0; //initialisieurng
DatagramSocket socketeins = new DatagramSocket(90);
while (0 != 1) {
/*
int neugenentscheidungszahl = zahlgen.nextInt() % 10;
if (neugenentscheidungszahl > 8) {
aktwetter = zahlgen.nextInt() % 4;
}
*/
aktwetter = ++aktwetter % 4;
System.out.printf(aktwetter + "\n");
String stringeins = new String("iwas" + aktwetter);
;
byte[] buffer = stringeins.getBytes();
//InetAddress empfangsip;
//empfangsip = InetAddress.getByName("127.0.0.1");
DatagramPacket paketeins = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("127.0.0.1"), 50); //senden an port 50
//DatagramSocket socketeins = new DatagramSocket(90);
socketeins.send(paketeins);
TimeUnit.SECONDS.sleep(1);
}
//}
/*
catch(IOException | InterruptedException e){
//e.printStackTrace();
System.out.printf("verkackt");
}
*/
}
}
//code of receiver-programm
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
public class empfaenger {
public static void main(String[] args) throws IOException {
InetAddress empfangadresse = InetAddress.getByName("127.0.0.1");
DatagramSocket socketeins = new DatagramSocket(50, empfangadresse);
byte[] empfangbytearray = new byte[65000];
DatagramPacket empfangpaket = new DatagramPacket(empfangbytearray, empfangbytearray.length);
socketeins.receive(empfangpaket);
String teststring = new String(empfangpaket.getData().toString());
System.out.println("bla" + teststring + "bla");
}
}