KahnClifford

ClientGui.java

Jul 21st, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.FlowLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.DataInputStream;
  7. import java.io.DataOutputStream;
  8. import java.net.Socket;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.JScrollPane;
  14. import javax.swing.JTextArea;
  15. import javax.swing.JTextField;
  16. import javax.swing.text.DefaultCaret;
  17.  
  18. public class ClientGui extends JFrame{
  19.     //Initialize components
  20.     JPanel mainPanel = null;
  21.    
  22.     JTextArea chtArea = null;
  23.     JScrollPane chtScroll = null;
  24.     DefaultCaret chtScrCaret = null;
  25.    
  26.     JPanel subPanel = null;
  27.    
  28.     JTextField txtMessage = null;
  29.     JButton btnSnd = null;
  30.     JButton btnCls = null;
  31.     Client c = null;
  32.    
  33.     public ClientGui() {
  34.         super("ClientChat");
  35.        
  36.  
  37.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  38.         setVisible(true);
  39.        
  40.         //Create components and set properties
  41.         mainPanel = (JPanel)getContentPane();
  42.         mainPanel.setLayout(new BorderLayout());
  43.        
  44.         chtArea = new JTextArea();
  45.         chtScroll = new JScrollPane(chtArea);
  46.         chtScrCaret = (DefaultCaret)chtArea.getCaret();
  47.         chtScrCaret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
  48.        
  49.         subPanel = new JPanel();
  50.         subPanel.setLayout(new FlowLayout());
  51.        
  52.         txtMessage = new JTextField("");
  53.         txtMessage.setPreferredSize(new Dimension(300,20));
  54.        
  55.         btnSnd = new JButton("Send");
  56.         btnCls = new JButton("Close");
  57.        
  58.         btnSnd.addActionListener(new SendActionListener());
  59.         btnCls.addActionListener(new CloseActionListener());
  60.         btnSnd.setEnabled(false); // default false
  61.        
  62.         subPanel.add(txtMessage);
  63.         subPanel.add(btnSnd);
  64.         subPanel.add(btnCls);
  65.         mainPanel.add(chtScroll, BorderLayout.CENTER);
  66.         mainPanel.add(subPanel, BorderLayout.SOUTH);
  67.        
  68.         setSize(480,400);
  69.         setResizable(false);
  70.        
  71.         //Creates the client
  72.         c = new Client();
  73.        
  74.         //Keep reading the socket
  75.         while(true) {
  76.             chtArea.append("\nServer: "+ c.readSocket()+ "\n");
  77.            
  78.         }
  79.     }
  80.    
  81.     // Buttons ActionListeners class
  82.     public class SendActionListener implements ActionListener{
  83.         //For button send
  84.         @Override
  85.         public void actionPerformed(ActionEvent e) {
  86.             c.writeSocket(txtMessage.getText());
  87.             chtArea.append("\nYou: "+txtMessage.getText()+"\n");
  88.             txtMessage.setText("");
  89.            
  90.         }
  91.        
  92.     }
  93.    
  94.     public class CloseActionListener implements ActionListener{
  95.         //For button close
  96.         @Override
  97.         public void actionPerformed(ActionEvent e) {
  98.             System.exit(DISPOSE_ON_CLOSE);
  99.             c.closeConnection();
  100.            
  101.         }
  102.        
  103.     }
  104.    
  105.    
  106.     // Client class
  107.    
  108.     public class Client {
  109.        
  110.         // Initialize sockets and socket streams
  111.         Socket s = null;
  112.         DataInputStream cdis = null;
  113.         DataOutputStream cdos = null;
  114.         String address = "localhost";
  115.         int port = 3333;
  116.         boolean notConnected;
  117.        
  118.         //Create connection when Server is created
  119.         public Client() {
  120.             createConnection();
  121.         }
  122.        
  123.         public void createConnection() {
  124.             notConnected = true; //default false
  125.            
  126.             while(notConnected) {
  127.                
  128.                 //Create client connection
  129.                 try {
  130.                     chtArea.append("Creating client... \n");
  131.                     s = new Socket(address,port);
  132.                     notConnected = false;
  133.                     chtArea.setText("");
  134.                     chtArea.append("Client Established! \n");
  135.                     btnSnd.setEnabled(true);
  136.                    
  137.                 } catch (Exception e) {
  138.                    
  139.                     //When failed disable send button.
  140.                     chtArea.append("Error on Connection. \n");
  141.                     btnSnd.setEnabled(false);
  142.                    
  143.                     try {
  144.                         chtArea.append("Trying to reconnect... \n");
  145.                         Thread.sleep(2000);
  146.                     } catch (InterruptedException e1) {
  147.                         chtArea.append("Error reconnecting. \n");
  148.                         e1.printStackTrace();
  149.                     }
  150.                    
  151.                 }
  152.                
  153.             }
  154.            
  155.             //Creates the socket streams
  156.             try {
  157.                 cdis = new DataInputStream(s.getInputStream());
  158.                 cdos = new DataOutputStream(s.getOutputStream());
  159.                
  160.             } catch (Exception e) {
  161.                 chtArea.append("Error on creating streams. \n");
  162.             }
  163.            
  164.            
  165.            
  166.         }
  167.        
  168.         //for reading data on socket streams
  169.         public String readSocket() {
  170.             try {
  171.  
  172.                 return cdis.readUTF();
  173.             } catch (Exception e) {
  174.                 // try to reconnect when read fails
  175.                 c = new Client();
  176.                
  177.                 if(notConnected) {
  178.                     return "Error on read or connection timeout \n";
  179.                 }else {
  180.                     return "Reconnection success";
  181.                 }
  182.                
  183.             }
  184.         }
  185.        
  186.         //for writing data on socket streams
  187.         public String writeSocket(String ms) {
  188.            
  189.             try {
  190.                 cdos.writeUTF(ms);
  191.                 return ms;
  192.             } catch (Exception e) {
  193.                 return "Error on write or connection timeout\n";
  194.             }
  195.            
  196.         }
  197.        
  198.         //for closing socket connections
  199.         public void closeConnection(){
  200.            
  201.             try {
  202.                 s.close();
  203.                 cdis.close();
  204.                 cdos.close();
  205.  
  206.             } catch (Exception e) {
  207.                 chtArea.append("Error closing connection. \n");
  208.             }
  209.         }
  210.        
  211.     }
  212.    
  213.    
  214.    
  215. }
Advertisement
Add Comment
Please, Sign In to add comment