Guest User

Untitled

a guest
Oct 21st, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. class PalClient implements ActionListener {
  8.     static Socket sock;
  9.     static PrintWriter socketWriter;
  10.     static BufferedReader socketReader;
  11.     static String inputString;
  12.     static String auth_string;
  13.     static JPanel panel;
  14.     static JButton submitAuth, submitInput;
  15.     static JTextField username, password, input;
  16.     static JTextArea status;
  17.     static void createPanel() {
  18.         panel = new JPanel();
  19.         submitAuth = new JButton("Authenticate");
  20.         submitInput = new JButton("Check");
  21.         username = new JTextField("joseph", 30);
  22.         password = new JTextField("pass123", 30);
  23.         input = new JTextField(30);
  24.         status = new JTextArea(30, 30);
  25.         submitAuth.addActionListener(new PalClient());
  26.         submitInput.addActionListener(new PalClient());
  27.         panel.add(username);
  28.         panel.add(password);
  29.         panel.add(submitAuth);
  30.         panel.add(input);
  31.         panel.add(submitInput);
  32.         panel.add(status);
  33.     }
  34.     public void actionPerformed(ActionEvent ae) {
  35.         String actionString = ae.getActionCommand();
  36.         String str;
  37.         System.out.println(actionString);
  38.         if (actionString == "Authenticate") {
  39.             auth_string = username.getText() + ";" + password.getText();
  40.             socketWriter.println(auth_string);
  41.             System.out.println(auth_string);
  42.             try {
  43.                 str = socketReader.readLine();
  44.                 System.out.print(str);
  45.                 if (str == "true")
  46.                     status.append("Login successul\n");
  47.             } catch (Exception e) {
  48.                 System.out.println(e);
  49.             }
  50.         }
  51.         if (actionString == "Check") {
  52.             try {
  53.             socketWriter.print(input.getText());
  54.             status.setText(socketReader.readLine());
  55.         }
  56.         catch (Exception e){}
  57.         }
  58.     }
  59.     public static void main(String[] args) {
  60.         JFrame f = new JFrame();
  61.         boolean accountExists = false;
  62.         // Initialize sockets
  63.         try {
  64.             sock = new Socket("127.0.0.1", 5003);
  65.             // Set up IO streams
  66.             socketWriter = new PrintWriter(sock.getOutputStream(), true);
  67.             socketReader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  68.             createPanel();
  69.             f.add(panel);
  70.             f.setBounds(0, 0, 400, 400);
  71.             f.setVisible(true);
  72.             f.setDefaultCloseOperation(3);
  73.         } catch (Exception e) {
  74.             System.out.println(e);
  75.         }
  76.     }
  77. }
Add Comment
Please, Sign In to add comment