ClientFrame.java


import java.awt.*;
import java.awt.font.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class ClientFrame {
    static boolean is_listening=true;
    static Socket this_socket;
    static  PrintWriter    out;
    static BufferedReader  in;
    static JTextField theMessage;
    public static void main(String args[]) {
        theMessage=new JTextField();
        Font the_font = new Font("SansSerif", Font.BOLD, 20);
        theMessage.setFont(the_font);
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int ChatPort=Integer.parseInt(args[0]);
            InetAddress ip = InetAddress.getByName("localhost");
            try {
                this_socket = new Socket(ip, ChatPort);
                out =new PrintWriter(this_socket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(this_socket.getInputStream()));
            } catch (Exception e1) {
                System.out.println("Port "+ChatPort+ " not Available");
                return;
            }
            System.out.println("The Inet address is "+ip+
                               "\n listening on port "+this_socket.getLocalPort()+
                               "\n sending on port "+this_socket.getPort()+"\n\n");
            JFrame frame = new JFrame("Chat Frame using Port "+ChatPort);
            JButton button = new JButton("Ask Chat Server");
            button.addActionListener((ev)->{send_chat(theMessage);});
            JButton cbutton = new JButton("Close Window");
            cbutton.addActionListener((ev)->{close();});
            Container contentPane = frame.getContentPane();
            contentPane.add(theMessage, BorderLayout.CENTER);
            contentPane.add(button, BorderLayout.SOUTH);
            contentPane.add(cbutton, BorderLayout.NORTH);
            frame.setSize(500, 150);
            frame.setVisible(true);
            //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ;
            frame.addWindowListener(new WindowAdapter() {
                                        public void windowClosing(WindowEvent e) {
                                            close();
                                        }
                                    });          
        } catch (Exception e) {
            e.printStackTrace();
        }
        connect(theMessage);
        new Listener().start();  
    }
    public static void send_chat(JTextField the_message){
        ClientFrame.out.println(the_message.getText());
    }
    public static void close(){
        try {
            ClientFrame.out.println("shut_down123");
            is_listening=false;
            in.close();
            out.close();
            this_socket.close();
            System.out.println("Client Closing");
            System.exit(0);
        } catch (Exception ioe) {
            System.out.println("Socket did not close properly.");
        }
    }
    public static void connect(JTextField the_message){
        ClientFrame.out.println("start_up123");
        String the_response="";
        try {
            the_response=ClientFrame.in.readLine();
            the_message.setText(the_response);
            the_message.repaint();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Listener extends Thread {
    public Listener( ) {}
    @Override
    public void run() {
        String the_response="";
        while (ClientFrame.is_listening) {
            try {
                the_response=ClientFrame.in.readLine();
                ClientFrame.theMessage.setText(the_response);
                ClientFrame.theMessage.repaint();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Exception");
            }
        }

    }
}