byte[] message = ... Socket socket = ... DataOutputStream dOut = new DataOutputStream(socket.getOutputStream()); dOut.writeInt(message.length); // write length of the message dOut.write(message); // write the message