java扫描端口
❶ java扫描局域网的端口
直接上代码:
importjava.net.Socket;
importjava.text.SimpleDateFormat;
importjava.util.Date;
{
privateint[]p;
Socketss=null;
publicPortScanner(int[]p){
this.p=p;
}
publicstaticvoidmain(String[]args){
for(inti=0;i<65535;i=i+100){
newPortScanner(newint[]{i+1,i+100}).start();
}
}
@Override
publicvoidrun(){
for(inti=p[0];i<p[1];i++){
try{
ss=newSocket("8.8.8.8",i);
System.out.println("扫描到端口:"+i);
}catch(Exceptione){
//System.out.println("关闭端口:"+i);
}
}
}
}
❷ java如何做UDP端口扫描要用什么函数
我不知道有没有这样的方法。
然后自己写个循环
在用DatagramSocket(int port);这个构造方法的时候,假如抛异常了,就说明端口被占用了。
❸ 请教大神,怎么使用java实现UDP端口扫描
给你个UDP服务端与客户端的示例:
服务端代码:
importjava.net.DatagramPacket;
importjava.net.InetAddress;
importjava.net.MulticastSocket;
publicclassUDPMulticastServer{
finalstaticintRECEIVE_LENGTH=1024;
staticStringmulticastHost="224.0.0.1";
staticintlocalPort=9998;
publicstaticvoidmain(String[]args)throwsException{
InetAddressreceiveAddress=InetAddress.getByName(multicastHost);
if(!receiveAddress.isMulticastAddress()){//测试是否为多播地址
thrownewException("请使用多播地址");
}
intport=localPort;
=newMulticastSocket(port);
receiveMulticast.joinGroup(receiveAddress);
booleanisStop=false;
while(!isStop){
DatagramPacketdp=newDatagramPacket(newbyte[RECEIVE_LENGTH],RECEIVE_LENGTH);
receiveMulticast.receive(dp);
Stringdata=newString(dp.getData()).trim();
System.out.println(data);
if("exit".equals(data)){
System.out.println("程序退出");
isStop=true;
}
}
receiveMulticast.close();
}
}
客户端代码:
importjava.net.DatagramPacket;
importjava.net.InetAddress;
importjava.net.MulticastSocket;
publicclassUDPMulticastClient{
staticStringdestAddressStr="224.0.0.1";
staticintdestPortInt=9998;
staticintTTLTime=4;
publicstaticvoidmain(String[]args)throwsException{ InetAddressdestAddress=InetAddress.getByName(destAddressStr);
if(!destAddress.isMulticastAddress()){//检测该地址是否是多播地址
thrownewException("地址不是多播地址");
}
intdestPort=destPortInt;
MulticastSocketmultiSocket=newMulticastSocket();
// intTTL=TTLTime;
// multiSocket.setTimeToLive(TTL);
byte[]sendMSG="exit".getBytes();
DatagramPacketdp=newDatagramPacket(sendMSG,sendMSG.length,destAddress,destPort);
multiSocket.send(dp);
multiSocket.close();
}
}
❹ 如何用java语言实现端口扫描器
使用 DatagramSocket(int port) 建立socket(套间字)服务。
将数据打包到DatagramPacket中去
通过socket服务发送 (send()方法)
关闭资源
public static void main(String[] args) {
DatagramSocket ds = null; //建立套间字udpsocket服务
try {
ds = new DatagramSocket(8999); //实例化套间字,指定自己的port
} catch (SocketException e) {
System.out.println("Cannot open port!");
System.exit(1);
}
byte[] buf= "Hello, I am sender!".getBytes(); //数据
InetAddress destination = null ;
try {
destination = InetAddress.getByName("192.168.1.5"); //需要发送的地址
} catch (UnknownHostException e) {
System.out.println("Cannot open findhost!");
System.exit(1);
}
DatagramPacket dp =
new DatagramPacket(buf, buf.length, destination , 10000);
//打包到DatagramPacket类型中(DatagramSocket的send()方法接受此类,注意10000是接受地址的端口,不同于自己的端口!)
try {
ds.send(dp); //发送数据
} catch (IOException e) {
}
ds.close();
}
}
接收步骤:
使用 DatagramSocket(int port) 建立socket(套间字)服务。(我们注意到此服务即可以接收,又可以发送),port指定监视接受端口。
定义一个数据包(DatagramPacket),储存接收到的数据,使用其中的方法提取传送的内容
通过DatagramSocket 的receive方法将接受到的数据存入上面定义的包中
使用DatagramPacket的方法,提取数据。
关闭资源。
import java.net.*;
public class Rec {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(10000); //定义服务,监视端口上面的发送端口,注意不是send本身端口
byte[] buf = new byte[1024];//接受内容的大小,注意不要溢出
DatagramPacket dp = new DatagramPacket(buf,0,buf.length);//定义一个接收的包
ds.receive(dp);//将接受内容封装到包中
String data = new String(dp.getData(), 0, dp.getLength());//利用getData()方法取出内容
System.out.println(data);//打印内容
ds.close();//关闭资源
}
}
❺ 如何 用java写一个检测端口的程序
进行一次http请求,看response返回是不是200,200就正确了。
❻ 修改一个JAVA源程序,使它能扫描指定IP的主机的UDP端口
单线程的
import java.net.*;
public class UDP
{
public static void main(String args[])
{
for (int port=0;port<=65535;port++) {
try {
DatagramSocket server=new DatagramSocket(port);
server.close();
}
catch(SocketException e) {
System.out.println("UDP端口"+port+"开放!");
}
}
}
}
希望能够帮助到你,望采纳!
❼ 如何用java实现tcp connect,tcp syn端口扫描
connect比较简单,就是用Socket+多线程,每个端口创建一次连接,没连上是不会往下执行的,会抛出异常,网上有源码,都是这个方法。
syn和FIN还不知道咋实现,可以考虑用本地方法。
❽ 有没有用java代码操作nmap进行过端口扫描的
nmap端口状态解析
open , 应用程序在该端口接收 TCP 连接或者 UDP 报文。
closed 关闭的端口对于nmap也是可访问的, 它接收nmap探测报文并作出响应。但没有应用程序在其上监听。
filtered 由于包过滤阻止探测报文到达端口,nmap无法确定该端口是否开放。过滤可能来自专业的防火墙设备,路由规则 或者主机上的软件防火墙。
unfiltered 未被过滤状态意味着端口可访问,但是nmap无法确定它是开放还是关闭。 只有用于映射防火墙规则集的 ACK 扫描才会把端口分类到这个状态。
❾ java多线程TCP端口扫描程序有什么用
你能说的详细点吗?
从字面上理解,应该是扫描计算机的端口,检查是否可用之类的
❿ 用JAVA如何实现UDP端口扫描器
使用 DatagramSocket(int port) 建立socket(套间字)服务。
将数据打包到DatagramPacket中去
通过socket服务发送 (send()方法)
关闭资源
public static void main(String[] args) {
DatagramSocket ds = null; //建立套间字udpsocket服务
try {
ds = new DatagramSocket(8999); //实例化套间字,指定自己的port
} catch (SocketException e) {
System.out.println("Cannot open port!");
System.exit(1);
}
byte[] buf= "Hello, I am sender!".getBytes(); //数据
InetAddress destination = null ;
try {
destination = InetAddress.getByName("192.168.1.5"); //需要发送的地址
} catch (UnknownHostException e) {
System.out.println("Cannot open findhost!");
System.exit(1);
}
DatagramPacket dp =
new DatagramPacket(buf, buf.length, destination , 10000);
//打包到DatagramPacket类型中(DatagramSocket的send()方法接受此类,注意10000是接受地址的端口,不同于自己的端口!)
try {
ds.send(dp); //发送数据
} catch (IOException e) {
}
ds.close();
}
}
接收步骤:
使用 DatagramSocket(int port) 建立socket(套间字)服务。(我们注意到此服务即可以接收,又可以发送),port指定监视接受端口。
定义一个数据包(DatagramPacket),储存接收到的数据,使用其中的方法提取传送的内容
通过DatagramSocket 的receive方法将接受到的数据存入上面定义的包中
使用DatagramPacket的方法,提取数据。
关闭资源。
import java.net.*;
public class Rec {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(10000); //定义服务,监视端口上面的发送端口,注意不是send本身端口
byte[] buf = new byte[1024];//接受内容的大小,注意不要溢出
DatagramPacket dp = new DatagramPacket(buf,0,buf.length);//定义一个接收的包
ds.receive(dp);//将接受内容封装到包中
String data = new String(dp.getData(), 0, dp.getLength());//利用getData()方法取出内容
System.out.println(data);//打印内容
ds.close();//关闭资源
}
}
希望能够帮助到你,望采纳!