I’m having a strange problem and I hope that I’m not trying to do anything stupid here.
What I am trying to do is just send a String to the SGS and then echo the ByteBuffer back out on a channel to all the connected clients (just one at the moment).
What I am finding is if once the data is recieved at the server, I get all the bytes out of the ByteBuffer and then send the ByteBuffer on, the information is sent back to the client correctly. If I just send the ByteBuffer on, the information dosen’t get sent correctly to the clients (the ByteBuffer that is recieved dosn’t contain any data, capacity = 0).
To send the data I am doing this:
ByteBuffer buffer = ByteBuffer.allocate(message.length());
buffer.put(message.getBytes());
toServer.sendData(buffer);
toServer.sendData does this:
public void sendData(ByteBuffer bytes) {
mgr.sendToServer(bytes, true);
}
at the recieving end I am doing this:
public void dataArrived(byte[] from, ByteBuffer data, boolean reliable) {
int remaining = data.remaining();
System.out.println("remaining: " + remaining + " capacity: " + data.capacity());
byte [] message = new byte[remaining];
data.get(message);
System.out.println("raw message is: " + new String(message));
}
and in the server I am doing this:
public void userDataReceived(UserID from, ByteBuffer data) {
SimTask task = SimTask.getCurrent();
byte[] dummy = new byte[data.remaining()]; //comment out these two lines and it dosn't work!!
data.get(dummy); //comment out these two lines and it dosn't work!!
for(UserID id : users)
{
task.sendData(gameChannel, id, data, true);
}
}
If I don’t have the two lines mentioned in the commentsthere I get a capacity 0 ByteBuffer at the client. I did try just doing a data.get(); and then sending the buffer on but that way I only got the first charceter of my message.
users is a list that I add clients too as they connect
public void userJoined(UserID uid, Subject subject) {
Set<Principal> principals = subject.getPrincipals();
Principal principal = principals.iterator().next();
System.err.println("User Joined server (P): " + uid + " (" + principal.getName() + ")");
users.add(uid);
SimTask.getCurrent().addUserDataListener(uid, thisobj);
}
Is there something really obvious that I’m missing here?
Thanks in advance,
Dan.