title: Java string comparison with equals() not working as expected with UDP
body: <p>I am trying to send a message over UDP and comparing it with a string literal, but it does not seem to work.</p>
  
  <p>Here is the UDP server code</p>
  
  <pre><code>import java.net.DatagramPacket;
  import java.net.DatagramSocket;
  import java.net.InetAddress;
  
  public class Controller {
  
      public static void main(String args[]) {
  
  
          //create UDP server
          try {
              DatagramSocket socket = new DatagramSocket(5002);
              byte[] buffer = new byte[1024];
              DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
  
              //wait for done message
              socket.receive(packet);
              String msg = new String (packet.getData());
              if(msg.equals("Done"))
              {
                  System.out.println("Done received");
              }
              else {
                  System.out.println("Done not received");
              }
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>And here is the UDP client code</p>
  
  <pre><code>import java.net.DatagramPacket;
  import java.net.DatagramSocket;
  import java.net.InetAddress;
  
  public class Slave {
  
      public static void main(String args[]) {
          //create UDP client
          try {
              DatagramSocket socket = new DatagramSocket();
              byte[] buffer;
  
              //send Done message
              buffer = "Done".getBytes();
              DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getLocalHost(), 5002);
              socket.send(packet);
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>
  
  <p>The output I get is "done not received", even though done message is sent. What is wrong with the code?</p>