Take the 2-minute tour ×

my target is it to collect data from clients which send data to an UDP server, concatenate the data in a string (just for testing now) and send the whole concatenated string back to the client. The communication between UDPClient and UDPServer works perfect. I can send data and receive it. But I wonder why I can't concatenate the data in a StringBuffer, because when I try that, the client always gets the very first string I ever sent. And the string does not change anymore regardless of what I'm sending to the server. Here is the code of the server class:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class UDPServer extends Thread {

     DatagramSocket serverSocket;
     DatagramPacket receivePacket;
     String sentence;
     InetAddress IPAddress;
     StringBuffer data = new StringBuffer();

    public UDPServer() throws SocketException {
        serverSocket = new DatagramSocket(9876);

    public void run()    {
        while (true)  {
             byte[] receiveData = new byte[1024];
             byte[] sendData = new byte[1024];
             receivePacket = new DatagramPacket(receiveData, receiveData.length);

             try {
            } catch (IOException e) {

             sentence = new String(receivePacket.getData());
             IPAddress = receivePacket.getAddress();
             int port = receivePacket.getPort();
             sendData = data.toString().trim().getBytes();
             DatagramPacket sendPacket = new DatagramPacket(sendData,
             sendData.length, IPAddress, port);
             try {
            } catch (IOException e) {

I already debugged it: The variable "sentences" always gets the right data, but the line data.append(sentence); doesn't do anything. It just doesn't append the data. Why is this so?

share|improve this question
have you looked in the string buffer before/after the append, with a debugger?? –  hvgotcodes Jul 31 '10 at 18:35
There is one more bug. Your stringbuffer variable needs to be cleared before appending new data. probably, data.setLength(0) and then start appending new data. But this will not solve your original problem –  chedine Jul 31 '10 at 18:46
I don't think it is relevant to your problem, but always specify a charset with new String(...) and String.getBytes(...). I recommend always using "UTF-8". Also, your line sendData = new byte[1024]; is useless. sendData will be whatever the data.toString() method returns, which could be larger than 1024 bytes. Presumably, this is a bug in your code. –  GregS Jul 31 '10 at 18:49
add comment

1 Answer

up vote 4 down vote accepted

Your problem has nothing to do with the StringBuffer, but with this line:

sentence = new String(receivePacket.getData());

You're turning the whole buffer into a string, when the sender may have sent less than the size of the buffer. So you get a bunch of zero bytes in the resulting string, which cause the data you print to be truncated.

You need to truncate the receive buffer based on how much data was actually read:

sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());
share|improve this answer
Thanks. I didn't think about the zero bytes. Now it works as I wish. –  Bevor Jul 31 '10 at 19:42
add comment

Your Answer


