Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
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  
  Incorrect data from server  (Read 1003 times)
0 Members and 1 Guest are viewing this topic.
Offline akzyl

Senior Newbie





« Posted 2012-01-16 10:19:26 »

Hello.
I'm working on simple real time 2D multiplayer shooter and now I started working on server.
To connect with server I decide to use DataInputStream and DataOutputStream.
For now I have simple test:
- if space key is pressed data are sending to server
- server recognizing data type and sending this data to others player (in theory)
- client/player receiving data from server
Connecting is working sending data to server and receiving also. Unfortunately weary often I'm getting wrong data
and I don’t know why.
Some code

I'm sending this data when space is pressed (this work fine)
1  
2  
3  
4  
Connector.getInstance().getOutToServer().writeInt(667);
Connector.getInstance().getOutToServer().writeInt(13);
Connector.getInstance().getOutToServer().flush();
Connector.getInstance().getOutToServer().close();


Server thread (also working fine)
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  
public void run() {
      int gameId = 0;
      int playerData = 0;
      try {
         iss = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
         oss = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));
         while (true){
            gameId = iss.readInt();
            if(gameId == 667) {
               playerData = iss.readInt();
               for (int i = 0; i <= 9; i++) {
                  //if (clients[i] != this) {
                    if (clients[i] != null){
                        System.out.println("---sending: "+gameId+" "+playerData);
                        clients[i].oss.writeInt(667);
                        clients[i].oss.writeInt(13);
                        clients[i].oss.flush();
                     }
                  //}
              }
            }
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }


Recipient thread (place where I'm geting bad data)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void run(){
      try {
         while(true){
            data = Connector.getInstance().getFromServer().readInt();
            if(data == 667) {
               int i = Connector.getInstance().getFromServer().readInt();
               System.out.println("byte b: " + data + " int: "+i);
            }
            Thread.sleep(1);
         }
         //Connector.getInstance().setClosed(true);
     } catch (InterruptedException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }


The 'i' variable very often displaying strange number (0 and 155) but always should be 13 why this happening and how to fix it?
I hope it's enough information.
Offline ra4king

JGO Kernel


Medals: 341
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-01-16 10:24:48 »

Are you sending data in different threads of the same app?

Offline akzyl

Senior Newbie





« Reply #2 - Posted 2012-01-16 10:30:33 »

Are you sending data in different threads of the same app?
yes
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 341
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-01-16 10:37:55 »

Well then you have your answer. Make sure to properly synchronized access to the Output streams Wink

Offline akzyl

Senior Newbie





« Reply #4 - Posted 2012-01-16 11:02:35 »

Well then you have your answer. Make sure to properly synchronized access to the Output streams Wink
You right ra4king.
After few hours I back to this problem and now I see, Output streams wasn’t properly synchronized (he wasn't synhtonized at all).
Thanks for help
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (16 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!