简体   繁体   中英

Sending multiple data over socket

I made a multiplayer snake game which is sending the actual score and health to the opponent over socket. The problem is during the game, the enemies health will be its score. Example the enemy has 90 health and 15 score. When the enemy get 1 score it health will be 16 and the score remains 15. I think the problem is somewhere in the server:

private boolean listenForServerRequest() {
    Socket socket = null;
    try {
        socket = serverSocket.accept();
        dos = new DataOutputStream(socket.getOutputStream());
        dos2 = new DataOutputStream(socket.getOutputStream());
        dis = new DataInputStream(socket.getInputStream());
        dis2 = new DataInputStream(socket.getInputStream());
        accepted = true;
        System.out.println("Client has requested and joined the game");
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

private boolean connect() {
    try {
        socket = new Socket(ip, port);
        dos = new DataOutputStream(socket.getOutputStream());
        dos2 = new DataOutputStream(socket.getOutputStream());
        dis = new DataInputStream(socket.getInputStream());
        dis2 = new DataInputStream(socket.getInputStream());
        accepted = true;
    } catch (IOException e) {
        System.out.println("Unable to connect to the address: " + ip + ":" + port + " | Starting a server");
        return false;
    }
    System.out.println("Successfully connected to the server.");
    return true;
}

private void initializeServer() {
    try {
        serverSocket = new ServerSocket(port, 8, InetAddress.getByName(ip));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public boolean getConnected() {
    return this.connected;
}

public void sendHealth(SnakeHead snakeHead) {
    try {
        dos.writeInt(snakeHead.getHealth());
        System.out.println(snakeHead.getHealth());
        dos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void sendScore(SnakeHead snakeHead) {
    try {
        dos2.writeInt(Globals.getScore());
        System.out.println(snakeHead.getHealth());
        dos2.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public int getEnemyHealth() {
    try{if (dis.available() != 0 ) {
        try {
            enemyHealth = dis.readInt();
            return enemyHealth;
        } catch (IOException e) {
            e.printStackTrace();
        }

    }}catch (IOException e){
        e.printStackTrace();
    }
    return enemyHealth;
}
public int getEnemyScore() {
    try{if (dis2.available() != 0) {
        try {
            enemyScore = dis2.readInt();
            return enemyScore;
        } catch (IOException e) {
            e.printStackTrace();
        }

    }}catch (IOException e){
        e.printStackTrace();
    }
    return enemyScore;
}

Hope someone will find the problem or has any advice! Thanks!

使用socket.getOutputStream()每次您必须在包含得分和运行状况的struct中发送数据时,或者在按顺序分别发送它们(如首先运行状况然后是score或Vicevesa)时,都将获得相同的流。

Sending multiple data is not a problem. Socket works in TCP mode in this example so write order = read order. To avoid serialization, I would suggest you to send separated values in form of String and use PrintWriter for this purpose. This would send data in "one shot" See this example:

try (
    Socket echoSocket = new Socket(hostName, portNumber);
    PrintWriter out =
        new PrintWriter(echoSocket.getOutputStream(), true);
    BufferedReader in =
        new BufferedReader(
            new InputStreamReader(echoSocket.getInputStream()));

    // reading
    String userInput;
    while ((userInput = stdIn.readLine()) != null) {
        System.out.println("echo: " + in.readLine());
    }

    // writing
    out.println(int + "," + int); // multiple data
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM