简体   繁体   中英

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". 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. Eg sender sends "iwas1" and receiver understands "[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.)

//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());

Change it to

String teststring = new String(empfangpaket.getData());

You are printing the result of Byte[].toString() instead of what you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM