Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import java.io.*;
- import java.net.*;
- import javax.swing.JPanel;
- import java.awt.SystemColor;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.JLabel;
- import javax.swing.JRadioButton;
- import javax.swing.JButton;
- import javax.swing.SwingUtilities;
- import java.awt.Toolkit;
- import javax.swing.ButtonGroup;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import org.eclipse.wb.swing.FocusTraversalOnArray;
- import java.awt.Component;
- public class ServerGUI extends JFrame implements ActionListener {
- private static final long serialVersionUID = 1L;
- private JFrame frame;
- public JTextField enterField;
- public JTextField txtServerIP;
- public JTextField txtPort;
- public JTextArea displayArea;
- private final ButtonGroup buttonGroup = new ButtonGroup();
- private ObjectOutputStream output;
- private ObjectInputStream input;
- private ServerSocket server;
- private Socket connection;
- private int counter = 1;
- // private InetAddress ip;
- public static void main(String[] args) {
- // EventQueue.invokeLater(new Runnable() {
- // public void run() {
- // try {
- ServerGUI window = new ServerGUI();
- window.frame.setVisible(true);
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- // }
- // });
- }
- public ServerGUI() {
- super("Sunucu Arayüzü");
- initialize();
- }
- private void initialize() {
- frame = new JFrame();
- frame.setIconImage(Toolkit
- .getDefaultToolkit()
- .getImage(
- ServerGUI.class
- .getResource("/javax/swing/plaf/metal/icons/ocean/computer.gif")));
- frame.setBounds(100, 100, 640, 480);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
- JPanel panel1 = new JPanel();
- panel1.setBackground(SystemColor.inactiveCaptionBorder);
- panel1.setBounds(10, 11, 604, 100);
- frame.getContentPane().add(panel1);
- panel1.setLayout(null);
- JLabel lblIp = new JLabel("Your IP Address");
- lblIp.setBounds(10, 71, 89, 14);
- panel1.add(lblIp);
- JLabel lblPortNum = new JLabel("Port Number");
- lblPortNum.setBounds(10, 38, 89, 14);
- panel1.add(lblPortNum);
- txtServerIP = new JTextField();
- txtServerIP.setEnabled(false);
- txtServerIP.setEditable(false);
- txtServerIP.setBounds(111, 68, 200, 20);
- panel1.add(txtServerIP);
- txtServerIP.setColumns(10);
- txtPort = new JTextField();
- txtPort.setText("14789");
- txtPort.setToolTipText("14789 is default port");
- txtPort.setBounds(111, 35, 200, 20);
- panel1.add(txtPort);
- txtPort.setColumns(10);
- JLabel lblProtocolSelection = new JLabel("Select Protocol");
- lblProtocolSelection.setBounds(356, 11, 90, 14);
- panel1.add(lblProtocolSelection);
- JRadioButton rdbtnTcp = new JRadioButton("TCP");
- rdbtnTcp.setBackground(SystemColor.inactiveCaptionBorder);
- buttonGroup.add(rdbtnTcp);
- rdbtnTcp.setSelected(true);
- rdbtnTcp.setBounds(352, 30, 76, 23);
- panel1.add(rdbtnTcp);
- JRadioButton rdbtnUdp = new JRadioButton("UDP");
- rdbtnUdp.setBackground(SystemColor.inactiveCaptionBorder);
- buttonGroup.add(rdbtnUdp);
- rdbtnUdp.setBounds(352, 55, 76, 23);
- panel1.add(rdbtnUdp);
- JButton btnStartConnection = new JButton("Start Connection");
- btnStartConnection.addActionListener(this);
- btnStartConnection.setBounds(458, 10, 136, 78);
- panel1.add(btnStartConnection);
- panel1.setFocusTraversalPolicy(new FocusTraversalOnArray(
- new Component[] { lblProtocolSelection, txtPort,
- txtServerIP, rdbtnTcp, rdbtnUdp, btnStartConnection,
- lblIp, lblPortNum }));
- JPanel panel2 = new JPanel();
- panel2.setBounds(10, 122, 604, 309);
- frame.getContentPane().add(panel2);
- panel2.setLayout(null);
- displayArea = new JTextArea();
- displayArea.setEditable(false);
- displayArea.setBounds(10, 11, 584, 247);
- panel2.add(displayArea);
- enterField = new JTextField();
- enterField.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- sendData(e.getActionCommand());
- enterField.setText("");
- }
- });
- enterField.setBounds(10, 269, 584, 29);
- panel2.add(enterField);
- enterField.setColumns(10);
- }
- public void actionPerformed(ActionEvent a) {
- try {
- txtServerIP.setText(getIPAddress());
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- try {
- runServer();
- } catch (IOException e2) {
- e2.printStackTrace();
- }
- }
- private void sendData(String message) {
- // send object to client
- try {
- output.writeObject("SERVER > " + message);
- output.flush();
- displayMessage("SERVER > " + message);
- }
- // process problems sending object
- catch (IOException ioException) {
- displayArea.append("Error writing object.");
- }
- }
- private void displayMessage(final String messageToDisplay) {
- // display message from event-dispatch thread of execution
- SwingUtilities.invokeLater(new Runnable() { // inner class to ensure GUI
- // updates properly
- public void run() // updates displayArea
- {
- displayArea.append("\n" + messageToDisplay);
- displayArea.setCaretPosition(displayArea.getText()
- .length());
- }
- } // end inner class
- ); // end call to SwingUtilities.invokeLater
- }
- public void runServer() throws IOException {
- // set up server to receive connections; process connections
- try {
- // Step 1: Create a ServerSocket.
- int portNum = Integer.parseInt(txtPort.getText());
- server = new ServerSocket(portNum, 100);
- while (true) {
- try {
- waitForConnection(); // Step 2: Wait for a connection.
- getStreams(); // Step 3: Get input & output streams.
- processConnection(); // Step 4: Process connection.
- }
- // process EOFException when client closes connection
- catch (EOFException eofException) {
- System.err.println("Client terminated the connection.");
- }
- finally {
- closeConnection(); // Step 5: Close connection.
- ++counter;
- }
- } // end while
- } // end try
- // process problems with I/O
- catch (IOException ioException) {
- ioException.printStackTrace();
- }
- } // end method runServer
- private void waitForConnection() throws IOException {
- displayMessage("Waiting for client to connect...");
- connection = server.accept(); // allow server to accept connection
- displayMessage("Connection " + counter + " from "
- + connection.getInetAddress().getHostName() + " ("
- + connection.getInetAddress().getHostAddress()
- + ") established.");
- }
- // get streams to send and receive data
- private void getStreams() throws IOException {
- // set up output stream for objects
- output = new ObjectOutputStream(connection.getOutputStream());
- output.flush(); // flush output buffer to send header information
- // set up input stream for objects
- input = new ObjectInputStream(connection.getInputStream());
- displayMessage("I/O stream receieved.");
- }
- // process connection with client
- private void processConnection() throws IOException {
- // send connection successful message to client
- String message = "Connected.";
- sendData(message);
- // enable enterField so server user can send messages
- setTextFieldEditable(true);
- do { // process messages sent from client
- // read message and display it
- try {
- message = (String) input.readObject();
- displayMessage(message);
- }
- // catch problems reading from client
- catch (ClassNotFoundException classNotFoundException) {
- displayMessage("Unknown object type receieved.");
- }
- } while (!message.equals("TEMINATE"));
- } // end method processConnection
- // close streams and socket
- private void closeConnection() {
- displayMessage("Closing connection...");
- setTextFieldEditable(false); // disable enterField
- try {
- output.close();
- input.close();
- connection.close();
- displayMessage("Connection closed.");
- } catch (IOException ioException) {
- ioException.printStackTrace();
- }
- }
- private void setTextFieldEditable(final boolean editable) {
- // display message from event-dispatch thread of execution
- SwingUtilities.invokeLater(new Runnable() { // inner class to ensure GUI
- // updates properly
- public void run() // sets enterField's editability
- {
- enterField.setEditable(editable);
- }
- } // end inner class
- ); // end call to SwingUtilities.invokeLater
- }
- public String getIPAddress() throws IOException {
- return InetAddress.getLocalHost().getHostAddress();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement