Some tips you might need :
When you sending some data to the user, you might want to tell the client what type of info you are sending.
Is it a Message? Is it a Ban Message? Is it a Invite to a new Room?
So, before every message, you could have, like a integer,
If its 0 , its a message.
If its 1, its ....
Heres a little example
Got from my old chat Code... Bring memories

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| public void errorMessage(String message,Socket socket) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeByte(7); dos.writeUTF(message); byte[] msg = bos.toByteArray();
DataOutputStream out = new DataOutputStream(socket.getOutputStream()); out.writeInt(msg.length);
out.write(msg); out.flush();
} catch (Exception e) { System.out.println("Error in Transmitter Class! (Connection lost ?)" + e.getMessage()); } } public void defaultBlackMessage(String message, Socket socket) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeByte(0); dos.writeUTF(message); byte[] msg = bos.toByteArray();
DataOutputStream out = new DataOutputStream(socket.getOutputStream()); out.writeInt(msg.length);
out.write(msg); out.flush();
} catch (Exception e) { } } |