here is the code i send the string:
byte[] buffer = txtAreaSendText.getText().getBytes();// Point A
DatagramPacket dp = new DatagramPacket(buffer,buffer.length,remoteAddr, remoteTextPort);
udpSocket.send(dp);
and here is my code for receiving the string:
byte[] buffer = new byte[1024];
DatagramPacket dp = new DatagramPacket(buffer, 1024);
try {
udpSocket.receive(dp);
String txtString = dp.getData().toString();
} catch (IOException e) {
e.printStackTrace();
}
i try to correct the code by change the code at Point A:
byte[] buffer = txtAreaSendText.getText().getBytes("utf-8");
but i can not get the right string as well.
plus, my default encoding of eclipse workspace is utf-8,and java file as well.
What are you getting?
From the looks of it you’re calling
toString()on a byte array and it probably returns the class type as a string (or something similar).I believe what you should be doing is: