• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

String object != String object?  RSS feed

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about the Strings being
sent through a UDP socket and then having
a new hash code upon retrieval. For example,
I have a string "Hello" and I cast it as
bytes and send it in a Datagram to a server:
// Client does this
byte[] sendData = new byte[1024];
sendData = myString.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IP, port);
The server receives the packet and the string
is cast from the data received.
// Server does this
String socketString = new String(receivedPacket.getData());
The server has a Hashtable, so it looks up the string
"Hello" which is supposed to be in the Hashtable, but
it isn't there. Why is that? I then create another
String regularString = "Hello";
and look it up in the Hashtable and it is there.
So I take the regular String and use the equal()
method to the string passed through the UDP socket
and they say they are not equal?
How do I make the string that came through the UDP
socket a regular normal string like it was before
it left my client?
[This message has been edited by Jeff Jones (edited May 10, 2001).]
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Have you tried printing the value of the string that you receive on the server side to the screen? Your logic for the byte[] to stream looks correct, for instance take this code example:

public class ByteToString {
public static void main(String[] args) {
String myString = "Hello";
byte[] sendData = new byte[1024];

sendData = myString.getBytes();
String socketString = new String(sendData);

if(socketString.equals("Hello")) System.out.println(socketString);

This code does indeed print "Hello" to screen. The only thing different from your code snippet is the fact that it is not passing it to a server first. I think something may be incorrect with the way you are passing it, or possibly the order you are passing it. I'm not sure because I've only used sockets(TCP) for communication and not datagrams(UDP). Remember UDP sends and receives packets of bytes without worrying about lost data or missing packets. You may want to make sure that the byte size is large enough so that you don't lose data. If this is the problem, it may be more appropriate to use sockets to ensure that the integrity of your data stays intact.

Anyway, I'd definitely check to make sure you're getting the value that you think you are getting. Hope this helped!

Jeff Jones
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply and I had the server
print out the string in a sentence to make sure
it did look the same as the original string sent.
System.out.println("--->" + socketString + "<----");<br /> output:<br /> --->Hello<---
So, I know that there wasn't any data loss and
the string does not have any visible additions.
I am thinking that maybe the string is changed
from a particular format during the byte transition
and then when it is recast as a string on the
server side that it is no longer the same object.
If it were, then the hashCode() would be the same.
Just my guess, but I am not sure how to change
the string back to the way before it was before
I sent it through the UDP socket.
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably you got some whitespaces or something. did you check lenght of strings ?
maybe trim() helps ?

It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!