After being send through socket string is corrupted

J

Jan Liße

Hi,
I just want to send from my client a udp-packet
that only contains a string to a server.
Everything works fine but when i receive the packet at my server
and convert the received byte[] to a string, the string is corrupted:
it contains the correct text but additionally there are strange characters
appended looking like squares. It seems that the conversion to string
is not abel to get the correct end of the string...
First i thought it might be an issue with encoding settings and i tried
beside
the default encoding on both sides UTF-8 and ISO-8859-1 but it didnt work
either.
When i use tcp instead of udp it the same problem...
Maybe i could do some string filtering but i want to grasp the reason
for this strange behaviour!
Any help appreciated,
Jan


My reduced source:

Client-side:

serverAddr = InetAddress.getByName("Server-IP");
udpSocket = new DatagramSocket();
String testString = "Test";
byte[] sendBuf = testString.getBytes();
DatagramPacket packet = new DatagramPacket(sendBuf, sendBuf.length,
serverAddr, udpPort);
udpSocket.send(packet);
udpSocket.close();

Server-side:

DatagramSocket udpSocket = new DatagramSocket(udpPort);
DatagramPacket p = new DatagramPacket(rcvBuffer,rcvBuffer.length);
udpSocket.receive(p);
rcvBuffer = p.getData();
dataStr = new String(rcvBuffer);
 
G

Gordon Beaton

I just want to send from my client a udp-packet that only contains a
string to a server.

Everything works fine but when i receive the packet at my server and
convert the received byte[] to a string, the string is corrupted: it
contains the correct text but additionally there are strange
characters appended looking like squares.
[...]

DatagramPacket p = new DatagramPacket(rcvBuffer,rcvBuffer.length);
udpSocket.receive(p);
rcvBuffer = p.getData();
dataStr = new String(rcvBuffer);

Most likely rcvBuffer is longer than the actual received message.

Try this instead:

dataStr = new String(rcvBuffer, 0, p.getLength());

Although I'm certain the above will solve your immediate problem, you
really should specify a character encoding whenever you convert from
String to byte[] or vice versa. Consider what happens if the sender
and receiver are on different platforms with different default
encodings. There are versions of String.getBytes() and String
constructors that let you do that.

/gordon
 
B

Barry White

Hi Jan,

are you sure it's not just an issue with new-line characters? Open a
unix text file in windows notepad and you will see a square character
where a new line should be. This is because unix uses '\n' for new lines
and windows uses '\n\r'. Using string.trim() will strip the string of
either.

Where are the strings coming from? A text file (Windows/Unix)?

Barry
 
S

Suresh

[..snip..]
DatagramPacket p = new DatagramPacket(rcvBuffer,rcvBuffer.length);
udpSocket.receive(p);
rcvBuffer = p.getData();
dataStr = new String(rcvBuffer);

Is the rcvBuffer the same length as the data that is recieved ? If it is
larger than the size of the data that is recieved, use the p.getLength()
function to get the length of the data.

dataStr=new String(rcvBuffer,0, p.getLength());



HTH.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top