⑴ 用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還不知道咋實現,可以考慮用本地方法。