1
public class MyClass{
static final String BROADCAST = "Broadcasting";
public static final int PORT = 12344;
public static String host = "localhost";
public static void main(String[] args) {
    // TODO Auto-generated method stub
    DatagramSocket serverSocket;
    try {
        serverSocket = new DatagramSocket(PORT);
        InetAddress addr = InetAddress.getByName(host);

        byte[] sendData = BROADCAST.getBytes();

        DatagramPacket sendPacket = new DatagramPacket(
                sendData, //data byte array
                sendData.length, //number of bytes
                addr, //destination host address
                PORT); //destination port
        serverSocket.send(sendPacket);
        byte[] rcvData = new byte[1024];

        DatagramPacket rcvPacket = new DatagramPacket(
                rcvData, rcvData.length);

        //packet gets filled in by receive
        serverSocket.receive(rcvPacket);

        //Why the two Strings are not the same?
        //Why the bytes derived from the same string different?
        byte[] a = rcvData;
        byte[] b= BROADCAST.getBytes();
        System.out.println(a.equals(b));
        System.out.println(new String(a));
        System.out.println(new String(b));
        System.out.println((new String(a).equals(new String(b))));


    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

I have noticed that

1) The byte arrays I got using getByte() method are different.

2) Even though the String of a and b(from the code) are exactly the same, but they are not equal.

Here is the output I get from four print statements:

false

Broadcasting

Broadcasting

false
  • 1
    Can you not use a debugger and see the `real` data? – Scary Wombat Oct 25 '17 at 05:42
  • 1
    rcvData is a byte[] of length 1024, whereas “Broadcasting”.getBytes() must be shorter. Also, that’s not how you compare arrays (you should use Arrays.equals(arr1, arr2)). – cpp beginner Oct 25 '17 at 05:49
  • 1
    Since the length of `a` is 1024 and the length of `b` is (most probably) 12 - how would you expect those two to be equal? But actually things are even worse as can be seen in https://stackoverflow.com/questions/8777257/equals-vs-arrays-equals-in-java – piet.t Oct 25 '17 at 05:49
  • Use the String(byte bytes[], long off, long len) constructor to get a string for the data the socket received. `new String(rcvData, rcvPacket.getOffset(), rcvPacket.getLength())` – xiaofeng.li Oct 25 '17 at 05:51
  • 1
    I recommend printing arrays by doing System.out.println(Arrays.toString(arr)). You could also print strings by doing System.out.println(“@“ + str + “@“); to see if there is any white space at either end. – cpp beginner Oct 25 '17 at 05:57

3 Answers3

1

Your receiving code is not correct, because the incoming packet is not always the same size as your receiving buffer. So you need to limit the length of your data in converting it to String, otherwise, you will also converting rubbish.

byte[] a = rcvData;
byte[] b = BROADCAST.getBytes();
String recStr = new String(rcvData, 0, rcvPacket.getLength());
System.out.println(recStr.equals(new String(b)));

Then you're really comparing the sending and receiving packet content exactly.

Alex
  • 803
  • 4
  • 9
0

There are problems in your code

First

Your Code:

byte[] rcvData = new byte[1024];

It should: (If it is not functional Requirement)

byte[] rcvData = new byte[BROADCAST.getBytes().length];

Second

Your code:

System.out.println(a.equals(b));

It should:

System.out.println(Arrays.equals(a,b));
Sunil Kanzar
  • 1,244
  • 1
  • 9
  • 21
  • I think the OP is testing the send and receive `DatagramPacket`. Assuming that the client will send packet in variable size, the server won't know the size of other packets. Therefore, it is not meaningful if you defined the array size exactly equal to the sending array size. – Alex Oct 25 '17 at 06:16
  • Yes, You are right @Alex but if it is so then also you code will not work in case of long length data in that case we need to use loop to retrieve data from stream, You are right Bro...! – Sunil Kanzar Oct 25 '17 at 06:33
  • And I just wanted to redirect @Shixu Zhang to the point and that is the length of data is not equals to the length of array in which you are storing that data. – Sunil Kanzar Oct 25 '17 at 06:39
-1

I think by the help of trim() you can achieve what you expect.May be byte a and b are differnt size use trim() while convering byte into string

        byte[] a = rcvData;
        byte[] b= BROADCAST.getBytes();    
        System.out.println(new String(a).trim());
        System.out.println(new String(b).trim());
        System.out.println((new String(a).trim().equals(new String(b).trim())));
AssenKhan
  • 576
  • 5
  • 15
  • `trim()` will not work, if the buffer in use contains previous receiving packet, which is longer than the current one. – Alex Oct 25 '17 at 06:02