Java Socket Server Behind Two Clients

I am new to StackOverflow lol, but I rely on this site for a while. I have a question regarding the Java socket server I created. When connected (client and server), my application creates a thread for this client. This is a MMORPG game server ... at least I'm trying to be. With one player, he does not lag behind this badly. With two, however, he began to show some delays ...

If I spam left-right-left-right on one of the clients and normally move with the other, the other would feel buggy. I hope to get an assistant, since I spent more than a week and a half confused =) This is the time when I 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();
    }
}

How it all started! In the Player object, it looks like this:

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 Final Static. , ( ?)

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);
        }
    }
}
}

shortToByte_U16(), , ( ). : 5 , 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;
}

, , ?

EDIT: , , setTcpNoDelay true. , , / ... .

            Socket socket = serverSocket.accept();      // Check if we have a connection, otherwise wait
            socket.setTcpNoDelay(true);  // This helped a lot!!!
            Player player = new Player(playerCount++, socket, roomNumber);
            new Thread(player).start();

, ... /, , , .

+4
1

. =) setTcpNoDelay true . , , , . . . , . , while, , . =) , . , .

0

Source: https://habr.com/ru/post/1619318/


All Articles