//importsimport java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Client extends JFrame{ JTextField userText; //user Text field where user can type his msg JTextArea chatWindow; //chat area in-which user can see old MSGs ObjectOutputStream output; //output stream ObjectInputStream input; //input stream String msg = “”; String serverIP; Socket connection; //socket for connection public Client(String host){ super(“Msg-(Client)”); // window name serverIP = host; userText = new JTextField(); userText.setEditable(false); //user can’t type any MSG before he is connected userText.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { sendMSG(e.getActionCommand()); userText.setText(“”); } } //for sending the MSG ); add(userText,BorderLayout.NORTH); //add the text field chatWindow = new JTextArea(); add(new JScrollPane(chatWindow),BorderLayout.CENTER); //and the chat area setSize(300,700); //set Size of window setVisible(true); //to set the window visible } public void StartRunning(){ try { connect2server(); //connect to server setupStreams(); // setup all streams whileChatting(); // write msgs , show them End the connection if user type END } catch (EOFException e) { showMessage(”
Client terminated connection”); // if something goes wrong } catch (IOException e) { e.printStackTrace(); }finally{ closeConn(); } } //connect to server void connect2server() throws IOException{ showMessage(“Attemping connection….
“); //connect to server connection = new Socket(InetAddress.getByName(serverIP),6789); // to the same port num showMessage(“Connected to: “+ connection.getInetAddress().getHostName()); } void setupStreams() throws IOException{ output = new ObjectOutputStream(connection.getOutputStream()); // setup output streams output.flush(); input = new ObjectInputStream(connection.getInputStream()); // setup input streams showMessage(”
streams are now good to go!
“); } void whileChatting() throws IOException{ able2Type(true); do { //same like server program try { msg = (String) input.readObject(); showMessage(”
“+ msg); //user can’t type if he is not connected } catch (ClassNotFoundException e) { showMessage(”
ERROR: can’t recognize that obj !”); } } while (!msg.equals(“SERVER – END”)); // if user type END … end the conn } void closeConn(){ showMessage(”
Closing Connection …”); able2Type(false); try { output.close(); input.close(); //close input & output streams connection.close(); //close conn } catch (IOException e) { e.printStackTrace(); } } void sendMSG(String MSG){ try { output.writeObject(“CLIENT – “+MSG); // write MSG output.flush(); showMessage(”
CLIENT – “+MSG); //and show it } catch (IOException e) { chatWindow.append(”
something messed up try again!”); } } void showMessage (final String m){ SwingUtilities.invokeLater( new Runnable() { @Override public void run() { chatWindow.append(m); //append to chat area } } ); } void able2Type(boolean tof) { SwingUtilities.invokeLater( new Runnable() { @Override public void run() { userText.setEditable(tof); //able to type } } ); } }