在Java中,如何实现UDP端口扫描? [英] in Java, how to achieve UDP port scanning?

查看:66
本文介绍了在Java中,如何实现UDP端口扫描?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新来的,我正在开发一个端口扫描程序,TCP 运行良好,但我不知道如何实现 UDP 端口扫描.假设我想知道这个 LAN 中另一台主机上的 UDP 端口 XXXX 是否打开.这段代码会起作用吗?如果不是,那是什么问题?

new here, I'm working on a program for port scanning, TCP works well, but I don know how to ahieve UDP ports scanning. Say I wanna know whether UDP port XXXX on another host in this LAN is open. will this code do the work? if not, what's the problem?

protected String scanUDP(InetAddress IP, int port)
{
    try{
        byte [] bytes = new byte[128];
        DatagramSocket ds = new DatagramSocket();
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, IP, port);
        ds.setSoTimeout(1000);
        ds.send(dp);
        dp = new DatagramPacket(bytes, bytes.length);
        ds.receive(dp);
        ds.close();
    }
    catch(InterruptedIOException e){
        return "CLOSED";
    }
    catch(IOException e){
        return "CLOSED";
    }
    return "OPEN";
}

只是一个新手,还在学习中.谢谢!

just a newbie, still learning. thanks!

推荐答案

UDP 是无连接的,所以你不能期待一个响应包,这是必然的.如果端口关闭,您可能会收到 ICMP 错误消息,但无法保证这一点(例如,防火墙可能会默默地丢弃数据包).

UDP is connectionless, so you can't expect a response packet, necessarily. If the port is closed, you might get an ICMP error message, though there's no guarantee of this (e.g. a firewall could silently drop the packet).

这篇关于在Java中,如何实现UDP端口扫描?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆