Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  DataInputStream corruption  (Read 3647 times)
0 Members and 1 Guest are viewing this topic.
Offline selimeki

Senior Newbie





« Posted 2007-04-03 18:34:22 »

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);
         }
      }
   }
Offline Kova

Senior Devvie





« Reply #1 - Posted 2007-04-03 20:10:33 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
...
           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?
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2007-04-03 21:56:56 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline selimeki

Senior Newbie





« Reply #3 - Posted 2007-04-03 22:58:45 »

Problem solved using PrintStream and BufferedReader.
Thanks for replies!
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2007-04-04 08:44:48 »

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

That way:
1  
2  
3  
4  
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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline ddyer
« Reply #5 - Posted 2007-04-23 20:06:50 »

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.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2007-04-23 20:38:06 »

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)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline ddyer
« Reply #7 - Posted 2007-04-24 17:29:38 »

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.
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1672 views)
2018-06-10 19:43:48

EgonOlsen (1720 views)
2018-06-10 19:43:44

EgonOlsen (1159 views)
2018-06-10 19:43:20

DesertCoockie (1586 views)
2018-05-13 18:23:11

nelsongames (1186 views)
2018-04-24 18:15:36

nelsongames (1714 views)
2018-04-24 18:14:32

ivj94 (2540 views)
2018-03-24 14:47:39

ivj94 (1766 views)
2018-03-24 14:46:31

ivj94 (2845 views)
2018-03-24 14:43:53

Solater (971 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!