[英]Java: Can send and receive UDP-packets, but received data is gibberish
EDIT: Solved (see comment)编辑:解决(见评论)
I'm trying to write a very basic programm/system where one programm sends UDP-Packets containing a string with the word iwas and a single-digit-number, eg "iwas2".我正在尝试编写一个非常基本的程序/系统,其中一个程序发送 UDP 数据包,其中包含一个带有单词 iwas 和一位数的字符串,例如“iwas2”。 A second programm than is supposed to receive the packet (and later write its content into a vector).
第二个程序应该接收数据包(然后将其内容写入向量)。
The sending-programm seems to work fine, but the receiving-programm not so much.发送程序似乎工作正常,但接收程序却没有那么多。 When i start the receiving programm it does receive a packet as soon as the sending-programm is started/starts to send (and does not receive a package/misinterpret other things as a package before), but the received content doesn't match the content sent or make any sense to me at all.
当我启动接收程序时,它会在发送程序启动/开始发送时立即收到一个数据包(并且没有收到一个包/之前将其他内容误解为 package),但接收到的内容与发送的内容或对我有任何意义。 Eg sender sends "iwas1" and receiver understands "[B@6a2bcfcb".
例如,发送方发送“iwas1”,接收方理解“[B@6a2bcfcb”。 (According to wireshark the data-part of the sender-packet really is "iwas1" with a length of 5 bytes, so that doesn't seem to be the problem.)
(根据wireshark,发送数据包的数据部分确实是“iwas1”,长度为5个字节,所以这似乎不是问题。)
//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");
}
}
I would think that very likely the problem is how i process/interpret the bytes received, but don't know where exactly.我认为问题很可能是我如何处理/解释接收到的字节,但不知道具体在哪里。 Thanks for any help.
谢谢你的帮助。
In line String teststring = new String(empfangpaket.getData().toString());
在行
String teststring = new String(empfangpaket.getData().toString());
Change it to将其更改为
String teststring = new String(empfangpaket.getData());
You are printing the result of Byte[].toString() instead of what you expect.您正在打印 Byte[].toString() 的结果,而不是您期望的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.