Two Laptop Bag*
The moose likes Sockets and Internet Protocols and the fly likes help with tcp causing lag in ping pong game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "help with tcp causing lag in ping pong game" Watch "help with tcp causing lag in ping pong game" New topic
Author

help with tcp causing lag in ping pong game

Bruno Mesta
Greenhorn

Joined: Dec 01, 2009
Posts: 18
Hello,
we are developing a multi networked ping pong game in java for class and the problems we are facing is that the game lags for some reason and it ruins the whole game. Our server is responsible for controlling the game and etc. We are using TCP to send messages constantly to the server with the following info: paddle x and y coordinate, paddle length, paddle height, ball x and y coordinate, and ball radius. Then the server broadcast the message to everyone the gui can be updated with the info that the server sent. The client and server are sending and receiving messages to each other every 40ms. We have tried running our program in different comp and we still have the same problem. does anyone have an idea how we can resolve this issue? We are thinking that the buffer can handle this constant messaging. If you need code i can post some code just let me know. I really need t fix this bug its driving me insane
Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
hi. I had such an idea a year ago. I also wanted to create a ping pong game. you need to make a synchronization algorithm between the 2 players.
One thing is to limit the access to the players with low internet connection. Second you will need to synchronize the ball position. so the player with a lower connection will see the ball blinking.
At a first look the game looks very simple but when you try to create it you'll see that is one of the most complicated, because both player need to see the ball position at the same position in the same time, so creating a system "hit and forget" will not work on this. At least this were the problems that I encountered when I wanted to start the game.
You can also use UDP instead of TCP, I never tried with UDP because I wanted to build the client in flash( and flash supports only TCP).

Hope this helped you
Bruno Mesta
Greenhorn

Joined: Dec 01, 2009
Posts: 18
do you really think i need to implemnt a synchronization algorithm? If yes, can you suggest any good ones? at the mome it semms synchronize for the first 30 sec and then it starts lagging like crazy. I cant use UDP because our prof wants us to use TCP. Did you ended up implementing it?
Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
No. I didn't finish it.
Well like I said. first try to determine the internet speed of every player. the player with a lower connection will need to be synchronized with the other player. you said that you process the data on server side.
one thing is to update the ball position every x seconds,but for the player with the lower connection you'll need to create an algorithm that will place the ball on a bigger position( how much can the ball move in the lag time).

another thing is for both clients to send the ball position to the server and then make a synchronization for the player with a smaller connection.


Big problems can appear when the player with a faster connection hits the ball , because the the player with lower connection will see the ball in the air. you'll need to synchronize that too. Like I said , when I saw how many problems I have to deal I give up. The big problem with this real time game is that you need precision. On other game types you can use the hit and forget.
Bruno Mesta
Greenhorn

Joined: Dec 01, 2009
Posts: 18
wow this seems more complicated than i thought. i guess the prof didn't think of this when she assigned us the project. We are running it on the school computer lab. Don't you think all clients will have the same internet speed? How can i determine the internet speed of each player?
Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
well yea. try it on localhost. open 2 clients on the same computer and see how is working. if there is lag you should also check your code too see if there are any irregularities in sending the parameters.

it's not necessary to determine the connection speed. you just need the time it took for a client to connect to the server. just ping your server

But in my opinion you can go to your teacher with this game even if it has lag. I think she is more interested in the implemented principle, not in making a commercial product.

Usually for real time games you need to use UDP not TCP.
Bruno Mesta
Greenhorn

Joined: Dec 01, 2009
Posts: 18
i was talking to one of my classmates and he told to use a ArrayBlockingQueue<String> because i might be overloading the network. I'm going to give that a try. He says his game isn't lagging thou. how weird is that? We will see what she says. I want solve this issue because i want to put in my personal website. Ill let you know how it goes. when i run it under local host it runs a little bit more smoother but it still lags slightly
Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
well yea. use a thread pool executor( java 5 or higher) and save the tasks in a list. also give more RAM memory to the application. (default is 64MB)
be careful that ArrayBlockingQueue has a limited amount of taks. I like to use LinkedBlockingQueue<Runnable>. also try to send as much few information as you can.

Usually the lag should not appear on the same computer or on your home network.

don't use the base Thread implementation and in the runnable use something like while(true){....} . every time the server send some information to the clients create a new task(runnable) and add it to the ThreadPoolExecutor
Also use a TimerTask to update the clients with information.


you should just send the ball trajectory and then on client side the ball should move on it's own until another action is taken and you need to update the trajectory again
but if you want to use it only on you home/school network updating all the info every x sec it's ok.

also see how much memory is taken by your server. check to see if there are any loops that eat you memory making it running slow and creating a lag.

Bruno Mesta
Greenhorn

Joined: Dec 01, 2009
Posts: 18
i just finished implementing the ArrayBlockingQueue and didnt have no luck. I thoguht it would solve the problem. The lag I'm having is only shown in the client side and is very slightly. The server works perfectly. I also made sure i flush the printwriter after we send a message. In my runnable I use while(true){..} what you mean by the ball should move on its own? should i use a timer on the client side? cuz i only have one on the server side. my prof said that we should only be sending the info of the objects. I use a timertask to keep updating the info. what you mean by the ball trajectory? can you be a lil more descriptive
Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
well. It's a 2D game right? the ball has an X and Y position. so the server sends the client the equation y=2x+3 for example. and you start to move the ball on the client side using this equation until the client receive a new equation from the server. you select a step of let's say 2 pixels, and you increase/decrease x with 2 pixels, and based on the equation received you calculate the y. This implementation is good because you're not sending a lot of information, but can create a lot of problems.
Like I said in the previous post , the way you implemented should work on your home network or localhost.

sending information using while(true){...} can create lags, because you send a lot of information without having a confirmation if the client received that info and most important when.

you can use a ThreadPoolExecutor and schedule tasks to send the information, or you can implement a system of sendind the info and receiving the confirmation
for example

while(true){
boolean ok=sendInfo(info);
if(ok){..}else{}
}

where sendInfo sends data to the client and receive a confirmation from the client.
Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
Take a look at this presentation:
http://www.electrotank.com/Presentations/FF/SF2004/FFSF2004.swf

it's supposed to be for flash, but the principle is the same t doesn't matter the programming language . see the latency chapter

Hope this helped you.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Just another cross poster
http://forums.sun.com/thread.jspa?threadID=5432483&messageID=10954521#10954521


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with tcp causing lag in ping pong game
 
Similar Threads
Pong
help with lag in multinetwork ping pong game
Physics
Official, Blue, Red player multi-threaded problem
How to make yield() work as expected