gpt4 book ai didi

java - 通过套接字发送多个数据

转载 作者:行者123 更新时间:2023-11-30 02:02:42 26 4
gpt4 key购买 nike

我制作了一个多人贪吃蛇游戏,它通过套接字向对手发送实际得分和生命值。问题是在游戏过程中,敌人的生命值将是其得分。例如敌人有 90 生命值和 15 分。当敌人获得 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;
}

希望有人能发现问题或提出建议!谢谢!

最佳答案

发送多个数据不是问题。在本例中,Socket 工作在 TCP 模式下,因此写入顺序 = 读取顺序。为了避免序列化,我建议您以字符串形式发送分隔值,并使用 PrintWriter 来实现此目的。这将“一次”发送数据请参阅此示例:

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
)

关于java - 通过套接字发送多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52312823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com