[Java] lập trình mạng server gửi thông tin đăng nhập cho client

Thảo luận trong 'Java' bắt đầu bởi Nguyễn Thành Đạt It, 15/8/17.

  1. Nguyễn Thành Đạt It

    Nguyễn Thành Đạt It Guest Registered

    Tham gia ngày:
    28/9/16
    Bài viết:
    435
    Đã được thích:
    4
    Điểm thành tích:
    18
    Giới tính:
    Nam
    Nghề nghiệp:
    N/A
    Nơi ở:
    N/A
    Web:
    N/A

    Server:

    CODE:
    Select All
    import java.io.IOException;
    import java.io.PrintStream;
    import java.net.DatagramPacket;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketException;
    import java.util.Scanner;

    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author dat96
    */
    public class server {
        public static 
    void main(String[] argsthrows SocketExceptionIOException {
           
    ServerSocket server = new ServerSocket(1234);
           
    System.out.println("server sẵn sàng");
           
    Socket client server.accept();
           
    System.out.println("client đã kết nối được tới sever");
           
    Scanner inFromClient = new Scanner(client.getInputStream());
           
    PrintStream outToClient = new PrintStream(client.getOutputStream());
           
    String username inFromClient.nextLine();
           
    System.out.println("Client xác nhận username"username);
           
    outToClient.print("pass?");
            
    String passwordinFromClient.nextLine();
            
    System.out.println("Client xác nhận password"password);
            
    String a "admin";
            
    String b "1234";
            if (!
    username.equals(a) ||!password.equals(b)){
                       
    outToClient.print("Nhập sai");
            }
            else

            {
            
    outToClient.print("đăng nhập thành công");
    }
    }
    }

    Client:
    CODE:
    Select All
    import java.io.IOException;
    import java.io.PrintStream;
    import java.net.Socket;
    import java.util.Scanner;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JOptionPane;

    public class 
    client extends javax.swing.JFrame {

        
    /**
         * Creates new form client
         */
        
    public client() {
            
    initComponents();
        }

        
    /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        
    @SuppressWarnings("unchecked")
        
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
        
    private void initComponents() {

            
    jPanel1 = new javax.swing.JPanel();
            
    jButton3 = new javax.swing.JButton();
            
    jLabel2 = new javax.swing.JLabel();
            
    jLabel1 = new javax.swing.JLabel();
            
    txtname = new javax.swing.JTextField();
            
    txtpass = new javax.swing.JTextField();
            
    jButton2 = new javax.swing.JButton();
            
    jButton1 = new javax.swing.JButton();

            
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            
    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null"Login"javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATIONjavax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma"114), new java.awt.Color(2555151))); // NOI18N

            
    jButton3.setFont(new java.awt.Font("Times New Roman"114)); // NOI18N
            
    jButton3.setText("Exit");
            
    jButton3.setName("btnthoat"); // NOI18N
            
    jButton3.addActionListener(new java.awt.event.ActionListener() {
                public 
    void actionPerformed(java.awt.event.ActionEvent evt) {
                    
    jButton3ActionPerformed(evt);
                }
            });

            
    jLabel2.setFont(new java.awt.Font("Times New Roman"014)); // NOI18N
            
    jLabel2.setText("Mật khẩu");

            
    jLabel1.setFont(new java.awt.Font("Times New Roman"014)); // NOI18N
            
    jLabel1.setText("Tên tài khoản");

            
    txtname.setName("txtname"); // NOI18N

            
    txtpass.setName("txtpass"); // NOI18N

            
    jButton2.setFont(new java.awt.Font("Times New Roman"114)); // NOI18N
            
    jButton2.setText("reset");
            
    jButton2.setName("btnreset"); // NOI18N
            
    jButton2.addActionListener(new java.awt.event.ActionListener() {
                public 
    void actionPerformed(java.awt.event.ActionEvent evt) {
                    
    jButton2ActionPerformed(evt);
                }
            });

            
    jButton1.setFont(new java.awt.Font("Times New Roman"114)); // NOI18N
            
    jButton1.setText("Login");
            
    jButton1.setName("btndangnhap"); // NOI18N
            
    jButton1.addActionListener(new java.awt.event.ActionListener() {
                public 
    void actionPerformed(java.awt.event.ActionEvent evt) {
                    
    jButton1ActionPerformed(evt);
                }
            });

            
    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            
    jPanel1.setLayout(jPanel1Layout);
            
    jPanel1Layout.setHorizontalGroup(
                
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .
    addGroup(jPanel1Layout.createSequentialGroup()
                    .
    addGap(787878)
                    .
    addComponent(jButton1)
                    .
    addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED94Short.MAX_VALUE)
                    .
    addComponent(jButton2)
                    .
    addGap(666666))
                .
    addGroup(jPanel1Layout.createSequentialGroup()
                    .
    addGap(363636)
                    .
    addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .
    addComponent(jButton3javax.swing.GroupLayout.PREFERRED_SIZE65javax.swing.GroupLayout.PREFERRED_SIZE)
                        .
    addGroup(jPanel1Layout.createSequentialGroup()
                            .
    addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADINGfalse)
                                .
    addComponent(jLabel1javax.swing.GroupLayout.DEFAULT_SIZEjavax.swing.GroupLayout.DEFAULT_SIZEShort.MAX_VALUE)
                                .
    addComponent(jLabel2javax.swing.GroupLayout.DEFAULT_SIZEjavax.swing.GroupLayout.DEFAULT_SIZEShort.MAX_VALUE))
                            .
    addGap(272727)
                            .
    addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADINGfalse)
                                .
    addComponent(txtnamejavax.swing.GroupLayout.DEFAULT_SIZE91Short.MAX_VALUE)
                                .
    addComponent(txtpass))))
                    .
    addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZEShort.MAX_VALUE))
            );
            
    jPanel1Layout.setVerticalGroup(
                
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .
    addGroup(jPanel1Layout.createSequentialGroup()
                    .
    addGap(525252)
                    .
    addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .
    addComponent(jLabel1)
                        .
    addComponent(txtnamejavax.swing.GroupLayout.PREFERRED_SIZEjavax.swing.GroupLayout.DEFAULT_SIZEjavax.swing.GroupLayout.PREFERRED_SIZE))
                    .
    addGap(262626)
                    .
    addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .
    addComponent(jLabel2)
                        .
    addComponent(txtpassjavax.swing.GroupLayout.PREFERRED_SIZEjavax.swing.GroupLayout.DEFAULT_SIZEjavax.swing.GroupLayout.PREFERRED_SIZE))
                    .
    addGap(353535)
                    .
    addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .
    addComponent(jButton1)
                        .
    addComponent(jButton2))
                    .
    addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED27Short.MAX_VALUE)
                    .
    addComponent(jButton3)
                    .
    addGap(222222))
            );

            
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            
    getContentPane().setLayout(layout);
            
    layout.setHorizontalGroup(
                
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .
    addGroup(layout.createSequentialGroup()
                    .
    addContainerGap()
                    .
    addComponent(jPanel1javax.swing.GroupLayout.PREFERRED_SIZEjavax.swing.GroupLayout.DEFAULT_SIZEjavax.swing.GroupLayout.PREFERRED_SIZE)
                    .
    addContainerGap(44Short.MAX_VALUE))
            );
            
    layout.setVerticalGroup(
                
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .
    addGroup(layout.createSequentialGroup()
                    .
    addContainerGap()
                    .
    addComponent(jPanel1javax.swing.GroupLayout.PREFERRED_SIZEjavax.swing.GroupLayout.DEFAULT_SIZEjavax.swing.GroupLayout.PREFERRED_SIZE)
                    .
    addContainerGap(18Short.MAX_VALUE))
            );

            
    pack();
        }
    // </editor-fold>                      

        
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                       
            
    System.exit(0);
        }                                      

        private 
    void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                       
            
    txtname.setText("");
            
    txtpass.setText("");
        }                                      

        private 
    void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
            
    String username txtname.getText();
            
    String password txtpass.getText();
            if(
    username.equals("")||password.equals("")){
                
    JOptionPane.showMessageDialog(null"nhập sai");

           }
            else{
                
    login();
        }                                      
        }
        
    /**
         * @param args the command line arguments
         */
        
    public static void main(String args[]) {
            
    /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
             */
            
    try {
                for (
    javax.swing.UIManager.LookAndFeelInfo info javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if (
    "Nimbus".equals(info.getName())) {
                        
    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (
    ClassNotFoundException ex) {
                
    java.util.logging.Logger.getLogger(client.class.getName()).log(java.util.logging.Level.SEVEREnullex);
            } catch (
    InstantiationException ex) {
                
    java.util.logging.Logger.getLogger(client.class.getName()).log(java.util.logging.Level.SEVEREnullex);
            } catch (
    IllegalAccessException ex) {
                
    java.util.logging.Logger.getLogger(client.class.getName()).log(java.util.logging.Level.SEVEREnullex);
            } catch (
    javax.swing.UnsupportedLookAndFeelException ex) {
                
    java.util.logging.Logger.getLogger(client.class.getName()).log(java.util.logging.Level.SEVEREnullex);
            }
            
    //</editor-fold>

            /* Create and display the form */
            
    java.awt.EventQueue.invokeLater(new Runnable() {
                public 
    void run() {
                    new 
    client().setVisible(true);
                }
            });
        }

        
    // Variables declaration - do not modify                   
        
    private javax.swing.JButton jButton1;
        private 
    javax.swing.JButton jButton2;
        private 
    javax.swing.JButton jButton3;
        private 
    javax.swing.JLabel jLabel1;
        private 
    javax.swing.JLabel jLabel2;
        private 
    javax.swing.JPanel jPanel1;
        private 
    javax.swing.JTextField txtname;
        private 
    javax.swing.JTextField txtpass;
        
    // End of variables declaration                 

        
    private void login() {
            try {
                
    Socket client = new Socket ("localhost"1234);
                
    System.out.println ("client sẵn sàng");
                
    Scanner inFromServer = new Scanner(client.getInputStream());
                
    PrintStream outToServer = new PrintStream(client.getOutputStream());
                
    String a txtname.getText();
                
    String b txtpass.getText();
                
    outToServer.print(a);
               
    System.out.println("server :"inFromServer.nextLine());
                
    outToServer.print(b);
                
    String c inFromServer.nextLine();
                            
    JOptionPane.showMessageDialog(nullc);

              
                
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            
    } catch (IOException ex) {
                
    Logger.getLogger(client.class.getName()).log(Level.SEVEREnullex);
            }
        }
    }
    upload_2017-8-16_10-55-35.png

     

    Bình Luận Bằng Facebook

    data-href="https://cnttqn.com/threads/java-lap-trinh-mang-server-gui-thong-tin-dang-nhap-cho-client.4925.html"