?? netdog.java
字號:
import java.io.*;
import java.awt.*;
import java.net.*;
//NetDog類是本程序的主類
public class NetDog extends Frame implements Runnable
{
Label startIPLabel;
Label stopIPLabel;
TextField startIP[];
TextField stopIP[];//IP地址輸入文本框
Button startButton;
Button stopButton;//開始、結束按鈕
Label portLabel;
TextField portTextField;//端口地址輸入文本框
Label LabelContent;
TextArea content;//查詢結果顯示文本區
int insertPos;//記錄content的當前插入位置
int startIPNum[];
int stopIPNum[];//開始、結束IP地址數組
String IPName;//當前搜索的IP地址
int portNum;//端口號
MenuBar NetDogMenuBar;//菜單條
FileDialog saveDialog;//保存文件對話框
Thread thread=null;//搜索IP地址相應端口的線程
//NetDog()構造方法,初始化各種變量
public NetDog()
{
System.out.println("I'm running......");
startIP=new TextField[4];
stopIP=new TextField[4];
setTitle("NetDog--Copyright Jiang Dehua.Tsinghua University");
setLayout(new BorderLayout());
for (int i=0;i<startIP.length;i++)
{
startIP[i]=new TextField("",3);
stopIP[i]=new TextField("",3);
}
startIPLabel=new Label("起始IP:");
stopIPLabel=new Label("結束IP:");
portLabel=new Label("端口號:");
portTextField=new TextField(6);
startButton=new Button("開始");
stopButton=new Button("停止");
LabelContent=new Label("搜索結果:");
content=new TextArea("",14,80);
content.setEditable(false);
insertPos=0;
IPName=new String();
startIPNum=new int[4];
stopIPNum=new int[4];
NetDogMenuBar=new MenuBar();
Menu menuFile=new Menu("File");
menuFile.add(new MenuItem("&Save"));
menuFile.addSeparator();
menuFile.add(new MenuItem("E&xit"));
NetDogMenuBar.add(menuFile);
Menu menuHelp=new Menu("Help");
menuHelp.add(new MenuItem("&About"));
NetDogMenuBar.add(menuHelp);
setMenuBar(NetDogMenuBar);//創建菜單條
Panel p1=new Panel();
Panel p2=new Panel();
Panel p=new Panel();
p1.setLayout(new FlowLayout());
p2.setLayout(new FlowLayout());
p.setLayout(new BorderLayout());
p1.add(startIPLabel);
p2.add(stopIPLabel);
for (int i=0;i<startIP.length;i++)
{
p1.add(startIP[i]);
p2.add(stopIP[i]);
}
p1.add(portLabel);
p1.add(portTextField);
p2.add(startButton);
p2.add(stopButton);
p.add("North",p1);
p.add("South",p2);
add("North",p);
add("Center",LabelContent);
add("South",content);//窗口布局
saveDialog=new FileDialog(this,"保存文件",FileDialog.SAVE);
resize(500,400);
show();
System.out.println("I'm ready now!");
}//構造方法結束
//write()方法用于保存文件
public void write(String s)
{
FileOutputStream out=null;
try{
out=new FileOutputStream(s);
}catch(Exception e){
System.out.println("Error in opening file:"+s);
return;
}
PrintStream psOut=new PrintStream(out);
psOut.print(content.getText());
try{
out.close();
}catch(IOException e){
System.out.println(e);
}
}//write()方法結束
//重載action()方法處理窗口事件
public boolean action(Event evt,Object obj)
{
if(obj.toString().equals("&Save"))//選擇保存菜單項
{
String filename;
saveDialog.show();
if((filename=saveDialog.getFile())==null)
{
System.out.println("You didn't assign file name.");
return true;
}//如果取消保存,退出
write(filename);
System.out.println("I have saved them in "+filename+"!");
return true;
}
if(obj.toString().equals("&About"))//選擇關于
{
System.out.println("About me......");
AboutDialog about=new AboutDialog(this);
return true;
}
if(obj.toString().equals("E&xit"))//選擇退出
{
System.out.println("Good-bye,my host!");
dispose();
System.exit(0);
}
if(evt.target==stopButton)//按下停止按鈕
{
System.out.println("I must stop!");
thread.stop();
return true;
}
if(evt.target==startButton)//按下開始按鈕
{
thread=new Thread(this);
System.out.println("Yes,Sir!I start now!");
thread.start();
return true;
}
return false;
}//action()方法結束
//run()方法實現了循環測試服務器端口的線程,
public void run()
{
portNum=Integer.parseInt(portTextField.getText());
if(portNum<=0||portNum>65535)
{
System.out.println("Port number error!");
thread.stop();
}//如果端口號輸入有錯,終止線程
for(int j=0;j<startIPNum.length;j++)//向開始、結束IP數組賦值
{
startIPNum[j]=Integer.parseInt(startIP[j].getText());
//判斷,如果開始IP地址小于0或大于255均提示出錯并終止線程
if(startIPNum[j]<0||startIPNum[j]>255)
{
System.out.println("startIPNum["+j+"] error!");
thread.stop();
}
stopIPNum[j]=Integer.parseInt(stopIP[j].getText());
//判斷,如果結束IP地址小于0或大于255均提示出錯并終止線程
if(stopIPNum[j]<0||stopIPNum[j]>255)
{
System.out.println("stopIPNum["+j+"] errror!");
thread.stop();
}
}
System.out.println("I'm finding......");
//循環測試IP地址的指定端口號
for (int i0=startIPNum[0];i0<=stopIPNum[0];i0++)
for (int i1=startIPNum[1];i1<=stopIPNum[1];i1++)
for (int i2=startIPNum[2];i2<=stopIPNum[2];i2++)
for (int i3=startIPNum[3];i3<=stopIPNum[3];i3++)
{
IPName=i0+"."+i1+"."+i2+"."+i3;
System.out.println("Hello "+IPName);
testSocket();//調用testSocket方法
}
}//run()方法結束
//testSocket()方法測試遠程服務器端口
public boolean testSocket()
{
Socket socket;
try{
try{
socket=new Socket(IPName,portNum);//創建新套接字
}catch(UnknownHostException e){
System.out.println("I don't know "+IPName);
return false;
}
}catch(IOException e2){
System.out.println("I can't open socket of "+IPName);
return false;
}
insertString(IPName+" is OK \n");
try{
socket.close();
}catch(IOException e2){
System.out.println("IOException closing socket");
return true;
}
return true;
}//testSocket()方法結束
//insertString()方法用于在文本區content中顯示信息
void insertString(String s)
{
content.insertText(s,insertPos);
insertPos+=s.length();
}
//main()方法,NetDog類的起點
public static void main(String args[])
{
new NetDog();
}
}//主類NetDog結束
/*
AboutDialog類用在幫助菜單中,該類繼承了對話框Dialog類,
只有一個構造方法和一個重載的事件處理方法handleEvent()
顯示一個對話框,內有作者信息。
*/
class AboutDialog extends Dialog
{
public AboutDialog(Frame parent)
{
super(parent,"About This Program",true);
Panel p=new Panel();
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
p1.add(new Label("清華大學"));
p2.add(new Label("江德華"));
add("North",p1);
add("Center",p2);
p3.add(new Button("OK"));
add("South",p3);
setFont(new Font("Dialog",Font.BOLD,24));
int weight=200;
int height=150;
resize(weight,height);
show();
}
public boolean handleEvent(Event evt)
{
switch(evt.id)
{
case Event.ACTION_EVENT:
{
if("OK".equals(evt.arg))
{
dispose();
return true;
}
}
}
return false;
}
}//AboutDialog類結束
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -