简体   繁体   English

通过套接字发送多个数据

[英]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. 例如,敌人的生命值为90,得分为15。 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: 当敌人得到1分时,它的生命值为16,而得分仍然为15。我认为问题出在服务器的某个位置:

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. 在此示例中,套接字以TCP模式工作,因此写入顺序=读取顺序。 To avoid serialization, I would suggest you to send separated values in form of String and use PrintWriter for this purpose. 为避免序列化,建议您以String形式发送分隔的值,并为此使用PrintWriter。 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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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