java中的小型ip扫描器代码

发布于 2024-10-27 13:36:51 字数 718 浏览 2 评论 0原文

我正在用java编写一个小游戏。有一个服务器和一个客户端模块。目前,每个客户端都必须手动输入服务器 IP(在本地网络中)。这就是我在这里编写这段代码的原因:

import java.net.*;


public class myIP {

public static void main (String argv[]) 
{ 
    try{
          InetAddress ownIP=InetAddress.getLocalHost();
          String myIP = ownIP.getHostAddress();
          System.out.println( "IP of my system is := "+ myIP );
        }catch (Exception e){
          System.out.println( "Exception caught ="+e.getMessage() );
        }
      }
}

这段代码返回机器的 IP 地址。有了这些信息(我自己的 IP 地址),我现在想检查此范围内的其他 IP 地址,以自动查找服务器。

现在我不知道如何迭代这个IP范围。例如:如果“myIP”是 10.0.0.5,我如何修改该字符串,以便我拥有 10.0.0.6?如果它是一个整数值,那么每次加 1 都会很容易 - 但因为它是一个字符串 - 用点分隔 - 我不知道如何解决这个问题:) 有什么想法吗?

干杯

I'm writing a small game in java. There's a server and a client module. At the moment every client has to enter the server IP (in a local network) manually. Thats why I have written this code here:

import java.net.*;


public class myIP {

public static void main (String argv[]) 
{ 
    try{
          InetAddress ownIP=InetAddress.getLocalHost();
          String myIP = ownIP.getHostAddress();
          System.out.println( "IP of my system is := "+ myIP );
        }catch (Exception e){
          System.out.println( "Exception caught ="+e.getMessage() );
        }
      }
}

This piece of code returns the IP address of the machine. With this information (my own IP address) I'd like to check now for other IP addresses in this range to find the server automatically.

Now I don't know how to iterate over this IP range. For example: if "myIP" is 10.0.0.5, how can I modify that string so I would have 10.0.0.6 for example? If it would be an integer value, it would be easy to add 1 every time - but since it's a string - separated by dots - I'm not sure how to solve that :) any idea?

cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假情假意假温柔 2024-11-03 13:36:51

这往往通过广播/组播来实现。这不是我玩过的东西,所以不能为您提供任何代码,但是 this链接提供了很好的解释。

编辑:它超越了 MulticastSocket 您也许可以使用的课程。

This tends to be achieved using broadcast/multicast. This is not something I have ever played with so can not offer you any code, but this link offers a good explanation.

Edit: it transcends there is a MulticastSocket class which you may be able to put to use.

虚拟世界 2024-11-03 13:36:51

因此,您需要将 IPv4 地址与 Int 地址相互转换。

看看这是否有帮助:

http:// /teneo.wordpress.com/2008/12/23/java-ip-address-to-integer-and-back/

So, what you need is to convert an IPv4 address to Int and back.

See if this helps:

http://teneo.wordpress.com/2008/12/23/java-ip-address-to-integer-and-back/

一身软味 2024-11-03 13:36:51

您应该使用 getAddress 来返回 ip 作为字节数组,而不是 getHostAdress

You should you getAddress which returns ip as byte array instead of getHostAdress.

娜些时光,永不杰束 2024-11-03 13:36:51

以下是来自 TechnoJeeves 的代码示例,用于执行此操作。

import java.net.InetAddress;

public class ScanNet {
    public static void main(String[] args) throws Exception {
    int[] bounds = ScanNet.rangeFromCidr("192.168.1.255/24");

    for (int i = bounds[0]; i <= bounds[1]; i++) {
        String address = InetRange.intToIp(i);
        InetAddress ip = InetAddress.getByName(address);

        if (ip.isReachable(100)) { // Try for one tenth of a second
            System.out.printf("Address %s is reachable\n", ip);
        }
    }
}

public static int[] rangeFromCidr(String cidrIp) {
    int maskStub = 1 << 31;
    String[] atoms = cidrIp.split("/");
    int mask = Integer.parseInt(atoms[1]);
    System.out.println(mask);

    int[] result = new int[2];
    result[0] = InetRange.ipToInt(atoms[0]) & (maskStub >> (mask - 1)); // lower bound
    result[1] = InetRange.ipToInt(atoms[0]); // upper bound
    System.out.println(InetRange.intToIp(result[0]));
    System.out.println(InetRange.intToIp(result[1]));

    return result;
}

static class InetRange {
    public static int ipToInt(String ipAddress) {
        try {
            byte[] bytes = InetAddress.getByName(ipAddress).getAddress();
            int octet1 = (bytes[0] & 0xFF) << 24;
            int octet2 = (bytes[1] & 0xFF) << 16;
            int octet3 = (bytes[2] & 0xFF) << 8;
            int octet4 = bytes[3] & 0xFF;
            int address = octet1 | octet2 | octet3 | octet4;

            return address;
        } catch (Exception e) {
            e.printStackTrace();

            return 0;
        }
    }

    public static String intToIp(int ipAddress) {
        int octet1 = (ipAddress & 0xFF000000) >>> 24;
        int octet2 = (ipAddress & 0xFF0000) >>> 16;
        int octet3 = (ipAddress & 0xFF00) >>> 8;
        int octet4 = ipAddress & 0xFF;

        return new StringBuffer().append(octet1).append('.').append(octet2)
                                 .append('.').append(octet3).append('.')
                                 .append(octet4).toString();
    }
} }

Here is the code sample from TechnoJeeves to do this.

import java.net.InetAddress;

public class ScanNet {
    public static void main(String[] args) throws Exception {
    int[] bounds = ScanNet.rangeFromCidr("192.168.1.255/24");

    for (int i = bounds[0]; i <= bounds[1]; i++) {
        String address = InetRange.intToIp(i);
        InetAddress ip = InetAddress.getByName(address);

        if (ip.isReachable(100)) { // Try for one tenth of a second
            System.out.printf("Address %s is reachable\n", ip);
        }
    }
}

public static int[] rangeFromCidr(String cidrIp) {
    int maskStub = 1 << 31;
    String[] atoms = cidrIp.split("/");
    int mask = Integer.parseInt(atoms[1]);
    System.out.println(mask);

    int[] result = new int[2];
    result[0] = InetRange.ipToInt(atoms[0]) & (maskStub >> (mask - 1)); // lower bound
    result[1] = InetRange.ipToInt(atoms[0]); // upper bound
    System.out.println(InetRange.intToIp(result[0]));
    System.out.println(InetRange.intToIp(result[1]));

    return result;
}

static class InetRange {
    public static int ipToInt(String ipAddress) {
        try {
            byte[] bytes = InetAddress.getByName(ipAddress).getAddress();
            int octet1 = (bytes[0] & 0xFF) << 24;
            int octet2 = (bytes[1] & 0xFF) << 16;
            int octet3 = (bytes[2] & 0xFF) << 8;
            int octet4 = bytes[3] & 0xFF;
            int address = octet1 | octet2 | octet3 | octet4;

            return address;
        } catch (Exception e) {
            e.printStackTrace();

            return 0;
        }
    }

    public static String intToIp(int ipAddress) {
        int octet1 = (ipAddress & 0xFF000000) >>> 24;
        int octet2 = (ipAddress & 0xFF0000) >>> 16;
        int octet3 = (ipAddress & 0xFF00) >>> 8;
        int octet4 = ipAddress & 0xFF;

        return new StringBuffer().append(octet1).append('.').append(octet2)
                                 .append('.').append(octet3).append('.')
                                 .append(octet4).toString();
    }
} }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文