Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import java.awt.SystemColor;
- import javax.swing.ButtonGroup;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.JLabel;
- import javax.swing.JRadioButton;
- import javax.swing.SwingUtilities;
- import java.awt.Toolkit;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.io.EOFException;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- public class ClientGUI extends JFrame {
- private static final long serialVersionUID = 1L;
- private JFrame frame;
- private JTextField enterField;
- private JTextField txtServerIP;
- private JTextField txtServerPort;
- private JTextArea displayArea;
- public String clientNick = "";
- private ObjectOutputStream output;
- private ObjectInputStream input;
- private String message = "";
- private String chatServer;
- private Socket client;
- private final ButtonGroup buttonGroup = new ButtonGroup();
- /**
- * Launch the application.
- */
- public ClientGUI(String host) {
- super("Client Interface");
- chatServer = host;
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frame = new JFrame();
- frame.setIconImage(Toolkit
- .getDefaultToolkit()
- .getImage(
- ClientGUI.class
- .getResource("/com/sun/java/swing/plaf/windows/icons/Computer.gif")));
- frame.setBounds(100, 100, 640, 589);
- 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 lblServerIP = new JLabel("Server IP");
- lblServerIP.setBounds(10, 39, 60, 14);
- panel1.add(lblServerIP);
- txtServerIP = new JTextField();
- txtServerIP.setText("127.0.0.1");
- txtServerIP.setBounds(73, 36, 160, 20);
- panel1.add(txtServerIP);
- txtServerIP.setColumns(10);
- JLabel lblServerPort = new JLabel("Server Port");
- lblServerPort.setBounds(10, 64, 60, 14);
- panel1.add(lblServerPort);
- txtServerPort = new JTextField();
- txtServerPort.setText("14789");
- txtServerPort.setBounds(73, 61, 160, 20);
- panel1.add(txtServerPort);
- txtServerPort.setColumns(10);
- JRadioButton rdbtnTcp = new JRadioButton("TCP");
- rdbtnTcp.setSelected(true);
- rdbtnTcp.setBackground(SystemColor.inactiveCaptionBorder);
- buttonGroup.add(rdbtnTcp);
- rdbtnTcp.setBounds(260, 30, 60, 23);
- panel1.add(rdbtnTcp);
- JRadioButton rdbtnUdp = new JRadioButton("UDP");
- rdbtnUdp.setBackground(SystemColor.inactiveCaptionBorder);
- buttonGroup.add(rdbtnUdp);
- rdbtnUdp.setBounds(260, 55, 53, 23);
- panel1.add(rdbtnUdp);
- JLabel lblProtocol = new JLabel("Protocol");
- lblProtocol.setBounds(260, 14, 46, 14);
- panel1.add(lblProtocol);
- JButton btnConnect = new JButton("Connect to Server");
- btnConnect.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- }
- });
- btnConnect.setBounds(326, 11, 109, 30);
- panel1.add(btnConnect);
- JPanel panel2 = new JPanel();
- panel2.setBackground(SystemColor.info);
- panel2.setBounds(10, 122, 604, 418);
- frame.getContentPane().add(panel2);
- panel2.setLayout(null);
- enterField = new JTextField();
- enterField.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- sendData(e.getActionCommand());
- enterField.setText("");
- }
- });
- enterField.setBounds(10, 372, 584, 35);
- panel2.add(enterField);
- enterField.setColumns(10);
- JTextArea displayArea = new JTextArea();
- displayArea.setEditable(false);
- displayArea.setBounds(10, 11, 584, 350);
- panel2.add(displayArea);
- }
- // public static String istemciNick = "CLIENT";
- private void connectToServer() throws IOException {
- displayMessage("Connecting...");
- client = new Socket(txtServerIP.getText(),
- Integer.parseInt(txtServerPort.getText()));
- // display connection information
- displayMessage("Connected to "
- + client.getInetAddress().getHostName());
- }
- // get streams to send and receive data
- private void getStreams() throws IOException {
- // set up output stream for objects
- output = new ObjectOutputStream(client.getOutputStream());
- output.flush(); // flush output buffer to send header information
- // set up input stream for objects
- input = new ObjectInputStream(client.getInputStream());
- displayMessage("Got I/O stream.");
- }
- // process connection with server
- private void processConnection() throws IOException {
- // enable enterField so client user can send messages
- setTextFieldEditable(true);
- do { // process messages sent from server
- // read message and display it
- try {
- message = (String) input.readObject();
- displayMessage(message);
- }
- // catch problems reading from server
- catch (ClassNotFoundException classNotFoundException) {
- displayMessage("Unknown object type receieved.");
- }
- } while (!message.contains("TERMINATE"));
- } // end method processConnection
- // close streams and socket
- private void closeConnection() {
- displayMessage("Closing connection...");
- setTextFieldEditable(false); // disable enterField
- try {
- output.close();
- input.close();
- client.close();
- displayMessage("Connection closed...");
- } catch (IOException ioException) {
- ioException.printStackTrace();
- }
- }
- // send message to server
- private void sendData(String message) {
- // send object to server
- try {
- output.writeObject("Client > " + message);
- output.flush();
- displayMessage("Client > " + message);
- }
- // process problems sending object
- catch (IOException ioException) {
- displayArea.append("Error writing object.");
- }
- }
- // utility method called from other threads to manipulate
- // displayArea in the event-dispatch thread
- private void displayMessage(final String messageToDisplay) {
- // display message from GUI 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
- }
- // utility method called from other threads to manipulate
- // enterField in the event-dispatch thread
- private void setTextFieldEditable(final boolean editable) {
- // display message from GUI 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
- }
- // connect to server and process messages from server
- private void runClient() {
- // connect to server, get streams, process connection
- try {
- connectToServer(); // Step 1: Create a Socket to make connection
- getStreams(); // Step 2: Get the input and output streams
- processConnection(); // Step 3: Process connection
- }
- // server closed connection
- catch (EOFException eofException) {
- System.err.println("Server terminated connection.");
- }
- // process problems communicating with server
- catch (IOException ioException) {
- ioException.printStackTrace();
- } finally {
- closeConnection(); // Step 4: Close connection
- }
- } // end method runClient
- public static void main(String[] args) {
- ClientGUI application;
- if (args.length == 0)
- application = new ClientGUI("127.0.0.1");
- else
- application = new ClientGUI(args[0]);
- application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- application.runClient();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement