diff --git a/out/production/Java-SocketProgramming/CLI/CLI.class b/out/production/Java-SocketProgramming/CLI/CLI.class index 93f0c49..565689f 100644 Binary files a/out/production/Java-SocketProgramming/CLI/CLI.class and b/out/production/Java-SocketProgramming/CLI/CLI.class differ diff --git a/out/production/Java-SocketProgramming/Client/Client.class b/out/production/Java-SocketProgramming/Client/Client.class index 555a885..d10ba41 100644 Binary files a/out/production/Java-SocketProgramming/Client/Client.class and b/out/production/Java-SocketProgramming/Client/Client.class differ diff --git a/out/production/Java-SocketProgramming/Client/ReadFromServer.class b/out/production/Java-SocketProgramming/Client/ReadFromServer.class index 028b701..29706f2 100644 Binary files a/out/production/Java-SocketProgramming/Client/ReadFromServer.class and b/out/production/Java-SocketProgramming/Client/ReadFromServer.class differ diff --git a/out/production/Java-SocketProgramming/Client/WriteToServer.class b/out/production/Java-SocketProgramming/Client/WriteToServer.class index 1b4b6fe..529f498 100644 Binary files a/out/production/Java-SocketProgramming/Client/WriteToServer.class and b/out/production/Java-SocketProgramming/Client/WriteToServer.class differ diff --git a/out/production/Java-SocketProgramming/GUI/GUI.class b/out/production/Java-SocketProgramming/GUI/GUI.class new file mode 100644 index 0000000..04f10b3 Binary files /dev/null and b/out/production/Java-SocketProgramming/GUI/GUI.class differ diff --git a/out/production/Java-SocketProgramming/GUI/MyFrame.class b/out/production/Java-SocketProgramming/GUI/MyFrame.class new file mode 100644 index 0000000..1d7ce4a Binary files /dev/null and b/out/production/Java-SocketProgramming/GUI/MyFrame.class differ diff --git a/out/production/Java-SocketProgramming/Main.class b/out/production/Java-SocketProgramming/Main.class index 24a742c..0fa6a36 100644 Binary files a/out/production/Java-SocketProgramming/Main.class and b/out/production/Java-SocketProgramming/Main.class differ diff --git a/out/production/Java-SocketProgramming/Server/ClientHandlerThread.class b/out/production/Java-SocketProgramming/Server/ClientHandlerThread.class index 3b69ecf..07c910a 100644 Binary files a/out/production/Java-SocketProgramming/Server/ClientHandlerThread.class and b/out/production/Java-SocketProgramming/Server/ClientHandlerThread.class differ diff --git a/out/production/Java-SocketProgramming/Server/Server.class b/out/production/Java-SocketProgramming/Server/Server.class index 444015e..a240d24 100644 Binary files a/out/production/Java-SocketProgramming/Server/Server.class and b/out/production/Java-SocketProgramming/Server/Server.class differ diff --git a/out/production/Java-SocketProgramming/Server/ServerInputHandlerThread.class b/out/production/Java-SocketProgramming/Server/ServerInputHandlerThread.class new file mode 100644 index 0000000..a465367 Binary files /dev/null and b/out/production/Java-SocketProgramming/Server/ServerInputHandlerThread.class differ diff --git a/out/production/Java-SocketProgramming/Server/ServerStdinHandlerThread.class b/out/production/Java-SocketProgramming/Server/ServerStdinHandlerThread.class deleted file mode 100644 index 15188b8..0000000 Binary files a/out/production/Java-SocketProgramming/Server/ServerStdinHandlerThread.class and /dev/null differ diff --git a/out/production/Java-SocketProgramming/UI/UI.class b/out/production/Java-SocketProgramming/UI/UI.class deleted file mode 100644 index 6c55065..0000000 Binary files a/out/production/Java-SocketProgramming/UI/UI.class and /dev/null differ diff --git a/src/CLI/CLI.java b/src/CLI/CLI.java index 552c614..799d1aa 100644 --- a/src/CLI/CLI.java +++ b/src/CLI/CLI.java @@ -1,20 +1,29 @@ package CLI; -import UI.UI; - +import java.util.Locale; import java.util.NoSuchElementException; import java.util.Scanner; -public class CLI extends UI { - private final Scanner scanner = new Scanner(System.in); +public class CLI { + private final static Scanner scanner = new Scanner(System.in); public CLI(String name) { super(); - System.out.println(name + " GUI Initiated"); + System.out.println(name + " CLI Initiated"); + } + + public static int selectClientOrServer() { + System.out.println("Run SERVER or CLIENT ?"); + String output = scanner.nextLine(); + int option = -1; + switch (output.toLowerCase(Locale.ROOT)) { + case "server", "s", "1" -> option = 0; + case "client", "c", "2" -> option = 1; + } + return option; } - @Override public String read (String msg) throws NoSuchElementException { if (!msg.isEmpty()) { System.out.println(msg); @@ -22,7 +31,16 @@ public String read (String msg) throws NoSuchElementException { return scanner.nextLine(); } - @Override + public String read() throws NoSuchElementException { + return scanner.nextLine(); + } + + public String read(String msg, String defaultValue) throws NoSuchElementException { + String in = this.read(msg + " (default="+ defaultValue +") :"); + if (!in.isEmpty()) return in; + return defaultValue; + } + public void write (String msg) { System.out.println(msg); } diff --git a/src/Client/Client.java b/src/Client/Client.java index 7b2147d..369a26d 100644 --- a/src/Client/Client.java +++ b/src/Client/Client.java @@ -1,7 +1,7 @@ package Client; import CLI.CLI; -import UI.UI; +import GUI.GUI; import java.io.DataInputStream; import java.io.DataOutputStream; @@ -13,27 +13,23 @@ public class Client { public static Socket socket = null; - private String ipAddress = "localhost"; - private String portNumber = "3000"; + String ipAddress; + String portNumber; public static DataInputStream inputStream; public static DataOutputStream outputStream; - public static UI ui; - public static String name = "newUser"; + public static GUI ui; + public static String name; public Client () { - ui = new CLI("Client"); + ui = new GUI("Client"); this.init(); this.startCommunication(); } private void init() { - String inIP = ui.read("Input IP Address of Server (default=localhost) :"); - String inPort = ui.read("Input Port Number of Server (default=3000) :"); - String inName = ui.read("Enter your name :"); - - if (!inIP.isEmpty()) ipAddress = inIP; - if (!inPort.isEmpty()) portNumber = inPort; - if (!inName.isEmpty()) name = inName; + ipAddress = ui.read("Input IP Address of Server", "localhost"); + portNumber = ui.read("Input Port Number of Server", "3000"); + name = ui.read("Enter your name :"); try { socket = new Socket(ipAddress, Integer.parseInt(portNumber)); @@ -43,10 +39,12 @@ private void init() { // send client name and some other imp details if required outputStream.writeUTF(name); + ui.write("Client Connected with Server on "+ipAddress+":"+portNumber); + } catch (IOException e) { Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, e); + ui.write(e.getMessage()); } - ui.write("Client Connected with Server on "+ipAddress+":"+portNumber); } private void startCommunication() { diff --git a/src/Client/WriteToServer.java b/src/Client/WriteToServer.java index c62d1e6..fee4509 100644 --- a/src/Client/WriteToServer.java +++ b/src/Client/WriteToServer.java @@ -2,6 +2,7 @@ import Server.Server; +import java.awt.event.ActionEvent; import java.io.IOException; import java.util.NoSuchElementException; import java.util.logging.Level; @@ -11,18 +12,32 @@ public record WriteToServer() implements Runnable { @Override public void run() { - String string; + + Client.ui.setSendBtnActionListener(this::sendButtonActionPerformed); + +// String string; +// try { +// while (true) { +// string = Client.ui.read(); +// if (!string.isEmpty()) { +// Client.outputStream.writeUTF(Client.name + " -> " + string.trim()); +// } +// } +// } catch (IOException e) { +// Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, e); +// } catch (NoSuchElementException e) { +// Logger.getLogger(Client.class.getName()).log(Level.INFO, "Server Stdin Closed !"); +// } + } + + private void sendButtonActionPerformed(ActionEvent evt) { try { - while (true) { - string = Client.ui.read(""); - if (!string.isEmpty()) { - Client.outputStream.writeUTF(Client.name + " -> " + string.trim()); - } + String str = Client.ui.getInputTextAndClear(); + if (!str.trim().isEmpty()) { + Client.outputStream.writeUTF(Client.name + " -> " + str.trim()); } } catch (IOException e) { Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, e); - } catch (NoSuchElementException e) { - Logger.getLogger(Client.class.getName()).log(Level.INFO, "Server Stdin Closed !"); } } } diff --git a/src/GUI/GUI.java b/src/GUI/GUI.java new file mode 100644 index 0000000..a1fe6e9 --- /dev/null +++ b/src/GUI/GUI.java @@ -0,0 +1,55 @@ +package GUI; + +import javax.swing.*; +import java.awt.event.ActionListener; +import java.lang.reflect.Method; +import java.util.NoSuchElementException; + +public class GUI { + + String name; + MyFrame frame; + + public String getInputTextAndClear() { + String str = frame.sendArea.getText(); + frame.sendArea.setText(""); + return str; + } + + public void setSendBtnActionListener(ActionListener listener) { + frame.sendButton.addActionListener(listener); + } + + public GUI (String name) { + this.name = name; + frame = new MyFrame(name); + } + + public static int selectClientOrServer() { + String[] options = {"Server", "Client"}; + return JOptionPane.showOptionDialog(null, "Run Client or Server ?", "Java Socket Programming", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, 0); + } + + public String read(String msg) throws NoSuchElementException { + if (msg.isEmpty()) return this.read(); + String option = ""; + while (option.isEmpty()) { + option = JOptionPane.showInputDialog(null, msg); + } + return option; + } + + public String read() throws NoSuchElementException { + return null; + } + + public String read(String msg, String defaultValue) throws NoSuchElementException { + String a = JOptionPane.showInputDialog(null, msg, defaultValue); + if (a == null) System.exit(0); + return a; + } + + public void write(String msg) { + frame.convoArea.append(msg+"\n"); + } +} diff --git a/src/GUI/MyFrame.java b/src/GUI/MyFrame.java new file mode 100644 index 0000000..193f15f --- /dev/null +++ b/src/GUI/MyFrame.java @@ -0,0 +1,114 @@ +package GUI; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; + +public class MyFrame extends JFrame { + + String title; + + public MyFrame(String title) { + this.title = title; + setVisible(true); + initComponents(); + } + + private void initComponents() { + + jScrollPane1 = new JScrollPane(); + convoArea = new JTextArea(); + jScrollPane2 = new JScrollPane(); + sendArea = new JTextArea(); + sendButton = new JButton(); + + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setTitle(title); + setResizable(false); + + jScrollPane1.setAutoscrolls(true); + + convoArea.setEditable(false); + convoArea.setColumns(20); + convoArea.setFont(new java.awt.Font("Calibri", Font.PLAIN, 18)); // NOI18N + convoArea.setLineWrap(true); + convoArea.setRows(5); + convoArea.setWrapStyleWord(true); + jScrollPane1.setViewportView(convoArea); + + sendArea.setColumns(20); + sendArea.setFont(new java.awt.Font("Calibri Light", Font.PLAIN, 18)); // NOI18N + sendArea.setLineWrap(true); + sendArea.setRows(3); + sendArea.setText("Type your message here.."); + sendArea.setWrapStyleWord(true); + sendArea.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); + jScrollPane2.setViewportView(sendArea); + sendArea.setText(""); + + sendButton.setText("Send"); + sendButton.addActionListener(this::sendButtonActionPerformed); + + GroupLayout layout = new GroupLayout(getContentPane()); + layout.setHorizontalGroup( + layout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE) + .addContainerGap()) + .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addComponent(jScrollPane2, GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE) + .addGap(28, 28, 28) + .addComponent(sendButton, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE) + .addGap(26, 26, 26)))) + ); + layout.setVerticalGroup( + layout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 262, GroupLayout.PREFERRED_SIZE) + .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(18, 18, 18) + .addComponent(jScrollPane2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) + .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(sendButton, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE) + .addGap(36, 36, 36)))) + ); + + getContentPane().setLayout(layout); + pack(); + } + + private void sendButtonActionPerformed(ActionEvent evt) { + + String str = sendArea.getText(); + +// if(!str.trim().equals("")) +// { +// try { +// Client.OUT.writeUTF(str.trim()); +// } catch (IOException ex) { +// Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, +// null, ex); +// } +// } + + sendArea.setText(""); + + } + + public JTextArea convoArea; + JScrollPane jScrollPane1; + JScrollPane jScrollPane2; + JTextArea sendArea; + JButton sendButton; + + public static void main(String[] args) { + MyFrame gui = new MyFrame("Server"); + } +} diff --git a/src/Main.java b/src/Main.java index 1f0e45f..c589e61 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,16 +1,14 @@ import Client.Client; import CLI.CLI; +import GUI.GUI; import Server.Server; -import java.util.Locale; public class Main { public static void main(String[] args) { - CLI CLI = new CLI("Main"); - String output = CLI.read("Run SERVER or CLIENT ?"); - switch (output.toLowerCase(Locale.ROOT)) { - case "server", "s", "1" -> new Server(); - case "client", "c", "2" -> new Client(); - } + int option = GUI.selectClientOrServer(); + if (option == 0) new Server(); + else if (option == 1) new Client(); + else System.out.println("Good Bye"); } } diff --git a/src/Server/Server.java b/src/Server/Server.java index caa6f02..24bc099 100644 --- a/src/Server/Server.java +++ b/src/Server/Server.java @@ -9,14 +9,14 @@ import java.util.logging.Level; import java.util.logging.Logger; import CLI.CLI; -import UI.UI; +import GUI.GUI; public class Server { Socket socket = null; - String portNumber = "3000"; + String portNumber; private boolean running = false; - public static UI ui; + public static GUI ui; // for multi-client maintain array for outputStream of each client private static final List allConnectedClients = new ArrayList<>(); @@ -35,10 +35,9 @@ synchronized public static void broadcastMessage (String msg) { synchronized public static int getTotalActiveClients () { return allConnectedClients.size(); } public Server() { - ui = new CLI("Server"); + ui = new GUI("Server"); - String in = ui.read("Enter the Port Number where server should run (default=3000) :"); - if (!in.isEmpty()) portNumber = in; + portNumber = ui.read("Enter the Port Number where server should run", "3000"); this.running = true; this.startAcceptingConnections(); @@ -51,9 +50,9 @@ private void startAcceptingConnections () { ui.write("Server listening on port: " + port); // to listen for server's stdin input and broadcast msg to all clients - Thread serverStdinHandlerThread = new Thread(new ServerStdinHandlerThread()); - serverStdinHandlerThread.setName("server-stdin-listener-thread"); - serverStdinHandlerThread.start(); + Thread serverInputHandlerThread = new Thread(new ServerInputHandlerThread()); + serverInputHandlerThread.setName("server-stdin-listener-thread"); + serverInputHandlerThread.start(); while (this.running) { diff --git a/src/Server/ServerInputHandlerThread.java b/src/Server/ServerInputHandlerThread.java new file mode 100644 index 0000000..a39f2eb --- /dev/null +++ b/src/Server/ServerInputHandlerThread.java @@ -0,0 +1,34 @@ +package Server; + +import java.awt.event.ActionEvent; +import java.util.NoSuchElementException; +import java.util.logging.Level; +import java.util.logging.Logger; + +public record ServerInputHandlerThread() implements Runnable { + + @Override + public void run() { + + Server.ui.setSendBtnActionListener(this::sendButtonActionPerformed); + +// try { +// String string; +// while (true) { +// string = Server.ui.read(); +// if (!string.isEmpty()) { +// Server.broadcastMessage("Server -> " + string.trim()); +// } +// } +// } catch (NoSuchElementException e) { +// Logger.getLogger(Server.class.getName()).log(Level.INFO, "Server Stdin Closed !"); +// } + } + + private void sendButtonActionPerformed(ActionEvent evt) { + String str = Server.ui.getInputTextAndClear(); + if (!str.trim().isEmpty()) { + Server.broadcastMessage("Server -> " + str.trim()); + } + } +} diff --git a/src/Server/ServerStdinHandlerThread.java b/src/Server/ServerStdinHandlerThread.java deleted file mode 100644 index 4fc0a4f..0000000 --- a/src/Server/ServerStdinHandlerThread.java +++ /dev/null @@ -1,23 +0,0 @@ -package Server; - -import java.util.NoSuchElementException; -import java.util.logging.Level; -import java.util.logging.Logger; - -public record ServerStdinHandlerThread() implements Runnable { - - @Override - public void run() { - try { - String string; - while (true) { - string = Server.ui.read(""); - if (!string.isEmpty()) { - Server.broadcastMessage("Server -> " + string.trim()); - } - } - } catch (NoSuchElementException e) { - Logger.getLogger(Server.class.getName()).log(Level.INFO, "Server Stdin Closed !"); - } - } -} diff --git a/src/UI/UI.java b/src/UI/UI.java deleted file mode 100644 index d34bce4..0000000 --- a/src/UI/UI.java +++ /dev/null @@ -1,8 +0,0 @@ -package UI; - -import java.util.NoSuchElementException; - -public abstract class UI { - public abstract String read (String msg) throws NoSuchElementException; - public abstract void write (String msg); -}