2.4.3到服务器验证用户名和密码

2017-07-14  本文已影响0人  linhaoyou
Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png
import java.net.*;
import java.io.*;


public class QQServer {
    public static void main(String[] args){
        try {
            //服务器在8000端口监听
            ServerSocket ss = new ServerSocket(8000);
            
            System.out.println("服务器正在8000端口监听.......");
            Socket s = ss.accept();
            
            //接收用户名和密码
            InputStream is = s.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            
            String uandp = br.readLine();
            
            //检验点
            System.out.println(uandp);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
Paste_Image.png Paste_Image.png
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.*;

public class QQLogin extends JFrame implements ActionListener{
    //new 组件
    JLabel labUser = new JLabel("用户名");
    JLabel labPass = new JLabel("密码");
    
    JTextField txtUser = new JTextField();
    JPasswordField txtPass = new JPasswordField();
    
    JButton btnLogin = new JButton("登录");
    JButton btnReg = new JButton("注册");
    JButton btnCancel = new JButton("取消");
    
    //布置输入面板
    JPanel panInput = new JPanel();
    
    //布置按钮面板
    JPanel panButton = new JPanel();
    
    public QQLogin() {
        this.setSize(250, 125);
        //注册事件监听
        btnLogin.addActionListener(this);
        btnReg.addActionListener(this);
        btnCancel.addActionListener(this);
        
        //布置输入面板
        panInput.setLayout(new GridLayout(2, 2));
        panInput.add(labUser);
        panInput.add(txtUser);
        
        panInput.add(labPass);
        panInput.add(txtPass);
        
        //布置按钮面板
        panButton.setLayout(new FlowLayout());
        
        panButton.add(btnLogin);
        panButton.add(btnReg);
        panButton.add(btnCancel);
        
        //布置窗体
        this.setLayout(new BorderLayout());
        
        this.add(panInput, BorderLayout.CENTER);
        this.add(panButton, BorderLayout.SOUTH);
        
    }
    public static void main(String args[]){
        Object w = new QQLogin();
        ((QQLogin)w).setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent arg0){
        if(arg0.getActionCommand().equals("登录")){
//          System.out.println("用户点击了登录");
            //发送用户名和密码到服务器端
            String user = txtUser.getText();
            String pass = txtPass.getText();
            try {
                Socket s = new Socket("127.0.0.1", 8000);
                
                OutputStream os = s.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                PrintWriter pw = new PrintWriter(osw,true);
                
                pw.println(user + "%" + pass);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        if(arg0.getActionCommand().equals("注册")){
            System.out.println("用户点击了注册");
        }
        if(arg0.getActionCommand().equals("取消")){
            System.out.println("用户点击了取消");
        }
    }
}
Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png
import java.net.*;
import java.io.*;


public class QQServer {
    public static void main(String[] args){
        try {
            //服务器在8000端口监听
            ServerSocket ss = new ServerSocket(8000);
            
            System.out.println("服务器正在8000端口监听.......");
            Socket s = ss.accept();
            
            //接收用户名和密码
            InputStream is = s.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            
            String uandp = br.readLine();
            
            //检验点
            //拆分用户名和密码
            String u = uandp.split("%")[0];
            String p = uandp.split("%")[1];
            
            OutputStream os = s.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            PrintWriter pw = new PrintWriter(osw, true);
            
            if (u.equals("aaa")&&p.equals("111")) {
                //发送正确信息到客户端
                pw.println("ok");
            }else {
                //发送错误信息到客户端
                pw.println("err");
            }
            System.out.println(uandp);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.*;

public class QQLogin extends JFrame implements ActionListener{
    //new 组件
    JLabel labUser = new JLabel("用户名");
    JLabel labPass = new JLabel("密码");
    
    JTextField txtUser = new JTextField();
    JPasswordField txtPass = new JPasswordField();
    
    JButton btnLogin = new JButton("登录");
    JButton btnReg = new JButton("注册");
    JButton btnCancel = new JButton("取消");
    
    //布置输入面板
    JPanel panInput = new JPanel();
    
    //布置按钮面板
    JPanel panButton = new JPanel();
    
    public QQLogin() {
        this.setSize(250, 125);
        //注册事件监听
        btnLogin.addActionListener(this);
        btnReg.addActionListener(this);
        btnCancel.addActionListener(this);
        
        //布置输入面板
        panInput.setLayout(new GridLayout(2, 2));
        panInput.add(labUser);
        panInput.add(txtUser);
        
        panInput.add(labPass);
        panInput.add(txtPass);
        
        //布置按钮面板
        panButton.setLayout(new FlowLayout());
        
        panButton.add(btnLogin);
        panButton.add(btnReg);
        panButton.add(btnCancel);
        
        //布置窗体
        this.setLayout(new BorderLayout());
        
        this.add(panInput, BorderLayout.CENTER);
        this.add(panButton, BorderLayout.SOUTH);
        
    }
    public static void main(String args[]){
        Object w = new QQLogin();
        ((QQLogin)w).setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent arg0){
        if(arg0.getActionCommand().equals("登录")){
//          System.out.println("用户点击了登录");
            //发送用户名和密码到服务器端
            String user = txtUser.getText();
            String pass = txtPass.getText();
            try {
                Socket s = new Socket("127.0.0.1", 8000);
                
                OutputStream os = s.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                PrintWriter pw = new PrintWriter(osw,true);
                
                pw.println(user + "%" + pass);
                
                //接受服务器发送回来的确认信息
                InputStream is = s.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                
                String yorn = br.readLine();
                
                //显示主窗体
                if (yorn.equals("ok")) {
                    QQMain w = new QQMain();
                    w.setVisible(true);
                    this.setVisible(false);
                }else {
                    JOptionPane.showMessageDialog(this, "对不起,用户名或密码错误");
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        if(arg0.getActionCommand().equals("注册")){
            System.out.println("用户点击了注册");
        }
        if(arg0.getActionCommand().equals("取消")){
            System.out.println("用户点击了取消");
        }
    }
}
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class QQMain extends JFrame implements ActionListener {
    JTextField txtMess = new JTextField();
    JComboBox cmbUser = new JComboBox();
    JTextArea txtContent = new JTextArea();
    
    QQMain(){
        this.setSize(300, 400);
        
        //new组件
        JButton btnSend = new JButton("发送");
        
        JScrollPane spContent = new JScrollPane(txtContent);
        
        //注册事件监听
        btnSend.addActionListener(this);
        
        //布置小面板
        JPanel panSmall = new JPanel();
        panSmall.setLayout(new GridLayout(1, 2));
        
        panSmall.add(cmbUser);
        panSmall.add(btnSend);
        
        //布置大面板
        JPanel panBig = new JPanel();
        panBig.setLayout(new GridLayout(2, 1));
        
        panBig.add(txtMess);
        panBig.add(panSmall);
        
        //布置窗体
        this.setLayout(new BorderLayout());
        
        this.add(panBig, BorderLayout.NORTH);
        this.add(spContent,BorderLayout.CENTER);
        
        //读聊天记录
        try {
            File f = new File("D:/work/聊天记录.qq");
                
            FileReader fr = new FileReader(f);
            BufferedReader br = new BufferedReader(fr);
                    
            while (br.ready()) {
                txtContent.append(br.readLine()+"\n");      
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    
    @Override
    public void actionPerformed(ActionEvent arg0){
        //txtMess ====>txtContent
        txtContent.append(txtMess.getText()+"\n");
        
        //将txtMess的内容存入聊天记录文件
        try {
            File f = new File("D:/work/聊天记录.qq");
            
            FileWriter fw = new FileWriter(f,true);
            PrintWriter pw = new PrintWriter(fw);
            
            pw.println(txtMess.getText());
            
            pw.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
        //清除txtMess中的内容
        txtMess.setText("");
    }
}
Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png
class A{
    int i = 10;
    public void show() {
        System.out.println(i);
    }
}

class B extends A{
    int i = 100;
    public void show() {
        System.out.println(i);
    }
}

public class MyTest {
    public static void main(String[] args){
        A a = new B();
        a.show();
                System.out.println(a.i);
    }
}

运行结果:
100
10

Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png
import java.net.*;
import java.io.*;


public class QQServer {
    public static void main(String[] args){
        try {
            //服务器在8000端口监听
            ServerSocket ss = new ServerSocket(8000);
            
            System.out.println("服务器正在8000端口监听.......");
            Socket s = ss.accept();
            
            //接收用户名和密码
            InputStream is = s.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            
            String uandp = br.readLine();
            
            //检验点
            System.out.println(uandp);
            
            //拆分用户名和密码
            String u = "";
            String p = "";
            try {
                u = uandp.split("%")[0];
                p = uandp.split("%")[1];
            } catch (Exception e) {
                System.out.println(e);
                // TODO: handle exception
            }
            
            OutputStream os = s.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            PrintWriter pw = new PrintWriter(osw, true);
            
            if("aaa".equals(u)&& "111".equals(p)){
                //发送正确信息到客户端
                pw.println("ok");
                
                //不断发接收客户端发送过来的信息
                while (true) {
                    String message = br.readLine();
                    System.out.println(message);
                }               
            }else {
                //发送错误信息到客户端
                pw.println("err");
                System.out.println("err");
            }
            System.out.println(uandp);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
/**
 * QQLogin,登录界面和逻辑
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class QQLogin extends JFrame implements ActionListener{
    //new 组件
    JLabel labUser = new JLabel("用户名");
    JLabel labPass = new JLabel("密码");
    
    JTextField txtUser = new JTextField();
    JPasswordField txtPass = new JPasswordField();
    
    JButton btnLogin = new JButton("登录");
    JButton btnReg = new JButton("注册");
    JButton btnCancel = new JButton("取消");
    
    //布置输入面板
    JPanel panInput = new JPanel();
    
    //布置按钮面板
    JPanel panButton = new JPanel();
    
    public QQLogin() {
        this.setSize(250, 125);
        //注册事件监听
        btnLogin.addActionListener(this);
        btnReg.addActionListener(this);
        btnCancel.addActionListener(this);
        
        //布置输入面板
        panInput.setLayout(new GridLayout(2, 2));
        panInput.add(labUser);
        panInput.add(txtUser);
        
        panInput.add(labPass);
        panInput.add(txtPass);
        
        //布置按钮面板
        panButton.setLayout(new FlowLayout());
        
        panButton.add(btnLogin);
        panButton.add(btnReg);
        panButton.add(btnCancel);
        
        //布置窗体
        this.setLayout(new BorderLayout());
        
        this.add(panInput, BorderLayout.CENTER);
        this.add(panButton, BorderLayout.SOUTH);
        
    }
    public static void main(String args[]){
        Object w = new QQLogin();
        ((QQLogin)w).setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent arg0){
        if(arg0.getActionCommand().equals("登录")){
//          System.out.println("用户点击了登录");
            //发送用户名和密码到服务器端
            String user = txtUser.getText();
            String pass = txtPass.getText();
            try {
                Socket s = new Socket("127.0.0.1", 8000);
                
                OutputStream os = s.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                PrintWriter pw = new PrintWriter(osw,true);
                
                pw.println(user + "%" + pass);
                System.out.println(user + "%" + pass);
                
                //接受服务器发送回来的确认信息
                InputStream is = s.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                
                String yorn = br.readLine();
                System.out.println(yorn);
                
                //显示主窗体
                if("ok".equals(yorn)){
                    QQMain w = new QQMain();
                    w.setSocket(s);
                    w.setVisible(true);
                    this.setVisible(false);
                }else {
                    JOptionPane.showMessageDialog(this, "对不起,用户名或密码错误");
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        if(arg0.getActionCommand().equals("注册")){
            System.out.println("用户点击了注册");
        }
        if(arg0.getActionCommand().equals("取消")){
            System.out.println("用户点击了取消");
        }
    }
}
/**
 * QQMain,主界面
 */
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

public class QQMain extends JFrame implements ActionListener {
    private Socket s;
    JTextField txtMess = new JTextField();
    JComboBox cmbUser = new JComboBox();
    JTextArea txtContent = new JTextArea();
    
    QQMain(){
        this.setSize(300, 400);
        
        //new组件
        JButton btnSend = new JButton("发送");
        
        JScrollPane spContent = new JScrollPane(txtContent);
        
        //注册事件监听
        btnSend.addActionListener(this);
        
        //布置小面板
        JPanel panSmall = new JPanel();
        panSmall.setLayout(new GridLayout(1, 2));
        
        panSmall.add(cmbUser);
        panSmall.add(btnSend);
        
        //布置大面板
        JPanel panBig = new JPanel();
        panBig.setLayout(new GridLayout(2, 1));
        
        panBig.add(txtMess);
        panBig.add(panSmall);
        
        //布置窗体
        this.setLayout(new BorderLayout());
        
        this.add(panBig, BorderLayout.NORTH);
        this.add(spContent,BorderLayout.CENTER);
        
        //读聊天记录
        try {
            File f = new File("D:/work/聊天记录.qq");
                
            FileReader fr = new FileReader(f);
            BufferedReader br = new BufferedReader(fr);
                    
            while (br.ready()) {
                txtContent.append(br.readLine()+"\n");      
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    
    public void setSocket(Socket value) {
        s = value;
    }
    
    @Override
    public void actionPerformed(ActionEvent arg0){
        //txtMess ====>txtContent
        txtContent.append(txtMess.getText()+"\n");
        
        //将txtMess的内容存入聊天记录文件
        try {
            File f = new File("D:/work/聊天记录.qq");
            
            FileWriter fw = new FileWriter(f,true);
            PrintWriter pw = new PrintWriter(fw);
            
            pw.println(txtMess.getText());
            
            pw.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
        
        //发送信息到服务器端
        try {
            OutputStream os = s.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            PrintWriter pw = new PrintWriter(osw, true);
            
            pw.println(txtMess.getText());
        } catch (Exception e) {
            // TODO: handle exception
        }
        //清除txtMess中的内容
        txtMess.setText("");
    }
}

运行错误结果:


Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png

运行正确结果:

Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png
上一篇下一篇

猜你喜欢

热点阅读