Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  [KryoNet] Client can't send and receive at the same time ?  (Read 1151 times)
0 Members and 1 Guest are viewing this topic.
Offline Positive

Junior Newbie





« Posted 2013-05-06 19:14:36 »

Hi,

I'm trying to code a simple client/server using KryoNet but I'm struggling with something, my client can't receive packets while sending.

When I hold the right key I want my player to move 1pixel to the right. In my situation when I hold the right key my player moves 1 pixel to the right, then stop moving, and finally when I release the key he literally teleports himself in the right direction. I other words the client receive almost all the packets at one time.

Here's my code:

Client:
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  
public void launch() throws SlickException, IOException {
      AppGameContainer container = new AppGameContainer(this);
      container.setMinimumLogicUpdateInterval(10);
      container.setMaximumLogicUpdateInterval(10);
      container.setUpdateOnlyWhenVisible(false);
      container.setAlwaysRender(true);


      client = new Client();
      ClassRegister.register(client.getKryo());
      client.addListener(new CustomListener(this));

      client.start();

      if (!connected) {
         Log.set(Log.LEVEL_TRACE);
         try {
            client.connect(5000, host, port);
            connected = true;
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
      container.start();
   }


CustomListener:
1  
2  
3  
4  
5  
6  
7  
8  
@Override
   public void received (Connection connection, Object object) {
     
      if (object instanceof MoveResponse) {
         MoveResponse response = (MoveResponse) object;
         getPlayer().move(response.dir);
      }
   }


Server:
1  
2  
3  
4  
5  
6  
7  
8  
private void start() throws IOException {
      server = new Server();
      ClassRegister.register(server.getKryo());
      server.bind(port);
      server.addListener(new ServerListener(this));
      server.start();
      Log.set(Log.LEVEL_TRACE);
   }


ServerListener:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
@Override
   public void received(Connection connection, Object object) {
                if (object instanceof MoveRequest) {
         MoveRequest request = (MoveRequest) object;
         
         MoveResponse response = new MoveResponse();
         response.dir = request.dir;
         connection.sendTCP(response);
      }
   }


Input:
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  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
public void update() {
      movePlayer();
   }

private void movePlayer() {
      if (pressedRight) {
         sendMoveRequest(3);
      } else if (pressedUp) {
         sendMoveRequest(1);
      } else if (pressedDown) {
         sendMoveRequest(2);
      } else if (pressedLeft) {
         sendMoveRequest(4);
      }
   }

   public void keyPressed(int key, char c) {
      if(key == goDown) {
         pressedDown = true;
      } else if(key == goLeft) {
         pressedLeft = true;
      } else if(key == goUp) {
         pressedUp = true;
      } else if(key == goRight) {
         pressedRight = true;
      }
   }

   public void keyReleased(int key, char c) {
      if(key == goDown) {
         pressedDown = false;
      } else if(key == goLeft) {
         pressedLeft = false;
      } else if(key == goUp) {
         pressedUp = false;
      } else if(key == goRight) {
         pressedRight = false;
      }
   }



I seriously don't see where I'm messing up  Sad Any help?
Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #1 - Posted 2013-05-06 19:35:47 »

You can send from any thread. Your send will either happen immediately (which is very likely) or it will be queued and later sent on the network thread. You always receive on the network thread. If you manipulate your game state from the network thread, you can have synchronization problems with your game thread. Also, while you block the network thread, you won't be able to receive more objects and won't be able to send data if the your send had to be queued. You can use Listener.QueuedListener to add runnables to a list, then in your game thread you can run these. Be sure to use proper synchronization for this list though (eg, in a synchronize block on the list, copy the contents to a second list, clear the list, then run the runnables in the second list outside of the synchronize block).

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.

theagentd (11 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!