Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
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  
  [FIXED][TCP] Movement delays  (Read 1797 times)
0 Members and 1 Guest are viewing this topic.
Offline Apex5

Junior Member


Medals: 1
Projects: 1



« Posted 2012-02-04 17:03:25 »

I've been trying to fix this annoying little headache for a while now; I've got a completely tile based, networked game (entities/player moves one tile at a time) which runs pretty much fine in Eclipse, however there are two problems that can occur.

1. Whilst running in Eclipse a client can sometimes see delays in hitting a key and his player moving if the client is running a while.
2. When running from a exported Jar file any client running for a while will generally notice the delay.

As far as I can tell this is not related to the speed at which the server is sending/receiving data to/from the client; essentially the process is like this:

1. Client KeyListener picks up input and sends it to the server using a PrintWriter
2. A server input thread picks this input up and looks up the world the player is in by using whatever key the player has stored in the player class to find the world in a hashlist (the server supports multiple worlds(maps) being run simultaneously).
3. A move method in the world class moves the player in the "world" depending on the key pressed.

As far as I can tell this can take between 20000 - 200000 nanoseconds averaging at about 50000 ( 0.00005 Seconds)

4. The serverOutput Thread for that client (which is continuously sending data at about 50fps) then sends a serialized object containing a string representation of a small chunk of the map around the player to the ClientInput thread (which gets the data at the same fps as the server output) and stores it.
5. The client Thread handling graphics is continuously rendering that store of information at 43(ish) fps to a JPanel.

As far as I can tell there shouldn't be any delay here, when the delay occurs it does not seem to be affected by multiple clients joining/disconnecting.

I apologise that this is not alot to go on, I'm probably doing something really stupid and blindingly obvious, but I can't seem to find it.
If you're willing to take a look at the source code it can be found here (the two jars in the top directory are the most recently compiled client/server, the server needs to be terminated in task manager afterwards.):

Source: (Removed due to fix)
(Hit 1 to get rid of the intro pic)
Video showing bug with terrible voice commentary ^^: http://www.youtube.com/watch?v=0qChX1uzQ6Y&feature=youtube_gdata

I've only been programming around 5 months now so pity me the rough design, I mostly made up how it was going to work as I went along; also the reason why the world loading is so awkward is because I'm going to implementing loading from object streams later with data already in the entities, I haven't got round to implementing spritesheets yet too ^^.

Any help of any sort/advice on architecture is appreciated Cheesy I don't expect anyone to look through my code for me, but a helpful pointer in how to solve it would be awesome, I've been banging my head against a wall for a while now.
Offline theagentd
« Reply #1 - Posted 2012-02-04 17:40:03 »

Try to call this method (with true) on every socket you open: http://docs.oracle.com/javase/1.4.2/docs/api/java/net/Socket.html#setTcpNoDelay(boolean). That could be it. Sorry, I don't want to bother looking through your whole code...

Myomyomyo.
Offline Apex5

Junior Member


Medals: 1
Projects: 1



« Reply #2 - Posted 2012-02-04 18:10:07 »

It worked!
Thank you kind sir, you've just made my day Cheesy
*Shakes fist at minimum posts needed to "appreciate" someone

And after all that trouble I only needed two lines of code Tongue
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-02-04 18:27:10 »

Yeah, I remember ripping my hair off about 5 years ago when I had added networking to a tile-based MMO-game (I was still a newbie, but an MMORPG shouldn't be that hard to make, right?). It worked perfectly when I tested it locally, but over LAN (cable, not wireless) I got either 0 or 300-1000 ms delays randomly, but I don't remember any build-up of the problem though (I probably didn't notice it xD). When I finally found out what the problem was and added that magic line... Kind of annoying, but I guess TCP isn't really made for games primarily.

That method controls Nagle's algorithm, which basically is an algorithm to improve throughput at the cost of latency. Simplified it seems to use both a threshold and a time limit on the data you send, meaning that if you don't write "enough" data it will buffer it for a few milliseconds because it's waiting for more data to send in the same TCP packet to avoid the overhead of sending many small packets. I suppose what's happening for you is that because you're not sending much data per second it's increasing the time it waits before actually sending the data.

Myomyomyo.
Offline Apex5

Junior Member


Medals: 1
Projects: 1



« Reply #4 - Posted 2012-02-04 18:37:45 »

That's pretty much what's happened for me Cheesy; thanks for the info, I guess when they created it they weren't thinking of games when they made it but things like big database files or something, the local map I'm sending only really changes a few string values per send.

I would say I wish they'd put stuff like this in with the networking trail, but at the same time I should probably have looked through all the documentation anyway ^^

Thank you so much, I don't think I could've found the method on my own Tongue
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (46 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (36 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (51 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!