Java string comparison with equals() not working as expected with UDP -


i trying send message on udp , comparing string literal, not seem work.

here udp server 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 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();         }     } } 

and here udp client 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();         }      } } 

the output "done not received", though done message sent. wrong code?

the problem is, declaring buffer 1024 bytes, converting string. string not equal "done", "too long". have obtain actual length of sent data. in controller:

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 done message             socket.receive(packet);              //get actual data length , copy             byte[] data = new byte[packet.getlength()];             system.arraycopy(packet.getdata(), packet.getoffset(), data, 0, packet.getlength());              //create string actual data             string msg = new string (data);             if(msg.equals("done"))             {                 system.out.println("done received");             }             else {                 system.out.println("done not received");             }          } catch (exception e) {             e.printstacktrace();         }     } } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -