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[] args) throws SocketException, IOException { 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 password= inFromClient.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_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(255, 51, 51))); // NOI18N jButton3.setFont(new java.awt.Font("Times New Roman", 1, 14)); // 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", 0, 14)); // NOI18N jLabel2.setText("Mật khẩu"); jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // 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", 1, 14)); // 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", 1, 14)); // 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(78, 78, 78) .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE) .addComponent(jButton2) .addGap(66, 66, 66)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(36, 36, 36) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(27, 27, 27) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtname, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE) .addComponent(txtpass)))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(52, 52, 52) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(26, 26, 26) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(txtpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(35, 35, 35) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE) .addComponent(jButton3) .addGap(22, 22, 22)) ); 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(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(44, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(18, Short.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.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</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(null, c); //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.SEVERE, null, ex); } }}