public class MyChatThread extends Thread{
private Socket sock;
private static HashMap <String, MyChatThread> pool = new HashMap<String, MyChatThread>();
private static HashMap <String, MyChatThread> pool2 = new HashMap<String , MyChatThread>();
//other things here...
public void run(){
try{
dos.writeBytes("Enter name: ");
String name = dis.readLine().trim();
System.out.println("The client named "+name+" has connected.");
pool.put(name, this);
String rcpt = "";
String temp = "";
String command = ""; //gets user command
dos.writeBytes("Write a command. (/shout to make a shoutout, /msg to PM, and /bye to exit from server.)\r\n");
command = dis.readLine(); //reads the command of the user.
do{
if(command.equals("/shout")){ //if user command is /shout
dos.writeBytes("Your message: ");
temp = dis.readLine();
System.out.println("The server has recieved this command: "+ command + " from: " + name);
System.out.println("The server has recieved this messages: "+ temp + " from: " + name);
for(MyChatThread v: pool.values()){
v.ShoutSendMessage(name, temp);
}
dos.writeBytes("Write another command: ");
command = dis.readLine(); //reads another command
}
//i tried doing this.
if(command.equals("/msg")){ //if user command is /msg
System.out.println("The server has recieved this command: "+ command + " from: " + name);
System.out.println("The server has recieved this messages: "+ temp + " from: " + name);
dos.writeBytes("Please input the name of the user you want to PM: ");
rcpt = dis.readLine().trim();
pool2.put(rcpt, this);
do{
if(pool.put(name, this) == pool2.put(rcpt, this)){
dos.writeBytes("Forever Alone? Don't PM yourself. xD \r\n");
dos.writeBytes("Try again. Please input the name of the user you want to PM: ");
rcpt = dis.readLine().trim();
}
}while(pool.put(rcpt, this) == pool2.put(name, this));
System.out.println("User " + name + " is sending a message to " + rcpt + ".");
dos.writeBytes("Please write your message: \r\n");
temp = dis.readLine();
for(MyChatThread v: pool.values()){
v.PrivateSendMessage(name, temp, rcpt);
}
dos.writeBytes("Write another command: ");
command = dis.readLine(); //reads another command
}