将字符串转换为字节时,addr的长度非法

时间:2012-12-02 23:53:57

标签: java ping

我正在为一个朋友写一个游戏(我的世界)的mod。为了确保服务器在一定时间内可以访问,我需要执行mod的指定功能,我使用InetAddress执行此操作,但每次使用该命令时都会出现此错误

2012-12-02 18:45:53 [INFO] [STDERR] java.net.UnknownHostException: addr is of illegal length
2012-12-02 18:45:53 [INFO] [STDERR]     at java.net.InetAddress.getByAddress(Unknown Source)
2012-12-02 18:45:53 [INFO] [STDERR]     at java.net.InetAddress.getByAddress(Unknown Source)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.src.CommandServSwitch.processCommand(CommandServSwitch.java:40)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.src.CommandHandler.executeCommand(CommandHandler.java:81)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.src.NetServerHandler.handleSlashCommand(NetServerHandler.java:715)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.src.NetServerHandler.handleChat(NetServerHandler.java:681)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.src.Packet3Chat.processPacket(Packet3Chat.java:60)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.src.MemoryConnection.processReadPackets(MemoryConnection.java:79)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.src.NetServerHandler.networkTick(NetServerHandler.java:82)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.src.NetworkListenThread.networkTick(NetworkListenThread.java:55)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.src.IntegratedServerListenThread.networkTick(IntegratedServerListenThread.java:111)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:696)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:592)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.src.IntegratedServer.tick(IntegratedServer.java:110)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:499)
2012-12-02 18:45:53 [INFO] [STDERR]     at net.minecraft.src.ThreadMinecraftServer.run(ThreadMinecraftServer.java:17)

生成此错误的方法(以及您需要知道的所有代码):

    @Override
public void processCommand(ICommandSender var1, String[] var2) {
    if(var2.length < 1 || var2.length > 3) {
            throw new WrongUsageException("command.switch.usage", new Object[0]);
    }
    else {
             servport = Integer.parseInt(var2[1]);

            boolean ping = false;
            try {
                InetAddress IP = InetAddress.getByAddress(var2[0].getBytes());
                ping = IP.isReachable(400);
            } catch(UnknownHostException e) {
                e.printStackTrace();
            } catch(IOException e) {
                e.printStackTrace();
            }

1 个答案:

答案 0 :(得分:1)

getByAddress的文档说:“给定原始IP地址返回InetAddress对象。参数,网络字节顺序:最高位字节地址在getAddress()[0]。“

            InetAddress IP = InetAddress.getByAddress(var2[0].getBytes());

这行代码没有任何意义。该字符串不是函数所需的网络字节顺序的原始IP地址。否则,它恰好是四个字节长。

您需要致电InetAddress.getByName以将文本字符串(例如foo.domain.com1.2.3.4)转换为内部二进制格式。