|
| 1 | +import java.io.*; |
| 2 | +import java.net.*; |
| 3 | +import java.awt.*; |
| 4 | +import java.awt.event.*; |
| 5 | +import javax.swing.*; |
| 6 | + |
| 7 | +public class Client extends JFrame { |
| 8 | + public JTextArea chatWindow; |
| 9 | + public JTextField userText; |
| 10 | + private ObjectOutputStream output; |
| 11 | + private ObjectInputStream input; |
| 12 | + private String message = ""; |
| 13 | + private String serverIP; |
| 14 | + private Socket connection; |
| 15 | + |
| 16 | + public Client(String host) { |
| 17 | + // in host we r sending the ip address of the server which we want to cinnect to |
| 18 | + super("Client FOMO.."); |
| 19 | + serverIP = host; |
| 20 | + userText = new JTextField(); |
| 21 | + userText.setEditable(false); |
| 22 | + userText.addActionListener(new ActionListener() { |
| 23 | + |
| 24 | + @Override |
| 25 | + public void actionPerformed(ActionEvent e) { |
| 26 | + sendMessage(e.getActionCommand()); |
| 27 | + userText.setText(""); |
| 28 | + |
| 29 | + } |
| 30 | + }); |
| 31 | + add(userText, BorderLayout.NORTH); |
| 32 | + chatWindow = new JTextArea(); |
| 33 | + add(new JScrollPane(chatWindow), BorderLayout.CENTER); |
| 34 | + setSize(300, 150); |
| 35 | + setVisible(true); |
| 36 | + } |
| 37 | + |
| 38 | + // connect to server |
| 39 | + public void startRunning() { |
| 40 | + try { |
| 41 | + connectToServer(); |
| 42 | + setupStreams(); |
| 43 | + whileChatting(); |
| 44 | + } catch (EOFException eofException) { |
| 45 | + showMessage("\n Client terminated connection "); |
| 46 | + } catch (IOException ioexception) { |
| 47 | + ioexception.printStackTrace(); |
| 48 | + } finally { |
| 49 | + closeCrap(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public void connectToServer() throws IOException { |
| 54 | + showMessage("Attempting conncection...\n"); |
| 55 | + connection = new Socket(InetAddress.getByName(serverIP), 6789); |
| 56 | + showMessage("Connected to " + connection.getInetAddress().getHostName()); |
| 57 | + } |
| 58 | + |
| 59 | + public void showMessage(String m) { |
| 60 | + SwingUtilities.invokeLater(new Runnable() { |
| 61 | + |
| 62 | + @Override |
| 63 | + public void run() { |
| 64 | + chatWindow.append(m); |
| 65 | + |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + // setup to send and receive messages |
| 71 | + public void setupStreams() throws IOException { |
| 72 | + output = new ObjectOutputStream(connection.getOutputStream()); |
| 73 | + output.flush(); |
| 74 | + input = new ObjectInputStream(connection.getInputStream()); |
| 75 | + showMessage("\n your streams are good to go...\n"); |
| 76 | + } |
| 77 | + |
| 78 | + public void whileChatting() throws IOException { |
| 79 | + ableToType(true); |
| 80 | + do { |
| 81 | + try { |
| 82 | + message = (String) input.readObject(); |
| 83 | + showMessage("\n " + message); |
| 84 | + } catch (ClassNotFoundException classNotFoundException) { |
| 85 | + showMessage("I don't know the type of object "); |
| 86 | + } |
| 87 | + } while (!message.equals("SERVER - END")); |
| 88 | + } |
| 89 | + |
| 90 | + public void closeCrap() { |
| 91 | + ableToType(false); |
| 92 | + showMessage("Closing everything"); |
| 93 | + try { |
| 94 | + output.close(); |
| 95 | + input.close(); |
| 96 | + connection.close(); |
| 97 | + } catch (IOException ioException) { |
| 98 | + ioException.printStackTrace(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public void sendMessage(String message) { |
| 103 | + try { |
| 104 | + output.writeObject("CLIENT - " + message); |
| 105 | + output.flush(); |
| 106 | + showMessage("\nCLIENT - " + message); |
| 107 | + } catch (IOException ioException) { |
| 108 | + chatWindow.append("\n smthing went wrong "); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public void ableToType(final boolean tof) { |
| 113 | + SwingUtilities.invokeLater(new Runnable() { |
| 114 | + |
| 115 | + @Override |
| 116 | + public void run() { |
| 117 | + userText.setEditable(tof); |
| 118 | + |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments