Python字节对象与java
早上好,我有一个关于python中字节对象(表示为b'')和如何在java中复制它之间差异的快速问题
我正在做的项目是在一个模拟服务器上为一个死亡游戏做一些个人工作,以提高我的反转技能。我有一个python项目的工作格式副本,但我想切换到java,因为我对该语言的使用更好,并且它附带了许多对这样的项目有用的附加工具
我正在使用ServerSocket捕获java项目中的TCP数据
当数据从Python项目通过网络传输时,看起来有点像这样:
当我通过java ServerSocket捕获相同的数据时,我得到如下结果:
我的问题是如何重新格式化这个ASCII文本,以获得软件python版本中所看到的正确数据
目前,我能够获得如下输出:
while(true) {
try {
Socket socket = serverSocket.accept();
onConnection(socket);
byte[] incomingData = new byte[0];
byte[] temp = new byte[1024];
int k = -1;
//this is due to the client of said game not sending EOL (readLine() does not work here)
while((k = socket.getInputStream().read(temp, 0, temp.length)) > -1) {
byte[] tbuff = new byte[incomingData.length + k];
System.arraycopy(incomingData, 0, tbuff, 0, incomingData.length);
System.arraycopy(temp, 0, tbuff, incomingData.length, k);
incomingData = tbuff;
receiveData(socket, incomingData); <--- this is the important bit
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void receiveData(Socket socket, byte[] data) {
int lenLo = (int) (data[0]);
int lenHi = (int) (data[1]);
int length = lenHi * 256 + lenLo;
if(lenHi < 0) {
System.out.println("Invalid Packet Length");
}
if(data.length != length) {
System.out.println("Incomplete Packet Received");
}
try {
String test = new String(data, "UTF-8");
serverGUI.serverDebug(test); //produces the string in a jframe (pic 2)
serverGUI.debugByteArray(test.getBytes(StandardCharsets.UTF_8)); //produces the byte[] in jframe (pic 3 -- all bytes in this array are & 0xff prior to being printed out)
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
然而,这显然没有产生预期的结果。感谢您提供的任何建议或可以提供的任何资源
提前谢谢
共 (0) 个答案