I changed your code to my liking, see:
For point 1:
public void run() { String line; try { while(true) { line = input.readLine(); if("EXIT".equals(line)) { output.println("Closing Connection . . . Goodbye"); clients.remove(this); users.remove(name); break; } else if(name.equals(line)) { output.println("OK"); } else {
For item 2:
It seems to me that you are typing on the console to get input, in this case, at least at a time you need to type what you have to send to the other side, then you can add your modification to this, as shown in the first method line, broadcast (); so that you can satisfy your need for point 2 or ( else. You can let the server side send it back to the client, a message containing the name, it will send the message to the server as the client, and the server will send all customers back. )
public void boradcast(String user, String message) { System.out.println(user + " : " + message);
And for point 3, you can write your conversation to a file or save it in a database that is always suitable for you.
And since you said you were new to Java, you'd better look at java.nio . This package is better than the previous java.io package.
LAST CHANGE:
Try this small code example, hope this can do what you ask for:
import java.io.*; public class StringConsole { public static void main(String... args) throws IOException { int count = 0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (count < 5) { System.out.print("My Name : "); String message = br.readLine(); count++; } } }
Hope this can help you in some way.
Hi
source share