gpt4 book ai didi

Java Socket 连接 -> 写入服务器时出现奇怪的崩溃

转载 作者:行者123 更新时间:2023-11-30 06:40:58 25 4
gpt4 key购买 nike

我最近正在制作一个小型 2D 平台游戏,并尝试实现一些在线 2 人游戏功能。有一个服务器程序,当然还有客户端。它的工作原理是这样的:你的客户端可以在 ID 1 和 ID 2 之间进行选择,并且 ID 直接发送到服务器,因此服务器可以决定接收哪些数据以及向你发送哪些数据(客户端 1 想要客户端 2 的坐标)例如)。

现在的问题是发送的数据以某种方式被操纵并且存在错误的值。此外,如果我发送了一个 true boolean 值,我的客户端就会在我发送 boolean 值的行停止,并且不会继续运行代码!

我在新的客户端程序中一一重建了“连接”方法,并且存在与我的游戏客户端相同的问题!

感谢您的建议!经过长时间的谷歌搜索,似乎没有人遇到同样的问题:-(

来自(重建)客户端的代码:

import java.io.*;
import java.net.Socket;

public class main {

public int x=5,y=10,id=2;
public int rX=0,rY=0;
public boolean receivedAttacked = false;
public boolean attacked=false;

public static void main(String[] args) {
System.out.println("Connecting...");
new main();
}

public main() {
try {
Socket socket = new Socket("localhost",6789);
DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
DataInputStream din = new DataInputStream(socket.getInputStream());

dout.writeByte(1);
dout.writeInt(id);
dout.flush();

dout.writeByte(2);
dout.writeInt(x);
System.out.print("X: " + x + " / ");
dout.flush();

dout.writeByte(3);
dout.writeInt(y);
System.out.print("Y: " + y + " / ");
dout.flush();

dout.writeByte(4);
System.out.print("Attacked: " + attacked + " / " + "\n");
dout.writeBoolean(attacked);
dout.flush();

int byteIndex = 3;
byte byteRead;
for(int i = 0; i < byteIndex; i++) {
byteRead = din.readByte();

switch(byteRead) {
case 1:
rX = din.readInt();
System.out.print("RECEIVED:" + "x: " + rX + " / ");
case 2:
rY = din.readInt();
System.out.print("Y: " + rY + " / ");
case 3:
receivedAttacked = din.readBoolean();
System.out.print("rAttacked: " + receivedAttacked + " / " + "\n");
default:
}
}
System.out.println("PAUSE");
dout.close();
din.close();
socket.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}

服务器代码(只是处理所有输入和输出的线程):

package me.server.lel;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.net.SocketException;

import javax.swing.JFrame;

public class ServerThread extends Canvas implements Runnable {

Socket incoming; //the client socket so to say

public int id;
public int x1 = 0,y1 = 0,x2 = 0,y2 = 0;
public boolean attacked1 = false, attacked2 = false;

public ServerThread(Socket incoming, int id) {
this.incoming = incoming;
this.id = id;
}

@Override
public void run() {

try {
//Reading the input and saving it in global variables
DataInputStream in = new DataInputStream(incoming.getInputStream());

byte index;
int byteAmount = 4;

for(int i = 0; i < byteAmount; i++ ){
index = in.readByte();
switch(index) {
case 1:
id = in.readInt();
break;
case 2:
if(id == 1) {
x1 = in.readInt();
} else if(id == 2) {
x2 = in.readInt(); ;
}
break;
case 3:
if(id == 1) {
y1 = in.readInt();
} else if(id == 2) {
y2 = in.readInt();
}
case 4:
if(id == 1) {
attacked1 = in.readBoolean();
} else if(id == 2) {
attacked2 = in.readBoolean();
}
default:
}
}

//Writing back the values of the opposite player
DataOutputStream out = new DataOutputStream(incoming.getOutputStream());

if(id == 1) {
out.writeByte(1);
out.writeInt(x2);
out.flush();

out.writeByte(2);
out.writeInt(y2);
out.flush();

out.writeByte(3);
out.writeBoolean(attacked2);
out.flush();

} else if(id == 2) {
out.writeByte(1);
out.writeInt(x1);
out.flush();

out.writeByte(2);
out.writeInt(y1);
out.flush();

out.writeByte(3);
out.writeBoolean(attacked1);
out.flush();
}
} catch(SocketException e) {
System.out.println("User Disconnected: " + incoming.getLocalAddress());
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}

正如您所看到的,我在客户端代码中做了一些控制台输出。因此,如果我按原样运行程序,这就是控制台输出:

Connecting...
X: 5 / Y: 10 / Attacked: false /
RECEIVED:x: 0 / Y: 33554432 / rAttacked: false /
rAttacked: false /

因此,您可以看到收到的每个值都有些错误,除了 boolean 值之外。现在,如果我进入客户端代码并将“attacked” boolean 值设置为 true,则输出将像这样运行:

Connecting...
X: 5 / Y: 10 / Attacked: true /

所以我假设代码只是停止并且不会继续执行代码(这发生在我的客户身上,然后甚至停止了)

很抱歉这篇文章很长,我希望我的代码足以供你们检查!非常感谢!

最佳答案

您忘记了客户端 cases 以及服务器中的 case 3case 4 中的 break。例如,当您的客户端发送第三个字节时,服务器会读取它,但也会传递处理 boolean 值的 case 4。这可能不是奇怪数值的来源,但我很确定这是 boolean 错误的原因。

尝试添加所有 break 语句,这样它就不会在执行时通过字节操作做一些意想不到的疯狂事情。

关于Java Socket 连接 -> 写入服务器时出现奇怪的崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44383632/

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