Java socket server lags behind two clients
I'm a newcomer lol to stackoverflow, but I've always relied on this website I have a question about the java socket server I created When connecting (client and server), my application creates a thread for the client This is an MMORPG game server... At least I've tried There's a player who won't fall behind so badly However, there are two, it starts to show some lag
If I send spam left and right – left and right – on one client and move normally with the other client, the other will feel bad I hope to get some assistants because I've spent a week and a half struggling =) it's time for me to ask for help
The code is simple:
public static void main(String[] args) throws IOException{
serverRooms.put(roomNumber,new Room());
try {
System.out.println("Starting Server...");
serverSocket = new ServerSocket(9595,20);
System.out.println("Server Started");
while(run){
Socket socket = serverSocket.accept(); // Check if we have a connection,otherwise wait
Player player = new Player(playerCount++,socket,roomNumber);
new Thread(player).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
That's how it all starts! On the player object, it looks like:
public void run() {
while(playerIsConnected) {
try {
int msgid = input.readUnsignedByte();
messageHandler(this,msgid);
} catch (IOException e) {
System.err.println("Player have signed off");
playerIsConnected = false;
}
}
// If Player leaves,close socket,and end thread
try {
socket.close();
} catch (IOException e) {
System.out.println("We got an error while closing a socket on player " + pid + ".");
}
}
Messagehandler happens to be a static method of the final static class It is a global method that can be called by each thread (can this be the reason for the lag?)
public final class MessageControl {
public static void messageHandler(Player player,int msgid) throws IOException{
DataInputStream input = player.getInputStream();
switch (msgid) {
case 10:
byte hspd = (byte) Math.signum(input.readByte());
byte vspd = (byte) Math.signum(input.readByte());
byte dir = input.readByte();
updatePlayerPosition(player);
byte spd = (byte) (hspd != 0 && vspd != 0 ? player.spd-1 : player.spd);
// Prepare packet and send to clients
ByteBuffer buffer = ByteBuffer.allocate(11);
buffer.put((byte) 10);
buffer.put(shortToByte_U16(player.pid));
buffer.put(shortToByte_U16(player.x));
buffer.put(shortToByte_U16(player.y));
buffer.put((byte)(hspd*spd));
buffer.put((byte)(vspd*spd));
buffer.put((byte)(dir));
sendPacketToAllClients(player,buffer,true);
// Update Player info
player.hspd = (byte) hspd;
player.vspd = (byte) vspd;
player.dir = dir;
player.lastUpdate = System.currentTimeMillis();
break;
}
private static void sendPacketToAllClients(Player player,ByteBuffer buffer,boolean includeMe){
for (Player otherPlayer : player.room.getPlayersInRoom()){
if (otherPlayer.pid != player.pid || includeMe){
sendPacketToClient(otherPlayer,buffer);
}
}
}
}
About shorttobyte_ U16 (), I just created a simple method to short the cone to bytes (send buffer packets to the client through bytes) For example, I have about five such conversions, including conversion to unsigned U16
public static byte[] shortToByte_16(int x){
short s = (short) x;
byte[] ret = new byte[2];
ret[0] = (byte)(s & 0xff);
ret[1] = (byte)((s >> 8) & 0xff);
return ret;
}
Look at the structure below, why would I fall behind any ideas?
Editor: I think I can improve it by setting settcpnodedelay to true When I spam left / right at my end, there still seems to be a lag... Other players on my screen seem very uncomfortable
Socket socket = serverSocket.accept(); // Check if we have a connection,otherwise wait
socket.setTcpNoDelay(true); // This helped a lot!!!
Player player = new Player(playerCount++,roomNumber);
new Thread(player).start();
From what I see... My "spam left / right" end seems to lack packets sent by the server
Solution
Problem solved. =) Settcpnodedelay true did a trick About the part I said I lost the packet, I actually didn't The two messages are combined and passed in as one message My program only reads the first few bytes and ignores the rest You have to put a byte in front to indicate the size of the message Once in place, I set up a while loop to read it until it can no longer read. (=) Thank you for helping me My first article, this is a grand experience
