I'm creating a multiplayer game, which creates a new thread for each client, and then listens on a socket for strings via input and output streams. On the client side I also have a thread separate from the main game thread, but this one will send the updates to the server 60 times per second with a two letter string as the key for what I want to happen. When I request my "general update" (code RP) on the client side, the server sends the size of the entity array OK, then there seems to be a very long delay for the stream to become ready again.
I'm relatively sure this is the code of the problem area, after lots of testing to debug.
On the server side:
if(inputLine.equals("RP")){ //this is the main update
timetoupdate = System.currentTimeMillis(); //start timer
ArrayList<Player> currentplayers =world.getPlayers(); //get the list of players
out.println(currentplayers.size()); //send the size
for(int pl = 0; pl<currentplayers.size();pl++){ //loop through
Player player = currentplayers.get(pl); //get the player object
out.println(""+pl);
out.println(player.getUsername());
out.println(player.getType());
out.println(player.getXloc());
out.println(player.getYloc());
out.println(player.getXvel());
out.println(player.getYvel());
out.println(player.getPressing());
out.println(player.getLookingxloc());
out.println(player.getLookingyloc());
out.println(player.getTeam());
out.flush();
/*
id
username
class(type)
xloc
yloc
lxloc
lyloc
*/
// Just gaveout all info on player.getUsername()
}
System.out.println("Time this ("+inputLine+") part takes:"+(System.currentTimeMillis()-timetoupdate)); //stop timer
On the Client Side
out.println("RP"); //tell the server we are generally updating
String playercountstring = in.readLine(); //get the array size
int playercount = Integer.valueOf(playercountstring); //convert to int
for (int i = 0; i < playercount; i++) {//loop through all players
long timetoupdate = System.currentTimeMillis(); //start timer
while(!in.ready()){}//wait for input to become ready
System.out.println("time this (to ready) part takes:"+(System.currentTimeMillis()-timetoupdate));//stop timer
String toprocess = in.readLine(); //get id of player
... and continues to get all info on this player
As you'll notice I am timing how long the offending areas take to complete: the server takes less than 1 millisecond, yet just that one area of the client takes 200 millisecs, especially weird as the previous line collecting the size of the array takes less than 1 millisecond too. I'm really at a loss here as to what the issue is, I don't necessarily even need a solution just an explanation would be great. Thanks!