Advertisement
Guest User

ServerGUI.java

a guest
Oct 21st, 2014
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.59 KB | None | 0 0
  1. import javax.swing.JFrame;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5.  
  6. import javax.swing.JPanel;
  7.  
  8. import java.awt.SystemColor;
  9.  
  10. import javax.swing.JTextArea;
  11. import javax.swing.JTextField;
  12. import javax.swing.JLabel;
  13. import javax.swing.JRadioButton;
  14. import javax.swing.JButton;
  15. import javax.swing.SwingUtilities;
  16.  
  17. import java.awt.Toolkit;
  18.  
  19. import javax.swing.ButtonGroup;
  20.  
  21. import java.awt.event.ActionListener;
  22. import java.awt.event.ActionEvent;
  23.  
  24. import org.eclipse.wb.swing.FocusTraversalOnArray;
  25.  
  26. import java.awt.Component;
  27.  
  28. public class ServerGUI extends JFrame implements ActionListener {
  29.  
  30.     private static final long serialVersionUID = 1L;
  31.     private JFrame frame;
  32.     public JTextField enterField;
  33.     public JTextField txtServerIP;
  34.     public JTextField txtPort;
  35.     public JTextArea displayArea;
  36.     private final ButtonGroup buttonGroup = new ButtonGroup();
  37.     private ObjectOutputStream output;
  38.     private ObjectInputStream input;
  39.     private ServerSocket server;
  40.     private Socket connection;
  41.     private int counter = 1;
  42.  
  43.     // private InetAddress ip;
  44.  
  45.     public static void main(String[] args) {
  46.         // EventQueue.invokeLater(new Runnable() {
  47.         // public void run() {
  48.         // try {
  49.         ServerGUI window = new ServerGUI();
  50.         window.frame.setVisible(true);
  51.  
  52.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  53.  
  54.         // } catch (Exception e) {
  55.         // e.printStackTrace();
  56.         // }
  57.         // }
  58.         // });
  59.     }
  60.  
  61.     public ServerGUI() {
  62.         super("Sunucu Arayüzü");
  63.  
  64.         initialize();
  65.  
  66.     }
  67.  
  68.     private void initialize() {
  69.         frame = new JFrame();
  70.         frame.setIconImage(Toolkit
  71.                 .getDefaultToolkit()
  72.                 .getImage(
  73.                         ServerGUI.class
  74.                                 .getResource("/javax/swing/plaf/metal/icons/ocean/computer.gif")));
  75.         frame.setBounds(100, 100, 640, 480);
  76.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  77.         frame.getContentPane().setLayout(null);
  78.  
  79.         JPanel panel1 = new JPanel();
  80.         panel1.setBackground(SystemColor.inactiveCaptionBorder);
  81.         panel1.setBounds(10, 11, 604, 100);
  82.         frame.getContentPane().add(panel1);
  83.         panel1.setLayout(null);
  84.  
  85.         JLabel lblIp = new JLabel("Your IP Address");
  86.         lblIp.setBounds(10, 71, 89, 14);
  87.         panel1.add(lblIp);
  88.  
  89.         JLabel lblPortNum = new JLabel("Port Number");
  90.         lblPortNum.setBounds(10, 38, 89, 14);
  91.         panel1.add(lblPortNum);
  92.  
  93.         txtServerIP = new JTextField();
  94.         txtServerIP.setEnabled(false);
  95.         txtServerIP.setEditable(false);
  96.         txtServerIP.setBounds(111, 68, 200, 20);
  97.         panel1.add(txtServerIP);
  98.         txtServerIP.setColumns(10);
  99.  
  100.         txtPort = new JTextField();
  101.         txtPort.setText("14789");
  102.         txtPort.setToolTipText("14789 is default port");
  103.         txtPort.setBounds(111, 35, 200, 20);
  104.         panel1.add(txtPort);
  105.         txtPort.setColumns(10);
  106.  
  107.         JLabel lblProtocolSelection = new JLabel("Select Protocol");
  108.         lblProtocolSelection.setBounds(356, 11, 90, 14);
  109.         panel1.add(lblProtocolSelection);
  110.  
  111.         JRadioButton rdbtnTcp = new JRadioButton("TCP");
  112.         rdbtnTcp.setBackground(SystemColor.inactiveCaptionBorder);
  113.         buttonGroup.add(rdbtnTcp);
  114.         rdbtnTcp.setSelected(true);
  115.         rdbtnTcp.setBounds(352, 30, 76, 23);
  116.         panel1.add(rdbtnTcp);
  117.  
  118.         JRadioButton rdbtnUdp = new JRadioButton("UDP");
  119.         rdbtnUdp.setBackground(SystemColor.inactiveCaptionBorder);
  120.         buttonGroup.add(rdbtnUdp);
  121.         rdbtnUdp.setBounds(352, 55, 76, 23);
  122.         panel1.add(rdbtnUdp);
  123.  
  124.         JButton btnStartConnection = new JButton("Start Connection");
  125.         btnStartConnection.addActionListener(this);
  126.  
  127.         btnStartConnection.setBounds(458, 10, 136, 78);
  128.         panel1.add(btnStartConnection);
  129.         panel1.setFocusTraversalPolicy(new FocusTraversalOnArray(
  130.                 new Component[] { lblProtocolSelection, txtPort,
  131.                         txtServerIP, rdbtnTcp, rdbtnUdp, btnStartConnection,
  132.                         lblIp, lblPortNum }));
  133.  
  134.         JPanel panel2 = new JPanel();
  135.         panel2.setBounds(10, 122, 604, 309);
  136.         frame.getContentPane().add(panel2);
  137.         panel2.setLayout(null);
  138.  
  139.         displayArea = new JTextArea();
  140.         displayArea.setEditable(false);
  141.         displayArea.setBounds(10, 11, 584, 247);
  142.         panel2.add(displayArea);
  143.  
  144.         enterField = new JTextField();
  145.         enterField.addActionListener(new ActionListener() {
  146.             public void actionPerformed(ActionEvent e) {
  147.                 sendData(e.getActionCommand());
  148.                 enterField.setText("");
  149.             }
  150.         });
  151.         enterField.setBounds(10, 269, 584, 29);
  152.         panel2.add(enterField);
  153.         enterField.setColumns(10);
  154.     }
  155.  
  156.     public void actionPerformed(ActionEvent a) {
  157.         try {
  158.             txtServerIP.setText(getIPAddress());
  159.         } catch (IOException e1) {
  160.  
  161.             e1.printStackTrace();
  162.         }
  163.  
  164.         try {
  165.             runServer();
  166.         } catch (IOException e2) {
  167.  
  168.             e2.printStackTrace();
  169.         }
  170.     }
  171.  
  172.     private void sendData(String message) {
  173.         // send object to client
  174.         try {
  175.             output.writeObject("SERVER > " + message);
  176.             output.flush();
  177.             displayMessage("SERVER > " + message);
  178.         }
  179.  
  180.         // process problems sending object
  181.         catch (IOException ioException) {
  182.             displayArea.append("Error writing object.");
  183.         }
  184.     }
  185.  
  186.     private void displayMessage(final String messageToDisplay) {
  187.         // display message from event-dispatch thread of execution
  188.         SwingUtilities.invokeLater(new Runnable() { // inner class to ensure GUI
  189.                                                     // updates properly
  190.  
  191.                     public void run() // updates displayArea
  192.                     {
  193.                         displayArea.append("\n" + messageToDisplay);
  194.                         displayArea.setCaretPosition(displayArea.getText()
  195.                                 .length());
  196.                     }
  197.  
  198.                 } // end inner class
  199.                 ); // end call to SwingUtilities.invokeLater
  200.     }
  201.  
  202.     public void runServer() throws IOException {
  203.         // set up server to receive connections; process connections
  204.         try {
  205.  
  206.             // Step 1: Create a ServerSocket.
  207.             int portNum = Integer.parseInt(txtPort.getText());
  208.             server = new ServerSocket(portNum, 100);
  209.  
  210.             while (true) {
  211.  
  212.                 try {
  213.                     waitForConnection(); // Step 2: Wait for a connection.
  214.                     getStreams(); // Step 3: Get input & output streams.
  215.                     processConnection(); // Step 4: Process connection.
  216.                 }
  217.  
  218.                 // process EOFException when client closes connection
  219.                 catch (EOFException eofException) {
  220.                     System.err.println("Client terminated the connection.");
  221.                 }
  222.  
  223.                 finally {
  224.                     closeConnection(); // Step 5: Close connection.
  225.                     ++counter;
  226.                 }
  227.  
  228.             } // end while
  229.  
  230.         } // end try
  231.  
  232.         // process problems with I/O
  233.         catch (IOException ioException) {
  234.             ioException.printStackTrace();
  235.         }
  236.  
  237.     } // end method runServer
  238.  
  239.     private void waitForConnection() throws IOException {
  240.         displayMessage("Waiting for client to connect...");
  241.         connection = server.accept(); // allow server to accept connection
  242.         displayMessage("Connection " + counter + " from "
  243.                 + connection.getInetAddress().getHostName() + " ("
  244.                 + connection.getInetAddress().getHostAddress()
  245.                 + ") established.");
  246.     }
  247.  
  248.     // get streams to send and receive data
  249.     private void getStreams() throws IOException {
  250.         // set up output stream for objects
  251.         output = new ObjectOutputStream(connection.getOutputStream());
  252.         output.flush(); // flush output buffer to send header information
  253.  
  254.         // set up input stream for objects
  255.         input = new ObjectInputStream(connection.getInputStream());
  256.  
  257.         displayMessage("I/O stream receieved.");
  258.     }
  259.  
  260.     // process connection with client
  261.     private void processConnection() throws IOException {
  262.         // send connection successful message to client
  263.         String message = "Connected.";
  264.         sendData(message);
  265.  
  266.         // enable enterField so server user can send messages
  267.         setTextFieldEditable(true);
  268.  
  269.         do { // process messages sent from client
  270.  
  271.             // read message and display it
  272.             try {
  273.                 message = (String) input.readObject();
  274.                 displayMessage(message);
  275.             }
  276.  
  277.             // catch problems reading from client
  278.             catch (ClassNotFoundException classNotFoundException) {
  279.                 displayMessage("Unknown object type receieved.");
  280.             }
  281.  
  282.         } while (!message.equals("TEMINATE"));
  283.  
  284.     } // end method processConnection
  285.  
  286.     // close streams and socket
  287.     private void closeConnection() {
  288.         displayMessage("Closing connection...");
  289.         setTextFieldEditable(false); // disable enterField
  290.  
  291.         try {
  292.             output.close();
  293.             input.close();
  294.             connection.close();
  295.             displayMessage("Connection closed.");
  296.         } catch (IOException ioException) {
  297.             ioException.printStackTrace();
  298.         }
  299.     }
  300.  
  301.     private void setTextFieldEditable(final boolean editable) {
  302.         // display message from event-dispatch thread of execution
  303.         SwingUtilities.invokeLater(new Runnable() { // inner class to ensure GUI
  304.                                                     // updates properly
  305.  
  306.                     public void run() // sets enterField's editability
  307.                     {
  308.                         enterField.setEditable(editable);
  309.                     }
  310.  
  311.                 } // end inner class
  312.  
  313.                 ); // end call to SwingUtilities.invokeLater
  314.     }
  315.  
  316.     public String getIPAddress() throws IOException {
  317.  
  318.         return InetAddress.getLocalHost().getHostAddress();
  319.     }
  320.  
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement