I have a UDP server/client that sends and receive information. The Client
will send example "TEST"
to the server, the server will receive "TEST"
in a byte[]
array representation and convert it to String
using the String(byte[])
constructor. I then need to compare the new String
received against another string using the equalsIgnoreCase
method. But it's not working...
Here's a sample of my code :
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
//Client sent "TEST"
String sentence = new String(receivePacket.getData(), "UTF-8");
System.out.println("RECEIVED: " + sentence);
System.out.println(sentence.equalsIgnoreCase("TEST")); //This gives me FALSE
Any ideas?
There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.
Given a Byte value in Java, the task is to convert this byte value to string type. One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable.
We can use String class getBytes() method to encode the string into a sequence of bytes using the platform's default charset. This method is overloaded and we can also pass Charset as argument. Here is a simple program showing how to convert String to byte array in java.
Convert byte[] to String (text data) toString() to get the string from the bytes; The bytes. toString() only returns the address of the object in memory, NOT converting byte[] to a string ! The correct way to convert byte[] to string is new String(bytes, StandardCharsets. UTF_8) .
The most likely explanation is that the string in sentence
is not "TEST"
but something that looks like "TEST"
when you display it. The most likely strings are:
" TEST"
or "TEST "
"TEST\n"
But it is also possible that you have a homoglyph in either the client string, or your server code.
Alright so I just found out using sentence.length() that sentence is 1024 characters long...if I take a substring of it, specifically sentence.substring(0,4).
Ah. So the problem is (probably) that you are ignoring the size of the received packet, and including the entire buffer array in the string.
The most correct way to extract the string is:
new String(receivePacket.getData(),
receivePacket.getOffset(),
receivePacket.getLength(), "UTF-8");
... though I think that the offset is going to be zero, given the previous statements.
(But it is also possible that the client is sending the NUL bytes ... in which case, you will also need to change things on the client side.)
Create string object like this way
String msg = new String(receiveData, 0, receivePacket.length);
instead of
String sentence = new String(receivePacket.getData(), "UTF-8");
In your code will getting the data but it have extra content too. For example in your receiveData size set to 10 which will be reading from your packet object. Now for "Test" it will be use 4 block in your receiveData and rest of block i.e. 6 are null so that all will be use in your sentence object too. Thats why sentence will not match.
For test just print your sentence object first like this way
Log.e("Sentence",""+sentence);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With