hi all,
i encountered a strange problem while writing a simple UDP client-server pair which cost me nearly an hour to track down.
i have my class UDPServer which waits for a datagram from the client and will then send a string back. I build the string using a string buffer like this:
Now this looks like it should work, only it doesn't.
The client only receives the first part of the strbuf (rcvString) and doesn't get the rest.
but now comes the best part:
when i simply change the order in which i append stuff to the stringbuffer like so:
it works, wtf is going on there
maybe i am just dense, but i can't see what goes wrong.
btw i use netbeans 3.3.1 under linux (don't know if this matters)
so, anyway, maybe one of you can figure out where this behaviour comes from.
thanks in advance
i encountered a strange problem while writing a simple UDP client-server pair which cost me nearly an hour to track down.
i have my class UDPServer which waits for a datagram from the client and will then send a string back. I build the string using a string buffer like this:
Code:
tstamp = new java.sql.Timestamp(date.getTime());
Calendar now = Calendar.getInstance();
int seconds = now.get(Calendar.SECOND);
strbuf.append(rcvString).append(seconds).append(tstamp.getNanos());
// i now pass the stringbuffer to a bytebuffer
byte[] bufnew = new byte[256];
bufnew = strbuf.toString().getBytes();
packet = new DatagramPacket(bufnew, bufnew.length, addr, 1201)
socket.send(packet)
Now this looks like it should work, only it doesn't.
The client only receives the first part of the strbuf (rcvString) and doesn't get the rest.
but now comes the best part:
when i simply change the order in which i append stuff to the stringbuffer like so:
Code:
strbuf.append(tstamp.getNanos()).append(seconds).append(rcvString));
it works, wtf is going on there
maybe i am just dense, but i can't see what goes wrong.
btw i use netbeans 3.3.1 under linux (don't know if this matters)
so, anyway, maybe one of you can figure out where this behaviour comes from.
thanks in advance