5

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?

5
  • 1
    What is not working? Exception, expected vs. actual output? Commented Oct 31, 2014 at 7:25
  • What does System.out.println("RECEIVED: " + sentence); print? Commented Oct 31, 2014 at 7:26
  • System.out.println("RECEIVED: " + sentence) will print "RECEIVED: TEST" But when comparing sentence against "TEST" the result is false Commented Oct 31, 2014 at 7:29
  • Are you sure you have no spaces in your input string, in sentence? Commented Oct 31, 2014 at 7:48
  • Could you report the hexdump (you can use this to print hexdump) of sentence and receivePacket.getData()? Commented Oct 31, 2014 at 7:49

5 Answers 5

5

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.)

Sign up to request clarification or add additional context in comments.

3 Comments

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) the comparison gives me true.
Any way to cut out all those extra blank characters after or before the word "TEST" for example?
Check out the DatagramPacket.getOffset() and DatagramPacket.getLength() methods.
1

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);

1 Comment

System.out.println(msg) is giving me TEST, but the comparison between msg and "TEST" are false still
1

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.

Comments

0

"TEST " and "TEST" are not equal. Verify string length.

System.out.println(sentence.length());

To remove spaces using:

sentence = sentence.trim();

1 Comment

Tried the trim command, still returns 1024 character string
0

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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.