cmainframe.java

来自「windows系统下用java开发的网络入侵检测程序」· Java 代码 · 共 381 行

JAVA
381 行
字号
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
/**
 * The main window of JIDX Client.<p>
 * 2005.9.1
 * @version 0.1.2
 * @author Daxin Tian
 *
 */
class CmainFrame extends Frame implements ActionListener,Runnable
{
	/**
	 * The peer use it to listen to other peers connection on 58596.
	 */
	ServerSocket cs_socket;
	
	/**
	 * This thread is used by CmainFrame to receive JIDX Server's message.
	 */
	Thread cmainframe_thread;
	/**
	 * This thread is used by cs_socket to accept other peers connection.
	 */
	CmainFrame_ServerThread cmfs_thread;
	/**
	 * Menubar contains "Control" and "Information" menu.
	 */
	MenuBar menubar;
	/**
	 * Menu Control. 
	 */
	Menu menuEnter1;
	/**
	 * Menu Information.
	 */
	Menu menuEnter2;
	/**
	 * MenuItem "Find 2 Peer". Open CaddFriendFrame.
	 */
	MenuItem item_addfriend;
	/**
	 * MenuItem "Signature". Open Csignature.
	 */
	MenuItem item_signature;
	/**
	 * MenuItem "Modify Password". Open Cpassword.
	 */
	MenuItem item_password;
	/**
	 * MenuItem "Connecting Peers". Open CserverFrame.
	 */
	MenuItem item_connectingfriends;
	/**
	 * MenuItem "Parse IDMEF File". Open CparseFrame.
	 */
	MenuItem item_parseIDMEF;
	/**
	 * MenuItem "Build IDMEF File". Open CbuildIDMEF.
	 */
	MenuItem item_buildIDMEF;
	/**
	 * DataInputStream of this client.  Communication between JIDX Server and JIDX Client. 
	 */
	DataInputStream c_dis=null;
	/**
	 * DataOutputStream of this client.  Communication between JIDX Server and JIDX Client. 
	 */
	DataOutputStream c_dos=null;
	/**
	 * Receive JIDX Server's back XML messages.
	 */
	String c_in=null;
	/**
	 * Parse JIDX Server's back XML messages.
	 */
	XMLParser xp=new XMLParser();
	/**
	 * This client's self username.
	 */
	String c_name=null;
	/**
	 * Open when MenuItem "Find 2 Peer" is clicked.
	 */
	CaddFriendFrame cff;
	/**
	 * Open when MenuItem "Signature" is clicked.
	 */
	Csignature csig;
	/**
	 * Open when MenuItem "Modify Password" is clicked.
	 */
	Cpassword cps;
	/**
	 * Open when MenuItem "Connecting Peers" is clicked.
	 */
	CserverFrame csf;
	/**
	 * Open when MenuItem "Parse IDMEF File" is clicked.
	 */
	CparseFrame c_parse;
	/**
	 * Open when MenuItem "Build IDMEF File" is clicked.
	 */
	CbuildIDMEF c_bf;
	/**
	 * 
	 * @param s Window title.
	 * @param dis DataInputStream of this client to JIDX Server.
	 * @param dos DataoutputStream of this client to JIDX Server.
	 * @param name This client's self username.
	 */
	
	CmainFrame(String s,  DataInputStream dis, DataOutputStream dos,String name)
	{
		super(s);
		c_name=name;
		c_dis=dis;
		c_dos=dos;
		
		setBounds(200,100,400,300);
		
		menubar=new MenuBar();
		menuEnter1=new Menu("Control");
		menuEnter2=new Menu("Information");
		item_addfriend=new MenuItem("Find 2 Peer");
		item_signature=new MenuItem("Signature");
		item_password=new MenuItem("Modify Password");
		item_connectingfriends=new MenuItem("Connecting Peers");
		item_parseIDMEF=new MenuItem("Parse IDMEF File");
		item_buildIDMEF=new MenuItem("Build IDMEF File");
				
		
		menuEnter1.add(item_addfriend);
		menuEnter1.add(item_parseIDMEF);
		menuEnter1.add(item_buildIDMEF);
		menuEnter2.add(item_signature);
		menuEnter2.add(item_password);
		menuEnter2.add(item_connectingfriends);
		menubar.add(menuEnter2);
		menubar.add(menuEnter1);
		setMenuBar(menubar);
		
		setVisible(false);
	
		item_addfriend.addActionListener(this);
		item_signature.addActionListener(this);
		item_password.addActionListener(this);
		item_connectingfriends.addActionListener(this);
		item_parseIDMEF.addActionListener(this);
		item_buildIDMEF.addActionListener(this);
		
		cff=new CaddFriendFrame(c_dis,c_dos,c_name);
		csig=new Csignature(c_dis,c_dos,c_name);
		cps=new Cpassword(c_dis,c_dos,c_name);
		csf=new CserverFrame();
		c_parse=new CparseFrame();
		c_bf=new CbuildIDMEF();
	
		
		cmainframe_thread=new Thread(this);
		cmainframe_thread.setPriority(Thread.MAX_PRIORITY);
		cmainframe_thread.start();
		
		try
		{
			cs_socket=new ServerSocket(58596);
			
			System.out.println("CServer running");
			cmfs_thread=new CmainFrame_ServerThread(cs_socket,csf);
			cmfs_thread.start();
			
			
		}
		catch(IOException e)
		{
			System.out.println("Cerver wrong"+e);
		}
		
		addWindowListener(new WindowAdapter()
				  {public void windowClosing(WindowEvent e)
				  	{setVisible(false);System.exit(0);}
				  }
				);
	}
	/**
	 * Listen MenuItem's click action.
	 */
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==item_addfriend)
		{
			
			cff.c_tf.setText(null);
			cff.c_lusernum.setText("");
			
			cff.l_friend.removeAll();
			cff.l_info.removeAll();
			cff.l_state.removeAll();
			cff.l_ips.removeAll();
			cff.l2.setText("");
			cff.l2.setBackground(Color.white);
			cff.setVisible(true);
						
		    
		}
		if(e.getSource()==item_signature)
		{
			csig.c_send();
			csig.setVisible(true);
		}
		if(e.getSource()==item_password)
		{
			cps.setVisible(true);
		}
		if(e.getSource()==item_connectingfriends)
		{
			csf.setVisible(true);
		}
		if(e.getSource()==item_parseIDMEF)
		{
			c_parse.text.setText("");
			c_parse.text1.setText("");
			c_parse.setVisible(true);			
		}
		if(e.getSource()==item_buildIDMEF)
		{
			c_bf.setVisible(true);		
		}
	}
	/**
	 * The thread continued to read the JIDX Server's back messages and transact them.
	 */
	public void run()
	{
		while(true)
		{
			
			try
			{
				c_in=c_dis.readUTF();
				//System.out.println(c_in);
				c_parser();
				
			}
			catch(IOException e)
			{
				System.out.println("main frame readUTF error: "+e);
				break;
			}
			
			
		}
	}
	/**
	 * Use XMLParser parse XML message.
	 *
	 */
	public void c_parser()
	{
		xp.in=new StringBufferInputStream(c_in);
		xp.parser();
		if(xp.ackusernum_b)
		{
			xp.ackusernum_b=false;
			cff.c_lusernum.setText(xp.acknum_i+" users are found");
			cff.l2.setText("You can double click one online user and send IDMEF file to him/her");
			cff.l2.setBackground(Color.GREEN);
		}
		if(xp.ackusers_b)
		{
			xp.ackusers_b=false;
			if(xp.friendname_s.equals(c_name))
			{
				xp.friendname_s=xp.friendname_s+"(self)";
				
			}
			if(xp.friendstate_i==1)
			{
				cff.l_friend.add(xp.friendname_s+" :--Online");
			}
			else
			{
				cff.l_friend.add(xp.friendname_s+" :--Offline");
			}
			
			cff.l_info.add(xp.friendinfo_s);
			cff.l_state.add(Integer.toString(xp.friendstate_i));
			cff.l_ips.add(xp.friendips_s);
		}
		if(xp.acksignature_b)
		{
			xp.acksignature_b=false;
			csig.ta_sig.setText(xp.acksig_s);
		}
		if(xp.ok_b)
		{
			xp.ok_b=false;
			if(xp.ok_i==3)
			{
				cps.la1.setText("Modify successful!");
			}
			
		}
		if(xp.error_b)
		{
			xp.error_b=false;
			if(xp.err_i==4)
			{
				cps.la1.setText("Modify failed!");
			}
			
		}
	}
}
/**
 * 
 * This thread class is used by JIDX client act as a server to accept other peers connection.<p>
 * 2005.9.1
 * @version 0.1.2
 * @author Daxin Tian
 *
 */
class CmainFrame_ServerThread extends Thread
{
	/**
	 * Point to CmainFrame's cs_socket.
	 */
	ServerSocket ssoc;
	/**
	 * ServerSocket's accept() returns Socket.
	 */
	Socket cs_csocket;
	/*
	 * Point to CmainFrame's csf.
	 */
	CserverFrame cserverframe;
	/**
	 * 
	 * @param ss CmainFrame's cs_socket.
	 * @param csf CmainFrame's csf.
	 */
	CmainFrame_ServerThread(ServerSocket ss, CserverFrame csf)
	{
		ssoc=ss;
		cserverframe=csf;
		
	}
	/**
	 * Continue to accept JIDX Client peer's connection request.
	 */
	public void run()
	{
		
		while(true)
		{
			try
			{
				
				cs_csocket=ssoc.accept();
				if(cs_csocket!=null)
				{
					
					new CserverThread(cs_csocket,cserverframe).start();
					
					//System.out.println("cs socket is running");
				}
				
			}
			catch(IOException e)
			{
				System.out.println("main frame serversocket accept error: "+e);
				break;
			}
		}
		
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?