Question
assume that client and server as two different users and assumes the client will always send the first message and that the conversations will alternate
assume that client and server as two different users and assumes the client will always send the first message and that the conversations will alternate back and forth between the client and the server.
In real chat programs, either user could send the first message and users might send multiple messages in a row. Write text chat program that allows either user to send the first message and allows users to send messages in any order (i.e., they don't have to alternate back and forth). Hint: You'll likely need to use multithreading. Also, since this should be a console application, not JavaFX, do not worry if user input on one side is interrupted by an incoming message from the other side. import java.io.*; import java.net.*; import java.util.*; import javafx.application.Application; import javafx.application.Platform; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.control.TextArea; import javafx.stage.Stage; public class ChatServer { public static void main(String[] args) { try { // Create Scanner for user input Scanner s = new Scanner(System.in); // Create server socket ServerSocket serverSocket = new ServerSocket(8000); // Listen for connection request Socket socket = serverSocket.accept(); // Create data input and output streams ObjectInputStream inputFromClient = new ObjectInputStream( socket.getInputStream()); ObjectOutputStream outputToClient = new ObjectOutputStream( socket.getOutputStream()); while (true) { // Read message from the client String clientMsg = (String) inputFromClient.readObject(); // Write Msg to console System.out.println(clientMsg); // Get the reply from the user String replyMsg = s.nextLine(); // Send the reply to the client outputToClient.writeObject(replyMsg); } } catch(ClassNotFoundException ex) { System.err.println(ex); } catch(IOException ex) { System.err.println(ex); } } } import java.io.*; import java.net.*; import java.util.*; public class ChatClient { // IO streams private static ObjectOutputStream toServer = null; private static ObjectInputStream fromServer = null; public static void main(String[] args) { if (args.length != 1) { System.out.println("Usage: java ChatClient ServerIPAddress"); return; } try { // Create Scanner for user input Scanner s = new Scanner(System.in); // Create a socket to connect to the server Socket socket = new Socket(args[0], 8000); // Create an output stream to send data to the server toServer = new ObjectOutputStream(socket.getOutputStream()); // Create an input stream to receive data from the server fromServer = new ObjectInputStream(socket.getInputStream()); while(true) { // Get the message from the user String theMsg = s.nextLine(); // Send the message to the server toServer.writeObject(theMsg); // Read reply from the server String replyMsg = (String) fromServer.readObject(); // Write reply to console System.out.println(replyMsg); } } catch(ClassNotFoundException ex) { System.err.println(ex); } catch (IOException ex) { System.err.println(ex); }
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started