如何用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();//关闭资源
}
}
求IP端口扫描器的VB源码
'4个TEXTBOX控件,2个COMMANDBUTTON控件,一个WINSOCK控件
Dim portnum As Long
Dim start As String
Sub scanningports()
Dim porttwo As Long
portnum = Text1.Text
porttwo = Text2.Text
Command2.Enabled = True
On Error GoTo viriio
Do
portnum = portnum + 1
DoEvents
If start = True Then
'关闭当前WINSOCK
Winsock1.Close
'防止系统冻结
DoEvents
Winsock1.LocalPort = portnum
DoEvents
Text3.Text = portnum
Winsock1.Listen
DoEvents
Else
portnum = 0
Command1.Enabled = True
Text1.Locked = False
Text2.Locked = False
Exit Sub
End If
Winsock1.Close
DoEvents
Loop Until portnum = porttwo
portnum = 0
Command1.Enabled = True
logport.Text = logport.Text vbCrLf "Scanning Ports Done!" vbCrLf
Text1.Locked = False
Text2.Locked = False
viriio:
If Err.Number = 10048 Then
logport.Text = logport.Text vbCrLf "端口" Winsock1.LocalPort " 开启中"
Resume Next
End If
End Sub
Private Sub Command1_Click()
Command2.Enabled = True
If Text1.Text = "" Then
MsgBox "你必须指定开始端口号!"
Exit Sub
End If
If Text2.Text = "" Then
MsgBox "你必须指定一个结束端口号"
Exit Sub
End If
Text1.Locked = True
Text2.Locked = True
Command1.Enabled = False
Winsock1.Close
start = True
Call scanningports
logport.Text = logport.Text vbCrLf "端口" Text1.Text "- " Text3.Text " 已经成功扫描!"
End Sub
Private Sub Command2_Click()
Command2.Enabled = False
start = False
End Sub
设计一个判断端口是否开启的扫描器程序的源代码怎么写?
for(int i=0;i6;i++)
{
for(int j=0;j10;j++)
{
theport[j].rmt_host=rmt_host;
theport[j].p=port[i*10+j];
theport[j].n=j;
Thread[j]=AfxBeginThread(pScan,(LPVOID)theport[j]);
//启动端口扫描线程
hThread[j]=Thread[j]-m_hThread;
Sleep(1);
}
WaitForMultipleObjects(10,hThread,TRUE,120000);
}
//扫描模块代码
SOCKET sockfd;
SOCKADDR_IN addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd 0)
{
exit(0);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(rmt_host);
int r = connect(sockfd,(struct sockaddr *) addr, sizeof(addr));
//尝试连接端口进行检测
closesocket(sockfd);
//连接返回值处理
if (r!=-1)
{
::PostMessage(hWnd,WM_DISPLAY,port,0);
}
//显示端口扫描结果
LONG CScanDlg::OnDisplay(LONG lParam, UINT wParam)
{
LPSERVENT bar;
CString open;
int p=lParam;
bar = getservbyport(htons(p),"tcp");
open.Format("\t%d号端口(%s)开放!\r\n",p,(bar == NULL) ? "未知" :
bar-s_name);
m_HistoryEdit.AppendString (open);
return 0;
}
0条大神的评论