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

📄 mysslserver.java

📁 Java下模拟实现p2p平台下的洋葱路由协议
💻 JAVA
字号:
//此类主要实现通信

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.sql.*;
import javax.net.ssl.*;

import com.deitel.jhtp3.ch08.TableDisplay;
//import com.deitel.jhtp3.ch04.Enc1_RSA;
//import com.deitel.jhtp3.ch07.Dec_RSA;

public class MySSLServer extends JFrame{
	private JTextArea display,input1;
	private JLabel lab1,lab2;
	private GridLayout grid;
	private Container c;
	private JButton btn1;
	private JButton btn2;
	private JPanel pl1;
	
    ObjectOutputStream output;
    ObjectInputStream input;
    Socket connection;
    //int counter=1;
	
	public MySSLServer()
	{
		super("Server");
		Box b=Box.createHorizontalBox();
		Box b1=Box.createHorizontalBox();
		lab2=new JLabel("发送的信息:");
		lab1=new JLabel("接收的信息:");
		display=new JTextArea();
	
		b.add(new JScrollPane(display));
	
		input1=new JTextArea();
		b1.add(new JScrollPane(input1));
		btn1=new JButton("发送");
		btn2=new JButton("退出");
		pl1=new JPanel();
		grid=new GridLayout(5,1);
		pl1.add(btn1);
		pl1.add(btn2);
		c=getContentPane();
		c.setLayout(grid);
		c.add(lab1);
		c.add(b);
		c.add(lab2);
		c.add(b1);
		c.add(pl1);
		ButtonHandle handle =new ButtonHandle();
		ButtonHandle1 handle1=new ButtonHandle1();
		btn1.addActionListener(handle);
		btn2.addActionListener(handle1);
		setSize(500,400);
		show();
		
	}
	
	public void runServer()
	{   
	   
	    
	    try{
	       
	     //设置系统参数,证书的假名是xxd1212,密钥库名为xxdkeystore
		System.setProperty("javax.net.ssl.keyStore","xxdkeystore");
		System.setProperty("javax.net.ssl.keyStorePassword","xxd1212");
		
		//经过强制转换获得SSLServerSocketFactory类型的对象
		SSLServerSocketFactory ssf=(SSLServerSocketFactory)
		SSLServerSocketFactory.getDefault();
		
		//获得ServerSocket类型对象,参数指定一个整数作为端口号
		ServerSocket server=ssf.createServerSocket(5432);
		System.out.println("Waiting for connection...");
		   while(true)
		   {
			    connection=server.accept();
			    
			    //JOptionPane.showMessageDialog(c,"      Connection "+counter+"received from: "+
			    //                               connection.getInetAddress().getHostName());
			                                   
			    output=new ObjectOutputStream(connection.getOutputStream());
			    output.flush();
                input=new ObjectInputStream(connection.getInputStream());
                //output.writeObject("Welcome");
                			    
				
				
				/*try{
				   url="jdbc:odbc:portTable1";
				   
				   Class.forName("sun.jdbc.odbc.JdbcodbcDriver");
				   	
			    }*/
			         // Dec_RSA de=new Dec_RSA();
				
				   	  String msg=(String)input.readObject();//读取客户端发送的消息
				      msg=msg.trim();
				    //  while(!msg.equals("quit"))
				 //     {
				 	
				   
				   
				   //String gg=de.dec(msg);//用RSA算法解密(服务器的私钥)得到明文
		 		   
		 		   display.append(msg+"\n");//将客户发来的信息写入文本区
		 		   
				      
			     String[] buffer=msg.split(";");//数据包的元素用分号隔开
		 		   
		 		   //对读入的信息进行分类,对数据库进行一系列操作
				   if(!buffer[0].equals("4"))
				   {
				   	
				      //注册
				      if(buffer[0].equals("1"))
				      {
				      	String b;
				      //String bb="";
				     TableDisplay td1=new TableDisplay();
				      JOptionPane.showMessageDialog(c,"          对方请求注册或再上线!\n   更新信息...");
				      int p=Integer.parseInt(buffer[1]);
				      
				      //添加成功后查询假名并返回
				      b=td1.updateRecord7(p);
				      
				      /*if(b==1)
				          bb=td1.findRecord4(p);*/
				      
				    input1.append(b+"\n");
			         //input1.setCaretPosition(input1.getText().length());
			         
			         //input1.append("\naa\n");
				      }
				      
				      
				     //注销
				     if(buffer[0].equals("2"))
				     {
				    TableDisplay td2=new TableDisplay();
			         JOptionPane.showMessageDialog(c,"          对方已注销!\n  对数据库更新信息...");
					 
					 td2.updateRecord(Integer.parseInt(buffer[1]));
					 input1.append("\nWELL\n");
					 }
					 
					 
					 //退出
					 if(buffer[0].equals("3"))
				      
				     {
				      TableDisplay td3=new TableDisplay();				  
				   	  JOptionPane.showMessageDialog(c,"          对方已断开连接!");
				       
				      td3.deleteRecord(Integer.parseInt(buffer[1]));
				      input1.append("\nhello\n");
				     }
					 
					 
					 //请求假名
					 if(buffer[0].equals("5"))
				     {
				      TableDisplay td5=new TableDisplay();
			          JOptionPane.showMessageDialog(c,"          向接收方告知传送方!\n  查询并发送信息...");
					 //返回端口号
					  String st=td5.findRecord3(Integer.parseInt(buffer[1]));
					 input1.append(st+"\n");
					// input1.setCaretPosition(input1.getText().length());
					 //System.out.println(st);
					 //input1.append("\ndd\n");
					 }
			
				    
			   }
				  else
				    
				     {
				     TableDisplay td4=new TableDisplay();
			         JOptionPane.showMessageDialog(c,"          对方请求匿名!\n   查询并发送信息...");
			         //返回端口号
			         String stt=td4.findRecord2();
			         input1.append(stt+"\n");
			         input1.setCaretPosition(input1.getText().length());
			         //System.out.println(stt);
			         
			         //input1.append("\nee\n");
			         }
				 
				    /*catch (ClassNotFoundException cnfex){
				      	JOptionPane.showMessageDialog(c,"   Unknown object type received!);
				    }*/

		  }  
      }  
	      catch(ClassNotFoundException cnfex){}
		  catch(EOFException  eof){
					
					//display.append(ex.toString()+"\n");
					//ex.printStackTrace();
		  }
		  catch(IOException io){}
		  catch(Exception e1) {}
   }
			
			
			
	
    //监听发送
	public  class ButtonHandle implements ActionListener{
    	
    	public void actionPerformed(ActionEvent e)
    	{
        	if((input1.getText()).length()==0)
		 		{
		 			JOptionPane.showMessageDialog(c,"       请输入要发送的信息!");
		 		
		 			}
		 			
		 		 try
		 		 { 
		 		   String s=input1.getText();//从服务器发送文本区提取消息
		 		   //Enc1_RSA en=new Enc1_RSA(); 
		 		           
		 		   //String send=en.enc(s);//用RSA算法将消息加密(对方的公钥)
		 		           
		 	       output.writeObject(s);//向客户端输出消息
		 		   
                   
                   output.flush();
				    		
		 		   input1.setText("");
		 		  
		 		}
		 	  catch(IOException exception) {display.append(exception.toString()+"\n");
		 	                                exception.printStackTrace();}
    		  catch(Exception e2) {}
    		}
    	
  	}
	
	//监听退出
	private class ButtonHandle1 implements ActionListener{
    	
    	public void actionPerformed(ActionEvent e)
    	{
        	try{
		    		output.close();
		    		input.close();
		    		connection.close();
		     	    //socket.close();
		     	  //  ++counter;
		        //	svrsoc.close();
		            System.exit(0);
		            }
                 catch(Exception e1) {}
    		
    		}
    	
  	}
	
	public static void main(String args[])
	{
		MySSLServer sz =new MySSLServer();
		sz.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
			  System.exit(0);	
				}	
			});
		sz.runServer();
		
		}
	
	}

⌨️ 快捷键说明

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