我正在尝试构建一个基本的代理 4 服务器,我必须解析一个数据包并提取其信息,如下所示:
1 byte (version)
1 byte (command)
2 byte (port)
4 byte (ip)
X byte (userID, builds a string by looping until '\0' is found)
到目前为止,这是我的代码:
InputStream reader = socket.getInputStream();
byte[] ver = new byte[1];
byte[] cmd = new byte[1];
byte[] port = new byte[2];
byte[] ip = new byte[4];
reader.read(ver, 0, 1); //should be: 4
reader.read(cmd, 1, 1); //should be: 1
reader.read(port, 2, 2); //should be: 00, 80
reader.read(ip, 4, 4); //should be: 217, 70, 182, 162
这是我从我的代码中得到的响应:[4, 1, 0, 80, -39, 70, -74, -94]
不知为什么我得到的IP部分总是错误的,我真的不知道为什么。我的第二个问题是:是否有一种简单而干净的方法来获取最后一个用户 ID 字符串部分,而不必构建一个可能最终永远挂起的困惑循环,如果 \0
字节不是找到了吗?
最佳答案
全部扔掉,使用DataInputStream
的方法。它们将为您提供整数、短整数、长整数和完全读取的字节数组,并为您处理网络字节排序。
关于java - 如何在 java 中解析代理 socks 数据包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27087612/