I have assigned to create a multichat application that require to public message to all client, private message to certain client and show the user list, anyone can tell me which part should i do to implement private msg and online user list (where will show the name after client access the server, and remove the name when client close)
This is Server
public class MyChatServer {
private static final int PORT = 3367;
private static HashSet<String> names = new HashSet<String>();
/**
* The set of all the print writers for all the clients. This set is kept so
* we can easily broadcast messages.
*/
private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
/**
* The application main method, which just listens on a port and spawns
* handler threads.
*/
public static void main(String[] args) throws Exception {
System.out.println("MyChat server is running.");
ServerSocket listener = new ServerSocket(PORT);
try {
while (true) {
new Handler(listener.accept()).start();
}
} finally {
listener.close();
}
}
private static class Handler extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private String name;
/**
* Constructs a handler thread, squirreling away the socket. All the
* interesting work is done in the run method.
*/
public Handler(Socket socket) {
this.socket = socket;
}
/**
* Services this thread's client by repeatedly requesting a screen name
* until a unique one has been submitted, then acknowledges the name and
* registers the output stream for the client in a global set, then
* repeatedly gets inputs and broadcasts them.
*/
public void run() {
try {
// Create character streams for the socket.
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
//it reads one line from the buffer which is sent by client -- here name of file
while (true) {
out.println("SUBMITNAME");
name = in.readLine();
if (name == null) {
return;
}
synchronized (names) {
if (!names.contains(name)) {
names.add(name);
break;
}
}
}
out.println("NAMEACCEPTED");
writers.add(out);
while (true) {
String input = in.readLine();
if (input == null) {
return;
}
for (PrintWriter writer : writers) {
writer.println("MESSAGE " + name + ": " + input);
}
}
} catch (IOException e) {
System.out.println(e);
} finally {
if (name != null) {
names.remove(name);
}
if (out != null) {
writers.remove(out);
}
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
This is client
private String getIP() {
return JOptionPane.showInputDialog(
frame,
"Please Insert IP Address",
"MyChat Server Connection",
JOptionPane.PLAIN_MESSAGE);
public MyChatClientPage() {
initComponents();
connect();
ChatPanel.setVisible(false);
IPPanel.setVisible(false);
EmailPanel.setVisible(false);
PanelMenu.setVisible(false);
FilePanel.setVisible(false);
btlogout.setVisible(false);
LoginPanel.setVisible(false);
this.pack();
this.setLocationRelativeTo(null);
txtmsg.addActionListener(new ActionListener() {
/**
* Responds to pressing the enter key in the textfield by sending
* the contents of the text field to the server. Then clear the text
* area in preparation for the next message.
*/
@Override
public void actionPerformed(ActionEvent e) {
out.println(txtmsg.getText());
txtmsg.setText("");
}
});
}
private void run() throws IOException {
// Make connection and initialize streams
try {
String serveraddress = getIP();
lbltempip.setText(serveraddress);
socket = new Socket(serveraddress, 3587);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
JOptionPane.showMessageDialog(null, "Connection Success!");
LoginPanel.setVisible(true);
this.setLocationRelativeTo(null);
this.pack();
} catch (UnknownHostException e) {
JOptionPane.showMessageDialog(null, "Unknown Host! System terminated...");
System.exit(0);
}
// Process all messages from server, according to the protocol.
while (true) {
String line = in.readLine();
if (line.startsWith("SUBMITNAME")) {
// out.println(PassName);
} else if (line.startsWith("NAMEACCEPTED")) {
txtmsg.setEditable(true);
} else if (line.startsWith("MESSAGE")) {
txtmsgarea.append(line.substring(8) + "\n");
}
}
}
public static void main(String args[]) throws Exception {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
System.err.println("Look and feel not set.");
}
MyChatClientPage clientpage = new MyChatClientPage();
clientpage.setVisible(true);
clientpage.setLocationRelativeTo(null);
clientpage.run();
clientpage.pack();
}