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

📄 p2psys.java

📁 任意多台机器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    popupMenu.add(groupmenu);//在iplist处添加
	    
	    //getContentPane().add(popupMenu);
	     
	    //小组树
	    
        TreeNode rootnote = initGroup();
        treemodel = new DefaultTreeModel(rootnote);
        tree = new JTree(treemodel);
        tree.setEditable(true);
        
        scrolltree = new JScrollPane(tree);
        
	    
	    //IP Label
        ipLabel=new JLabel("IP 列表:");
        ipLabel.setBounds(370 ,10 ,80,20);
        getContentPane().add(ipLabel);      

        //一个IP列表
        ipList=new JList(modeIp); 
        scrollIp=new JScrollPane(ipList);
        scrollIp.setBounds(370 ,35 ,110,180);
        ipList.setCellRenderer(new IconListItemRenderer(P2pSys.this)); 
        getContentPane().add(scrollIp);
        
        ipList.addMouseListener(new  MouseAdapter()  
        {  
        	public void mouseClicked(MouseEvent e) 
        	{  
        		        if(e.getClickCount() == 2)  
        		        //the  thing  you  want  to  do 
        		        {
        		        	if(SimpleP2pSysShow != 1)
        	            	 {   	 
        	            		 
        	            		 ipTextField.setText(ipList.getSelectedValue() .toString() );
        	            	 }
        	            	 else
        	            		 
        	            	 {   
        	            		 SimpleSendSys simplesendsys = new SimpleSendSys(P2pSys.this);
        	                	 simplesendsys.frame.show();
        	                	 simplesendsys.frame.setTitle("发送给  "+selectip.trim());
        	                	 simplesendsys.frame.setIconImage(selecticon.getImage());
        	                	 simplesendsys.sAddress = myfriend[ipList.getSelectedIndex()];
        	                	 //selectip = selectip.substring(selectip.indexOf('|')+3 );
        	                	 System.out.print(" \n getSelectedIndex   "+ipList.getSelectedIndex()); 
        	            		 ipTextField.setText(selectip);       	                 
        	            		 System.out.print("\ndddddddd: "+myfriend[ipList.getSelectedIndex()]); 
        	            	 }
        		        }
        		       
        	    }
        	
        	public void mousePressed(MouseEvent e) 
        	{
        		int mods=e.getModifiers();
        		ipList.setComponentPopupMenu(popupMenu);
        		if((mods&InputEvent.BUTTON3_MASK)!=0)
        		{
        			
        			SelectedIndex = ipList.getSelectedIndex();
        		}
        	}
        	      
        	
         });
      
        
         //-------------启动三个线程--------------
         receiveMutiCast.start() ;
         receiveP2p.start() ;
         infoResponse.start() ;
        
         
        //
        addWindowListener(new 
        WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
            	
                String string=xmlFormat.xmlForm("exitSys","go away","",getLocalIp(),"",myName,"","") .trim() ;
                sender.sendMutiCast(string);
                receiveMutiCast.stopWork() ;
                receiveP2p.stopWork() ;
                infoResponse.stopWork() ;
                //fileTransfer.stop();
                System.exit(0);

            }
        }
        );
    }

    //发送消息
    public  void actionPerformed(ActionEvent e)
    {	   	
    	//fileTransfer.receivefile();
        Object object=e.getSource() ;
        int i = 0 ;
        int ipyes = 0 ;
        
        //单击发送按钮
        if(object.equals(sendButton) )
        {
          
        	for(i=0;i<modeIp .size();i++ )
            {
               if(ipTextField.getText().equals(modeIp.getElementAt(i)))
               {
            	   ipyes = 1;
            	   break;
               }
      
            }  
        	
        	if(ipTextField.getText().equals("")||ipyes != 1)
            {
            	JOptionPane.showMessageDialog(null,
                        "请正确选择接收者IP地址",
                        "发送失败",
                        JOptionPane.INFORMATION_MESSAGE);	
            } 	
		    else if(messageTextField.getText().equals("")&& fileField.getText().equals(""))
            {
            	JOptionPane.showMessageDialog(null,
                        "请输入要发送的消息或发送文件",
                        "发送失败",
                        JOptionPane.INFORMATION_MESSAGE);	
            }                
            else
            {   
            	//首先在消息区增加两条消息,           	
                modeMsg2.addElement("To :  "+ipTextField.getText()+"    ("+java.util.Calendar.getInstance().getTime()+")");                
                modeMsg2.addElement(messageTextField.getText());
                
                String sAddress=ipTextField.getText() .trim() ;
                String ip=getLocalIp() ;
                String str=messageTextField.getText() .toString().trim() ;
                String file=fileField.getText().toString().trim() ;
                
                
                String string=xmlFormat.xmlForm("p2p",str,file,ip,"8888",myName,myface,myemail)  ;

                if(!fileField.getText().equals(""))
                {              	              	             	   
            	   modeMsg2.addElement("file:  "+fileField.getText());
            	   //文件在准备好transferfile()之前不可继续发送消息
            	   filehashtable.addHashfileItem(sAddress,file);
            	   //fileItem.addElement(fileName);
            	   //fileButton.setEnabled(false);
            	   //sendButton.setEnabled(false);
                }
                            
               
                sender.sendP2p (string,sAddress);
                //fileTransfer.transferfile();
                messageTextField.setText("");               
                //fileButton.setText("清除");
                
           }
        	
        }
        
         if(object.equals(getIpButton))
         {
             modeIp.clear() ;
             ipTextField.setText("");
             String ip = getLocalIp() ;
			
			
			 
             
             String string = xmlFormat.xmlForm("getIp","","",ip,"","   "+myName,myface,myemail) .trim() ;
             sender.sendMutiCast(string) ;
        }
         
         //if(object.equals(clearButton))
         //{
        	// modeMsg.clear(); 
         //}
         
         if(object.equals(fileButton))
         {
        	 if(fileField.getText().equals(""))
        	 {
        			 
	        	 int result;
	        	 fc = new JFileChooser("C:\\temp\\"); 
	        	 result = fc.showOpenDialog(this); 
	        	 File file = fc.getSelectedFile(); 
	        	 if(file!=null && result==JFileChooser.APPROVE_OPTION) 
	        	 { 
		        	 fileName = file.getAbsolutePath(); 
		        	 fileField.setText(fileName);
		        	 //System.out.println("You chose to open this file: " +fileName); 
		        	 fileButton.setText("清除");
		        	 
	        	 }  
	        	
        	 }
        	 else
        	 {
        		
        		 fileField.setText("");	
        		 fileButton.setText("打开");
        	 }
        	 
         }
         //if(object.equals(downloadButton))
         //{
        	 //BufferedReader user; 
             //int port; 
              
             //user = new BufferedReader(new InputStreamReader(System.in)); 
             //port = Integer.parseInt("8888"); 
             //System.out.println("将接收文件于端口: " + port);
             //String ip = getLocalIp() ;
             //String sAddress;
             
             //sAddress = modeMsg.getElementAt(modeMsg.size()-3).toString().trim();
             //sAddress=sAddress.substring(sAddress.indexOf(':')+1 ).toString().trim();
             
             //System.out.println(newMsg);
             
             //try 
             //{
     			//serverSkt = new ServerSocket(port);
     		 //} 
             //catch (IOException e1) 
             //{	
             	//System.out.println(e1.toString());
     		 //} 
             
             //String string=xmlFormat.xmlForm("p2p","sure","",ip,"8888")  ;
             //sender.sendP2p (string,sAddress);
             //downloadButton.setEnabled(false);             
         //}
         if(object.equals(historyButton))
         {
        	 if(historyButton.getLabel()=="显示记录" )
        	 {
	        	 inputMessageLabel.setText("发送记录:");
	        	 scrollMsg.hide();
	        	 scrollMessage2.show();
	        	 historyButton.setText("隐藏记录");
        	 }
        	 else
        	 {
        		 inputMessageLabel.setText("编辑消息:");
        		 scrollMsg.show();
	        	 scrollMessage2.hide();
	        	 historyButton.setText("显示记录");
        	 }
         }
    }
    

    public String  getLocalIp()
    {
        String ip="";
        
        try
        {
            InetAddress inetAddress=InetAddress.getLocalHost() ;
            ip=inetAddress.toString() ;
            
            ip=ip.substring(ip.indexOf('/')+1 ) ;

        }
        catch(Exception e)
        {
            System.out.println("in getLocalIp() "+e);
        }
        return ip;

    }
    
    public String  getLocalHostname()
    {
      
        String hostname="";
        
        try 
        {
			hostname = InetAddress.getLocalHost().getHostName().toString();
		} 
        catch (UnknownHostException e1) 
        {
			// TODO 自动生成 catch 块
			e1.printStackTrace();
		}
            
        return hostname;

    }
    
//  具体实现按钮双击的功能的方法//已停用
    public boolean checkClickTime() 
    {
    	
	    long nowTime = (new Date()).getTime();
	    if ( (nowTime - clickTime) < 300) 
	    {
		    clickTime = nowTime;
		    return true;
	    }
	    clickTime = nowTime;
	    return false;

    }

    public TreeNode initGroup() 
    {
    	 final DefaultMutableTreeNode root = new DefaultMutableTreeNode(myName+" 的小组");
    	 String s;
         
         try
         {
   	      RandomAccessFile file = new RandomAccessFile("MyGroup/MyGroup.txt","r");
   	      long filepoint=0;
   	      long length=file.length();
   	      while(filepoint<length)
   	      {
   	    	  
   		        s=file.readLine(); 
   		    
   		        s=xmlParse.parse(s,"groupname").trim();
   		        
   		        root.add(new DefaultMutableTreeNode(s));
   		        
   		        filepoint=file.getFilePointer();
   	      
   	      }
   	      file.close();
         }
         catch(Exception e)
         {
           JOptionPane.showMessageDialog(null, e.getMessage());
         }
         
    	 
    	 return root;
    }
    
    public void initGroupmenu()
    {
    	DefaultMutableTreeNode root = new DefaultMutableTreeNode(myName+" 的小组");
   	    String s;
   	    
        try
        {
  	      RandomAccessFile file = new RandomAccessFile("MyGroup/MyGroup.txt","r");
  	      long filepoint=0;
  	      long length=file.length();
  	      while(filepoint<length)
  	      {
  	    	  
  		        s=file.readLine(); 
  		    
  		        s=xmlParse.parse(s,"groupname").trim();
  		            	   
  		        groupmenu.add(new JMenuItem(s));
  		       
  		         System.out.println("oooooo  " +s); 
  	              
  	      }
  	      file.close();
        }
        catch(Exception e)
        {
          //JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }
    
    public boolean fileIsNull(String filePath,String fileName) throws IOException 
    { 
	    boolean result = false; 
	    FileReader fr = new FileReader(filePath+fileName); 
	    
	    if(fr.read() == -1) 
	    { 
		    result = true; 
		    System.out.println(fileName+" 文件中没有数据!"); 
	    } 
	    else 
	    { 
	    	System.out.println(fileName+" 文件中有数据!"); 
	    } 
	    fr.close(); 
	    
	    return result; 
    } 

    
    public static void main(String[] args)
    {       
    	//EmailPassSys emailpass = new EmailPassSys();  
    	P2pSys p2pSys=new P2pSys();
    	p2pSys.simplep2psys = new SimpleP2pSys(p2pSys);  
    	p2pSys.SimpleP2pSysShow = 1;
    	//p2pSys.hide();
        //p2pSys.show();
        
    }
    
}

⌨️ 快捷键说明

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