Reputation: 53
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?
Upvotes: 5
Views: 1524
Reputation: 1831
From Java tutorial use:
String received = new String(packet.getData(), 0, packet.getLength());
to get string from received packet; in your case you will have:
String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());
Upvotes: 0
Reputation: 718678
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.)
Upvotes: 5
Reputation: 645
I think receiveData is longer than 4, so trailing bytes are also contained in the converted String. You should cut off the trailing bytes(byte 0) when you new String.
Upvotes: 1
Reputation: 1323
"TEST "
and "TEST"
are not equal.
Verify string length.
System.out.println(sentence.length());
To remove spaces using:
sentence = sentence.trim();
Upvotes: 0
Reputation: 30855
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);
Upvotes: 1