DataInputStream corruption

Hey,
I’m sending messages like “J&0&1&john” to the server and the server sends the message to all the clients. The server receives and send the messages, but the client receives rubbish or nothing after a while. “J 0 1 john” becomes " 0 j hn" or something like that. The server sends the messages to all the sockets with a for loop. I’m using dataoutputstream and datainputstream. What could be the problem?
Sincerely

Here is the code where the server sends messages to the clients:
public void send(String text)
{
System.out.println("** sending: "+text);

	for(int i=0;i<oda.getClients().size();i++)
	{
		Client s=(Client)oda.getClients().elementAt(i);
		output=s.getOutput();
		try {
			output.writeUTF(text);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

and the code of the client:

public void run()
{
while(true)
{
try
{
message=input.readUTF();

			if(message==null)
				return;
			else
				interpret(message);
			
			System.out.println("** incoming: "+message);
		}
		catch(SocketException e)
		{
			stop();
			try
			{
				socket.close();
			}
			catch(IOException ex)
			{
				ex.printStackTrace();
			}
			e.printStackTrace();
		} catch(IOException e)
		{
			System.exit(0);
		}
	}
}

I’m using NIO and ByteBuffers, in buffer I put array of bytes of the String I want to send, works perfectly so far, although reading the messages isn’t the best.

example of sending:


...
           try {
                byte[] temp = net_say.getBytes("UTF-16BE");
                message.putInt(temp.length);
                message.put(temp);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
...

Anyway do you have that corrupted messages ocasionly at random or are they all like that and continue to be always corrupted after some time?

A DataInput/DataOutput pair using read/writeUTF are guaranteed not to corrupt the bytes, whatever happens. So either you’re sending more messages than you think you are, or (very possible) your sending data from multiple threads, and I’m fairly sure the DataOutputStream.writeUTF is not synchronized.

Problem solved using PrintStream and BufferedReader.
Thanks for replies!

IIRC the methods of PrintStream are synchronized, so you if it’s fixed now, you’re probably sending data from multiple threads.

That way:


out.print("hello");
out.print(" ");
out.print("world");
out.println();

is not guaranteed to result in “hello world” on the receiving end of the stream, even if you use PrintStream which seems to solve your problems, but only in certain cases.

there are guarantees and then there are certainties. A guarantee only means
that when it happens, it’s a bug. It doesn’t mean that it won’t happen.

My game server uses TCP streams which are “guaranteed” to be reliable, but
occasionally a transaction is corrupted. I add my own checksum to each
transaction to catch these. The current report (which is typical)

17 checksum errors 429560k transactions 2602m in 887m out

Obviously, this is a pretty low rate of error, and I can’t prove that the problem
isn’t some incredibly rare bug in my code - 17 errors in half a million transactions -
but the bottom line is that there will ALWAYS be errors, so log it, invesitgate it,
and live with it.

Yup, but in this case the failrate is about 100% (after a while) and it’s kinda obvious what causes it (writing from multiple thread to 1 stream)

another way to subtly fail is to mix I/O styles to the same stream, even if you’re careful
to keep the byte count just right. By this I mean mixing WriteInt and WriteBytes()
matched by ReadInt() and ReadBytes() can fail for some stream types,
but this results in hard errors such as “ArrayIndexOutOfBounds” and “StreamCorrupted”
rather than silently corrupted data.