Alron Posted September 14, 2013 Posted September 14, 2013 I have an issue where sending strings with the PacketHandler inserts some wierd null chars and doesnt send along some chars. I'm currently using this code to send strings: ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream dataStream = new DataOutputStream(byteStream); try{ dataStream.writeChars(str + "\n"); PacketDispatcher.sendPacketToServer(PacketDispatcher.getPacket(ModInfo.CHANNEL, byteStream.toByteArray())); }catch(IOException ex){ System.err.append("Faild to send String"); } and this to recieve: String str = reader.readLine(); What am I doing wrong, or whats the proper way of sending strings?
fhntv24 Posted September 14, 2013 Posted September 14, 2013 for me,that works better. //reciving string int n=data.readInt(); for (int i=0;i<n;i++) name=name+data.readChar(); //sending string out.writeInt(name.length()); int n = name.length(); for (int i=0;i<n;i++) out.writeChar(name.charAt(i));
Alron Posted September 14, 2013 Author Posted September 14, 2013 Thanks for the answers! readUTF and writeUTF works perfectly for me.
Recommended Posts