本帖最后由 neal1991 于 2013-05-14 13:51:41 编辑

解决方案 »

  1.   

    客户端
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.io.*;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.security.Key;
    import java.security.NoSuchAlgorithmException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    import javax.swing.border.Border;/**
     *
     * @author Administrator
     */
    public class Client extends JFrame implements Runnable
    {
        private Socket server=null;
        private JLabel l1=new JLabel("明文");
        private JLabel l2=new JLabel("密文");
        private JTextField T=new JTextField(20);
        private JTextArea TA1=new JTextArea();//显示明文
        private JTextArea TA2=new JTextArea();//显示密文
        private JButton B=new JButton("发送");//发送信息
        private JPanel p1=new JPanel();
        private PrintWriter pp=null;
        private BufferedReader bb=null;
        private JPanel p2=new JPanel();
        private String message=null;
        private JScrollPane JS1=new JScrollPane(TA1);
        private JScrollPane JS2=new JScrollPane(TA2);
        private Key key=Cry.getKey("123456");
        int port;
        public Client(int port) throws UnknownHostException, IOException, NoSuchAlgorithmException
        {
            this.setTitle("客户端");
            this.setLocation(200, 200);
            this.setSize(300, 300);
            init();
            this.port=port;
            this.setVisible(true);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        public void init()
        {
               this.setLayout(new BorderLayout(3,3));
               this.setBackground(Color.red);
               p1.setLayout(new GridLayout(2,1));
               JS2.setBorder(BorderFactory.createTitledBorder("密文区"));          
               p1.add(JS2);
               JS1.setBorder(BorderFactory.createTitledBorder("明文区"));          
               p1.add(JS1);
               this.add(p1,"Center");
               p2.setLayout(new GridLayout(1,2));
               p2.add(T);
               B.addActionListener(new ActionListener()
               {
                   public void actionPerformed(ActionEvent e) 
                   {
                        if(T.getText().trim().equals("")||T.getText().trim()==null)
                        {
                             JOptionPane.showMessageDialog(null, "不能为空!");
                             T.requestFocus();
                        }
                        else{
                            pp.println(Cry.enCry(T.getText(),key));
                            pp.flush();
                            TA1.append("Client:"+T.getText()+"\n");
                            TA2.append("Client:"+Cry.enCry(T.getText(),key)+"\n");
                            T.setText("");
                        }               }           });
               p2.add(B);
               this.add(p2,"South");    }
      
        private void getConnection(int port) {        try {
                server = new Socket("localhost",port);
                pp=new PrintWriter(new OutputStreamWriter(server.getOutputStream()));
                bb=new BufferedReader(new InputStreamReader(server.getInputStream()));
                System.out.println("OOK");
                communicate();
                this.close();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        }
        private void close() throws IOException{
               pp.close();
               bb.close();
               server.close();
        }    private void communicate() throws IOException{
              do{
                try {
                    message = bb.readLine();
                    TA1.append("Server:"+Cry.deCry(message, key)+"\n");
                    TA2.append("Server:"+message+"\n");
                } catch (IOException ex) {
                   
                }
                  
              } while(!message.equals("bye"));
           
        }    public void run() 
        {
                getConnection(port);
       }}
      

  2.   

    首先我这个登录都登陆不上去,我在电脑上测试了我的mysql的JDBC是可以连接的,不知道问题出现在哪,求高手解答