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

📄 csendidmef.java

📁 windows系统下用java开发的网络入侵检测程序
💻 JAVA
字号:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
/**
 * Send XML IDMEF file to one JIDX Client peer.<p>
 * 2005.9.20
 * @version 0.1.2
 * @author Daxin Tian
 *
 */
public class CsendIDMEF extends Frame implements ActionListener,Runnable{
	
	/**
	 * Label "Path of the file:"
	 */
	JLabel la;
	/**
	 * Label "Have sended"
	 */
	
	JLabel la1;
	/**
	 * The sending file's path and name. 
	 */
	public JTextField tf_path;
	/**
	 * The sending progress.
	 */
	//public TextField tf_size;
	public JProgressBar p_bar;
	/**
	 * Show some information of the file and the result of sending.
	 */
	public TextArea ta_msg;
	/**
	 * Button Open.  When clicked Pop-up Open file dialog and choose which file will be sent.
	 */
	public JButton b_open;
	/**
	 * Button Clear.  When clicked clear tf_path.
	 */
	public JButton b_clear;
	/**
	 * Button Send.  When clicked send message to the connected JIDX Client peer tell him/her
	 * to prepare to receive the file.
	 */
	public JButton b_send;
	/**
	 * Button Stop. When clicked stop to send message to the connected JIDX Client peer.
	 */
	public JButton b_cancel;
	/**
	 * Button Cancel. If is sending then stop sending and close the window else close the window.
	 */
	public JButton b_cancel2;
	/**
	 * Open file dialog choose which file will be sent.
	 */
	FileDialog filedialog_load;
	/**
	 * File content will be read out through this FileInputStream.
	 */
	FileInputStream file_in;
	/**
	 * The opened file.
	 */
	File f;
	/**
	 * The opened file length.
	 */
	long file_size=0;
	/**
	 * Whether the sending has been stopped.
	 */
	public boolean stop_send=false;
	/**
	 * Whether the peer rejects to receive the file.
	 */
	public boolean reject_receive=false;
	/**
	 * Point to CconnFriendFrame which is initialized by CaddFriendFrame.
	 */
	CconnFriendFrame c_cff;
	/**
	 * Socket to connect to the peer.If successful then send file through this socket.
	 */
	Csocket sf=new Csocket();
	/**
	 * Thread used by sf to send file.
	 */
	Thread sendfile_t;
	/**
	 * 
	 * @param cff When JIDX Client want to send XML IDMEF file to one peer,he/she will open
	 * CaddFriendFrame to find the peer and open cff. From cff he/she can open CsendIDMEF 
	 * to send file. 
	 */
	CsendIDMEF(CconnFriendFrame cff)
	{
		super("JIDX_Send IDMEF File");
		
		c_cff=cff;
		setBounds(200,100,200,100);
	    setVisible(false);
	    setLayout(new BorderLayout());
	    la=new JLabel("Path of the file:");
	    tf_path=new JTextField(25);
	    tf_path.setEditable(false);
	    la1=new JLabel("Have sended");
	    //tf_size=new TextField(8);
	    //tf_size.setEditable(false);
	    p_bar=new JProgressBar(0,100);
	    p_bar.setBackground(Color.white);
	    p_bar.setStringPainted(true);
	    ta_msg=new TextArea(10,30);
	    ta_msg.setEditable(false);
	    b_open=new JButton("Open");
	    b_clear=new JButton("Clear");
	    b_send=new JButton("Send");
	    b_cancel=new JButton("Stop");
	    b_cancel2=new JButton("Cancel");
	    b_cancel.setEnabled(false);
	    filedialog_load=new FileDialog(this,"Open XML IDMEF",FileDialog.LOAD);
    	filedialog_load.setVisible(false);
    	JPanel p1,p2,p3;
        p1=new JPanel();
        p1.add(la);
        p1.add(tf_path);
        p1.add(b_open);
        p1.add(b_clear);
        b_open.setDefaultCapable(true);
	    p2=new JPanel();
	    p2.add(la1);
        //p2.add(tf_size);
	    p2.add(p_bar);
        //JLabel la2=new JLabel("Bytes",JLabel.LEFT);
        //p2.add(la2);
        p2.setBackground(Color.lightGray);
	    
	    p3=new JPanel();
	    p3.add(b_send);
        p3.add(b_cancel);
        p3.add(b_cancel2);
        p3.setBackground(Color.lightGray);
        JPanel p=new JPanel();
        p.setLayout(new GridLayout(3,1));
        p.add(p2);
        p.add(p1);
        p.add(p3);
        
        JPanel pp=new JPanel();
        pp.setLayout(new BorderLayout());
        JLabel l1=new JLabel("Sending record:",JLabel.LEFT);
        pp.add(l1,"North");
        pp.add(ta_msg,"Center");
       
        add(pp,"Center");
        add(p,"South");
        
	    pack();
	    b_open.addActionListener(this);
	    b_clear.addActionListener(this);
	    b_send.addActionListener(this);
	    b_cancel.addActionListener(this);
	    b_cancel2.addActionListener(this);
	    addWindowListener(new WindowAdapter()
	  	      {public void windowClosing(WindowEvent e)
	  	      {setVisible(false);}
	  	      });
	    filedialog_load.addWindowListener(new WindowAdapter()
	        	{  public void windowClosing(WindowEvent e)
	        	    {
	        	     filedialog_load.setVisible(false);}
	        	});
	}
	  /**
	 * Listen button Open, Clear, Send, Stop, Cancel's action.
	 */
	public void actionPerformed(ActionEvent e)
	{
		String s;
        String s1;
	    if(e.getSource()==b_open)
	    {
	    	
	    	filedialog_load.setVisible(true);
	    	s=filedialog_load.getFile();
            s1=filedialog_load.getDirectory();
            tf_path.setText(s1+s);
            if(s==null || s1==null)
	 	    {
	 	    	tf_path.setText("");
	 	    }
            else
            {
            	f=new File(filedialog_load.getDirectory(),filedialog_load.getFile());
    			try
    			{
    				
    				file_in=new FileInputStream(f);
    				
    			}
    			catch(FileNotFoundException ee)
    			{
    				System.out.println("file_in "+ee);
    			}
    			catch(SecurityException eee)
    			{
    				System.out.println("file_in "+eee);
    			}
            }
	    }
	    if(e.getSource()==b_clear)
	    {
	    	tf_path.setText("");
	    }
	    if(e.getSource()==b_send)
	    {
	    	if(tf_path.getText().equals(""))
	    	{
	    		
		    	filedialog_load.setVisible(true);
		    	s=filedialog_load.getFile();
	            s1=filedialog_load.getDirectory();
	            tf_path.setText(s1+s);
	            if(s==null || s1==null)
		 	    {
		 	    	tf_path.setText("");
		 	    	new MsgDialog().show("You should choose a file to be sent");
		 	    }
	            else
	            {
	            	f=new File(filedialog_load.getDirectory(),filedialog_load.getFile());
	    			try
	    			{
	    				
	    				file_in=new FileInputStream(f);
	    			}
	    			catch(FileNotFoundException ee)
	    			{
	    				System.out.println("file_in "+ee);
	    			}
	    			catch(SecurityException eee)
	    			{
	    				System.out.println("file_in "+eee);
	    			}
	    			
	    			
	            }
	    		
	    	}
	    	else
	    	{
	    		b_send.setEnabled(false);
	    		
	    		StringBuffer c_sb=new StringBuffer();
	    		c_sb.append("<msg><send_file><peer_name>"+c_cff.myname_s+"</peer_name><file_name>"+filedialog_load.getFile()+"</file_name><size>"+f.length()+"</size></send_file></msg>");
	    		//System.out.println(c_sb.toString());
	    		try
				{
	    			
	    			c_cff.cs_friend.c_dos.writeUTF(c_sb.toString());
	    			
				}
	    		catch(IOException ee)
				{
					
				}
	    	}
	    	
	    }
	    if(e.getSource()==b_cancel)
	    {
	    	StringBuffer c_sb1=new StringBuffer();
	    	stop_send=true;
	    	b_cancel.setEnabled(false);
	    	
    		b_send.setEnabled(true);
    			
			
    		
	    }
	    if(e.getSource()==b_cancel2)
	    {
	    	if(!stop_send)
	    	{
	    		stop_send=true;
	    		
	    	}
	    	setVisible(false);
	    }
	    
	}
	/**
	 * If this client want to send file to a peer, he/she first send message, if the peer
	 * accept the request, the peer will send a port number. Then this client will try to
	 * connect the peer through this port.
	 * @param i The peer's opened port through which to receive the file.
	 */
	public void prepare_send(int i)
	{
		if(i>0)
		{
			if(sf.Cconnect(c_cff.friendips_s,i))
			{
				StringBuffer c_sb2=new StringBuffer();
	    		c_sb2.append("<msg><send_file_begin_end><num>"+1+"</num></send_file_begin_end></msg>");
	    		//System.out.println(c_sb2.toString());
	    		try
				{
	    			c_cff.cs_friend.c_dos.writeUTF(c_sb2.toString());
	    			ta_msg.append(filedialog_load.getDirectory()+filedialog_load.getFile()+"is Sending. \n");
	    			ta_msg.append("The file size is: "+f.length()+" Bytes. \n");
	    			file_size=0;
	    			//tf_size.setText("");
	    			p_bar.setValue(0);
	    			//stop_send=false;
	    			//reject_receive=false;
				}
	    		catch(IOException ee)
				{
				}
			}
		}
		else
		{
			StringBuffer c_sb4=new StringBuffer();
    		c_sb4.append("<msg><send_file_begin_end><num>"+0+"</num></send_file_begin_end></msg>");
    		//System.out.println("else"+c_sb4.toString());
    		try
			{
    			c_cff.cs_friend.c_dos.writeUTF(c_sb4.toString());
    			
			}
    		catch(IOException ee)
			{
				
			}
		}
	}
	/**
	 * If this client successfully connect to the peer then the peer will tell him/her to
	 * send file.  When this client receive the message, he/she will call send_file()to 
	 * start the send file thread--sendfile_t to transmit the file.
	 *
	 */
	public void send_file()
	{
		
		
		
	
		if(!reject_receive)
		{
			sendfile_t=new Thread(this);
			sendfile_t.setPriority(Thread.MIN_PRIORITY);
		    sendfile_t.start();
		    //System.out.println("send start");
		}
		else
		{
			ta_msg.append("The sending is rejected. \n");
			
			//b_cancel.setEnabled(false);
			b_send.setEnabled(true);
		}
		
		
	}
	/**
	 * This thread continued to transimit the file until over or stopped by peer.
	 */
	public void run()
	{
		int ri;
		byte rb[]=new byte[1000];
	    
		try
		{
			b_cancel.setEnabled(true);
			stop_send=false;
			
			while((ri=file_in.read(rb))!=-1 && !stop_send )
			{
				//System.out.println("send: "+ri);
				try
				{
	    			sf.c_dos.write(rb,0,ri);
	    			file_size=file_size+ri;
	    			//tf_size.setText(String.valueOf(file_size));
	    			p_bar.setValue((int)(100*file_size/f.length()));
	    			
	    			
				}
	    		catch(IOException ee)
				{
					break;
				}
			}
		}
		catch(IOException ee)
		{
			
		}
		
		sf.Cclose();
			    		
		int i_end=0;
		if(stop_send)
		{
			i_end=0;
		}
		else
		{
			i_end=2;
		}
		StringBuffer c_sb3=new StringBuffer();
		c_sb3.append("<msg><send_file_begin_end><num>"+i_end+"</num></send_file_begin_end></msg>");
		//System.out.println(c_sb3.toString());
		try
		{
			c_cff.cs_friend.c_dos.writeUTF(c_sb3.toString());
			if(stop_send)
			{
				ta_msg.append("Send is stopped. \n");
				ta_msg.append("Have sended "+file_size+" Bytes. \n \n");
			}
			else 
			{
				ta_msg.append("Send over. \n");
				ta_msg.append("Have sended "+file_size+" Bytes. \n \n");
			}
			
		}
		catch(IOException ee)
		{
			//System.out.println("tdx"+ee);
		}
		
		b_send.setEnabled(true);
	}

}

⌨️ 快捷键说明

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