Java Minecraft不';不响应响应数据包
我正在使用库从头开始创建自己的服务器软件 现在,客户端向服务器发送一个握手数据包,服务器使用握手编解码器对其进行解码,并返回一条使用响应编解码器编码的响应消息,但我从未收到Ping数据包 下面是我的代码,用于将响应消息编码到服务器Java Minecraft不';不响应响应数据包,java,network-programming,minecraft,packets,Java,Network Programming,Minecraft,Packets,我正在使用库从头开始创建自己的服务器软件 现在,客户端向服务器发送一个握手数据包,服务器使用握手编解码器对其进行解码,并返回一条使用响应编解码器编码的响应消息,但我从未收到Ping数据包 下面是我的代码,用于将响应消息编码到服务器 public class ResponseCodec implements Codec<ResponseMessage> { @Override public ResponseMessage decode(DataInputStream dataInput
public class ResponseCodec implements Codec<ResponseMessage> {
@Override
public ResponseMessage decode(DataInputStream dataInputStream) {
String json = ByteUtilities.readUTF8(dataInputStream);
return new ResponseMessage(json);
}
@Override
public DataOutputStream encode(DataOutputStream dataOutputStream, ResponseMessage responseMessage) {
ByteArrayOutputStream packetArray = new ByteArrayOutputStream();
DataOutputStream packetStream = new DataOutputStream(packetArray);
ByteUtilities.writeVarInt(packetStream, 0x00);
ByteUtilities.writeUTF8(packetStream, responseMessage.getJson());
ByteUtilities.writeVarInt(dataOutputStream, packetArray.toByteArray().length);
try {
dataOutputStream.write(packetArray.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
return dataOutputStream;
}
}
在你发疯想弄明白这一点之前,花点时间确定minecraft是否真的收到了你的数据包是值得的。即使是本地服务器,也可能有很多防火墙挡住了你的去路。这样可以节省你很多时间。如果您知道它正在接收数据包,那么您需要进行艰苦的工作,以确保它在语法和上下文方面都是对服务器的有效请求。通过快速查看,您似乎错误地解码了数据包,表明您应该使用3个不同的数据包进行响应,但是你只有2个不同的代码packets@Ferrybig请求包并不重要。它不包含信息。@Neil正在检查它。我只是检查它是否发送了数据包。
server.setConnectionListener(new ConnectionListener() {
@Override
public void onConnect(Socket socket, DataInputStream dataInputStream) throws Exception {
//header
int packetSize = ByteUtilities.readVarInt(dataInputStream);
int packetId = ByteUtilities.readVarInt(dataInputStream);
// writing handlers
OutputStream outputStream = socket.getOutputStream();
DataOutputStream socketStream = new DataOutputStream(outputStream);
//identification
if(packetId == 0x00) {
// decode
HandshakeCodec codec = new HandshakeCodec();
HandshakeMessage message = codec.decode(dataInputStream);
if(!(message.getProtocolVersion() == 47)) {
System.out.println("Client " + message.getAddress() + ":" + message.getPort() + " seems to have a incompatible client.");
}
ResponseCodec responseCodec = new ResponseCodec();
StringBuilder text = new StringBuilder();
BufferedReader formatReader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("ResponseFormat.txt")));
String line = formatReader.readLine();
while (line != null) {
text.append(line);
text.append(System.lineSeparator());
line = formatReader.readLine();
}
String everything = text.toString();
String accountedWithVariables = everything
.replaceAll("MAX_PLAYERS", "" + maxPlayers)
.replaceAll("ONLINE_PLAYERS", "" + BasicServer.onlinePlayers)
.replaceAll("MOTD", motd);
ResponseMessage responseMessage = new ResponseMessage(accountedWithVariables);
responseCodec.encode(socketStream, responseMessage);
}
if(packetId == 0x01) {
long pingLong = dataInputStream.readLong();
PongCodec codec = new PongCodec();
PongMessage message = new PongMessage(pingLong);
codec.encode(socketStream, message);
}
}
@Override
public void onCaughtException(Exception e) {
System.out.println("Main thread has recieved a exception: " + e.getMessage());
e.printStackTrace();
}
});