Advertisement
KahnClifford

ServerGui.java

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