导航:首页 > 编程语言 > java端口扫描器

java端口扫描器

发布时间:2022-03-09 05:13:17

⑴ 用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();//关闭资源
}
}

希望能够帮助到你,望采纳!

⑵ java如何做UDP端口扫描要用什么函数

我不知道有没有这样的方法。
然后自己写个循环
在用DatagramSocket(int port);这个构造方法的时候,假如抛异常了,就说明端口被占用了。

⑶ 谁有java本地监听与远程端口扫描 源程序

publicvoidsearchPort(intbegin,intend){
//1-65535
Sockets=null;
for(inti=begin;i<end;i++){
booleanflag=false;
try{
s=newSocket("125.72.125.37",i);
flag=s.isConnected();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
//e.printStackTrace();
}
if(flag){
System.out.println("port"+i+"isopen");
try{
s.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}else{
//System.out.println("port"+i+"isclose");
}
}
//System.out.println("seachport:"+begin+"to"+end+"ok..");
}

前两天写的一个扫描远程端口扫描的程序,写的时候是用多线程的,现在贴扫描部分的核心代码出来。。也没有什么技术含量,写得也乱。参考一下就好。

⑷ 有没有用java代码操作nmap进行过端口扫描的

nmap端口状态解析

open , 应用程序在该端口接收 TCP 连接或者 UDP 报文。

closed 关闭的端口对于nmap也是可访问的, 它接收nmap探测报文并作出响应。但没有应用程序在其上监听。

filtered 由于包过滤阻止探测报文到达端口,nmap无法确定该端口是否开放。过滤可能来自专业的防火墙设备,路由规则 或者主机上的软件防火墙。

unfiltered 未被过滤状态意味着端口可访问,但是nmap无法确定它是开放还是关闭。 只有用于映射防火墙规则集的 ACK 扫描才会把端口分类到这个状态。

⑸ 如何用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扫描局域网的端口

直接上代码:

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的多断口扫描程序,扫描内容不限!

这几天用java 语言写的一个简单的多线程多IP多tcp端口扫描程序,最大线程数为300,一秒钟可以扫500多个端口。原来看了网上的一些用java写的多线程tcp扫描程序,都不是多ip的,正好老师给了个题目,就写了下。其中涉及到两个ip范围内的所有ip计算时着实花了我不少时间,但是一写出来就觉得很简单,学到了不少东西。

下面是原代码:

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;
import java.lang.String.*;
import java.lang.Integer.*;

public class ScanPort2
{
public static void main(String args[])
{

System.out.println();
System.out.println(" *********>自定义多IP多TCP端口扫描程序 v0.01<*********");
System.out.println(" *******按Ctrl+c退出扫描*******");
String shubeginip="",shuendip="";
String sp1="",sp2="",sp3="";
int beginport,endport;
int maxThread=0;

// 异常判断变量
int bport=0,eport=0;
boolean bool = false;
boolean bool1 = false;
boolean bool2 = false;
boolean bool3 = false;
boolean bool4 = false;
long xxx=0,xxx2=0;

//由用户输入扫描范围
//读取输入开始ip

System.out.println();
System.out.println("请输入起始ip:");
while(!bool)
{
try
{

BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));
shubeginip=in1.readLine();
xxx=Com.ipj(shubeginip);
if(xxx==0)
{
bool=false;
System.out.println("IP格式错误,请输入正确的起始ip:");
}
else
if(xxx<=Com.ipj("1.0.0.0"))
{
bool=false;
System.out.println("IP范围错误,请输入正确的起始ip:");
}
else
bool=true;
}
catch(IOException e){System.out.println("IP格式错误,请输入正确的起始ip:");}
catch(NumberFormatException e){System.out.println("IP格式错误,请输入正确的起始ip:");}
catch( e){System.out.println("IP格式错误,请输入正确的开始ip:");}
catch(NullPointerException e){System.out.print("退出程序!");}
}

//读取输入结束ip
System.out.println();
System.out.println("请输入结束ip:");
while(!bool1)
{
try
{

BufferedReader in2=new BufferedReader(new InputStreamReader(System.in));
shuendip=in2.readLine();
xxx2=Com.ipj(shuendip);
if(xxx2==0)
{
bool1=false;
System.out.println("IP格式错误,请输入正确的结束ip:");
}
else
if(Math.abs(xxx2-xxx)>5155130)
{
bool1=false;
System.out.println("扫描IP数不能大于5155130,请输入正确的结束ip:");
}
else
bool1=true;

}
catch(IOException e){System.out.println("IP格式错误,请输入正确的结束ip:");}
catch(NumberFormatException e){System.out.println("IP格式错误,请输入正确的结束ip:");}
catch( e){System.out.println("IP格式错误,请输入正确的结束ip:");}
catch(NullPointerException e){System.out.print("退出程序!");}
}

//读取输入起始端口
System.out.println();
System.out.println("请输入起始端口:");
while(!bool2)
{
try
{

BufferedReader in3=new BufferedReader(new InputStreamReader(System.in));
sp1=in3.readLine();
bport=Integer.parseInt(sp1);
if(bport<0|bport>65535)
{
System.out.println("端口错误,请输入正确的起始端口:");
bool2=false;
}
else
bool2=true;
}
catch(IOException e){System.out.println("端口错误,请输入正确的起始端口:");}
catch(NumberFormatException e){System.out.println("端口错误,请输入正确的起始端口:");}
}
beginport=Integer.parseInt(sp1);

//读取输入结束端口
System.out.println();
System.out.println("请输入结束端口:");
while(!bool3)
{
try
{

BufferedReader in4=new BufferedReader(new InputStreamReader(System.in));
sp2=in4.readLine();
eport=Integer.parseInt(sp2);
if(eport<beginport|eport>65535)
{
System.out.println("端口错误,请输入正确的结束端口:");
bool3=false;
}
else
bool3=true;
}
catch(IOException e){System.out.println("端口错误,请输入正确的结束端口:");}
catch(NumberFormatException e){System.out.println("端口错误,请输入正确的起始端口:");}

}
endport=Integer.parseInt(sp2);

//读取输入最大线程数
System.out.println();
System.out.println("请输入最大线程(1-300):");
while(!bool4)
{
try
{

BufferedReader in5=new BufferedReader(new InputStreamReader(System.in));
sp3=in5.readLine();
maxThread=Integer.parseInt(sp3);
if(maxThread>300|maxThread<1)
{
System.out.println("请输入1-300之间的整数:");
bool4=false;
}
else
bool4=true;
}
catch(IOException e){System.out.print("退出程序!");}
catch(NumberFormatException e){System.out.println("请输入1-300之间的整数");}
catch(NullPointerException e){System.out.print("退出程序!");}
}
maxThread=Integer.parseInt(sp3);

//开始扫描
System.out.println("正在处理IP地址……");

long long_beginip=Com.ipj(shubeginip);//转换ip
long long_endip=Com.ipj(shuendip);

int allport=endport-beginport+1;//计算扫描端口总数
int allip=(int)Math.abs(long_beginip - long_endip) + 1;
int t1=0,t2=0; //计算耗时
long begint,endt,alltime;
Date mydate=new Date();
begint=mydate.getTime(); //获取当前时间

try
{
//计算两个ip值之间的所有ip
long[] ipan=new long[(int)Math.abs(long_beginip - long_endip) + 1];
for(int k=0;k<=Math.abs(long_beginip - long_endip);k++)
{

if (long_beginip-long_endip < 0)
ipan[k]=long_beginip+k;
else
ipan[k]=long_endip+k;

}

String[] ips1=new String[ipan.length];
System.out.println("正在扫描……");
for(int a=0;a<=ipan.length;a++)
{

ips1[a]=Com.ipk(ipan[a]);
if(ips1[a]!=null)
{

//System.out.println("正在扫描:"+ips1[a]);
for(int xPort=beginport;xPort<=endport;xPort++ )
{

int x=quanjubianliang.Threadnum;
if(x>maxThread)
{

try
{
Thread.sleep(100);
}
catch(InterruptedException e){}

}
String threadName="thread"+xPort;
if(xPort!=21)
new ScanThread(ips1[a],xPort,threadName).start();

}
}
else
allip=allip-1;

}

}
catch(Exception exp){System.out.println();}
Date mydate2=new Date(); //获取结束时间
try
{
Thread.sleep(1000);
}
catch(InterruptedException e){}
System.out.println("扫描完成!");

endt=mydate2.getTime();
alltime=endt-begint;
System.out.print("扫描了"+allip+"个IP共"+allip*allport+"个端口,用时"+alltime+"毫秒");

}
}

//全局变量,用做限制最大线程
class quanjubianliang
{

public static int Threadnum=0;

}

//扫描线程
class ScanThread extends Thread
{

private String ghost;
private int scanP;

public ScanThread(String h,int mPort,String threadName)
{

ghost=h;
scanP=mPort;

}
public void run()
{
quanjubianliang.Threadnum=quanjubianliang.Threadnum+1;
try
{

SocketAddress sockaddr = new InetSocketAddress(ghost,scanP);
Socket scans=new Socket();
int timeoutMs=50;
scans.connect(sockaddr, timeoutMs);
scans.close();
System.out.println("主机:"+ghost+" TCP端口:"+scanP+" 在线");

}
catch(SocketTimeoutException e){}
catch(IOException e){}
quanjubianliang.Threadnum=quanjubianliang.Threadnum-1;

}
}

//ip处理

class Com
{
//将字符串ip转换成长整型
public static long ipj(String ipAddress)
{

long[] ip=new long[4];
int position1=ipAddress.indexOf(".");
int position2=ipAddress.indexOf(".",position1+1);
int position3=ipAddress.indexOf(".",position2+1);

ip[0]=Long.parseLong(ipAddress.substring(0,position1));
ip[1]=Long.parseLong(ipAddress.substring(position1+1,position2));
ip[2]=Long.parseLong(ipAddress.substring(position2+1,position3));
ip[3]=Long.parseLong(ipAddress.substring(position3+1));
if(ip[0]==0|ip[3]==0|ip[0]>255|ip[1]>255|ip[2]>255|ip[3]>255)
return 0;
else
return (ip[0]<<24)+(ip[1]<<16)+(ip[2]<<8)+ip[3];
}

//将长整型ip转换为字符串型
public static String ipk(long longIP)
{

StringBuffer sb=new StringBuffer("");
//直接右移24位
sb.append(String.valueOf(longIP>>>24));
sb.append(".");
//将高8位置0,然后右移16位
sb.append(String.valueOf((longIP&0x00FFFFFF)>>>16));
sb.append(".");
sb.append(String.valueOf((longIP&0x0000FFFF)>>>8));
sb.append(".");
sb.append(String.valueOf(longIP&0x000000FF));
String sc=sb.toString();
String[] ss = sc.split("\\.");
if(Integer.parseInt(ss[3])==0)
return null;
else
return sb.toString();

}
}

⑻ java端口扫描时一点奇怪的问题

绝对不是这里的原因,我在我电脑上试过了,不管有没有那一句110端口都是关闭的!

⑼ 请教大神,怎么使用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实现tcp connect,tcp syn端口扫描

connect比较简单,就是用Socket+多线程,每个端口创建一次连接,没连上是不会往下执行的,会抛出异常,网上有源码,都是这个方法。
syn和FIN还不知道咋实现,可以考虑用本地方法。

阅读全文

与java端口扫描器相关的资料

热点内容
优信二手车解压后过户 浏览:62
Windows常用c编译器 浏览:778
关于改善国家网络安全的行政命令 浏览:833
安卓如何下载网易荒野pc服 浏览:654
javainetaddress 浏览:104
苹果4s固件下载完了怎么解压 浏览:1003
命令zpa 浏览:286
python编译器小程序 浏览:945
在app上看视频怎么光线调暗 浏览:540
可以中文解压的解压软件 浏览:593
安卓卸载组件应用怎么安装 浏览:913
使用面向对象编程的方式 浏览:340
程序员项目经理的年终总结范文 浏览:930
内衣的加密设计用来干嘛的 浏览:433
淮安数据加密 浏览:292
魔高一丈指标源码 浏览:982
松下php研究所 浏览:168
c回调java 浏览:401
梦幻端游长安地图互通源码 浏览:746
电脑本地文件如何上传服务器 浏览:314