⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netdog.java

📁 用java编程实现搜索网络服务器
💻 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 + -