Chat
ChatClient.java
import java.net.*;
import java.io.*;
public class ChatClient {
public static void main(String[] args) {
if(args.length != 2){
System.out.println("Usage : java ChatClient <username> <server-ip>");
System.exit(1);
}
Socket sock = null;
BufferedReader br = null;
PrintWriter pw = null;
boolean endflag = false;
try{
sock = new Socket(args[1], 10001);
pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
// send username.
pw.println(args[0]);
pw.flush();
InputThread it = new InputThread(sock, br);
it.start();
String line = null;
System.out.println("Welcome to OSS Chat, '" + args[0] + "'");
System.out.println("(/usage) to know how to use");
while((line = keyboard.readLine()) != null){
pw.println(line);
pw.flush();
if(line.equals("/quit")){
endflag = true;
break;
}
}
System.out.println("Connection closed.");
}catch(Exception ex){
if(!endflag)
System.out.println(ex);
}finally{
try{
if(pw != null)
pw.close();
}catch(Exception ex){}
try{
if(br != null)
br.close();
}catch(Exception ex){}
try{
if(sock != null)
sock.close();
}catch(Exception ex){}
} // finally
} // main
} // class
class InputThread extends Thread{
private Socket sock = null;
private BufferedReader br = null;
public InputThread(Socket sock, BufferedReader br){
this.sock = sock;
this.br = br;
}
public void run(){
try{
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
}catch(Exception ex){
}finally{
try{
if(br != null)
br.close();
}catch(Exception ex){}
try{
if(sock != null)
sock.close();
}catch(Exception ex){}
}
} // InputThread
}
ChatServer.java
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ChatServer {
public static void main(String[] args) {
try{
@SuppressWarnings("resource")
ServerSocket server = new ServerSocket(10001);
System.out.println("Waiting connection...");
HashMap<String, PrintWriter> hm = new HashMap<String, PrintWriter>();
HashSet<String> hs = new HashSet<String>();
while(true){
Socket sock = server.accept();
ChatThread chatthread = new ChatThread(sock, hm, hs);
chatthread.start();
} // while
}catch(Exception e){
System.out.println(e);
}
} // main
}
class ChatThread extends Thread{
private Socket sock;
private String id;
private BufferedReader br;
private HashMap<String, PrintWriter> hm;
//private boolean initFlag = false;
private HashSet<String> spamwords;
SimpleDateFormat time = new SimpleDateFormat("hh:mm:ss");
public ChatThread(Socket sock, HashMap<String, PrintWriter> hm, HashSet<String> hs){
this.spamwords = hs;
this.sock = sock;
this.hm = hm;
try{
PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
id = br.readLine();
broadcast(id + " entered.");
System.out.println("[" + time.format((new Date())) + "]"
+ "[Server] User (" + id + ") entered.");
synchronized(hm){
hm.put(this.id, pw);
}
//initFlag = true;
}catch(Exception ex){
System.out.println(ex);
}
} // Constructor
public void run(){
try{
String line = null;
while((line = br.readLine()) != null){
if(line.equals("/quit"))
break;
if(line.indexOf("/to ") == 0){
sendmsg(line);
}
else if(line.equals("/userlist")){
send_userlist();
}
else if(line.equals("/spamlist")){
send_spamlist(spamwords);
}
else if(line.indexOf("/addspam ") == 0){
add_spam(line);
}
else if(line.equals("/usage")){
usage();
}
else {
broadcast(id + " : " + line);
}
}
}catch(Exception ex){
System.out.println(ex);
}finally{
synchronized(hm){
hm.remove(id);
}
broadcast(id + " exited.");
try{
if(sock != null)
sock.close();
}catch(Exception ex){}
}
} // run
public void sendmsg(String msg){
int start = msg.indexOf(" ") +1;
int end = msg.indexOf(" ", start);
if(end != -1){
String to = msg.substring(start, end);
String msg2 = msg.substring(end+1);
Object obj = hm.get(to);
if(obj != null){
PrintWriter pw = (PrintWriter)obj;
String spamword = check_spamwords(msg, spamwords);
if (null == spamword)
pw.println("[" + time.format((new Date())) + "]"
+ id + " whisphered. : " + msg2);
else {
pw = hm.get(id);
pw.println("[" + time.format((new Date())) + "]"
+ " '" + spamword + "' is a spamword!");
}
pw.flush();
} // if
}
} // sendmsg
public void broadcast(String msg){
synchronized(hm){
Collection<PrintWriter> collection = hm.values();
Iterator<PrintWriter> iter = collection.iterator();
while(iter.hasNext()){
PrintWriter pw = (PrintWriter)iter.next();
if (pw!=hm.get(id)) {
String spamword = check_spamwords(msg, spamwords);
if (null == spamword)
pw.println("[" + time.format((new Date())) + "]" + msg);
else {
pw = hm.get(id);
pw.println("[" + time.format((new Date())) + "]"
+ " '" + spamword + "' is a spamword!");
}
pw.flush();
}
}
}
} // broadcast
public void send_userlist() {
Set<String> set = hm.keySet();
Iterator<String> iterator = set.iterator();
PrintWriter pw = hm.get(id);
int i = 1;
while(iterator.hasNext()){
String username = (String)iterator.next();
pw.println("User#" + i + ": " + username);
pw.flush();
i++;
}
} // send_userlist
public String check_spamwords(String str, HashSet<String> set) {
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String spam = (String)iterator.next();
if (str.contains(spam)) {
System.out.println("[" + time.format((new Date())) + "]" +
"[Server] User (" + id + ") used a spamword '" + spam + "'.");
return spam;
}
}
return null;
}
public void send_spamlist(HashSet<String> set) {
//Set<String> set = hm.keySet();
Iterator<String> iterator = set.iterator();
PrintWriter pw = hm.get(id);
int i = 1;
while (iterator.hasNext()){
String spam = (String)iterator.next();
pw.println(i + "#: " + spam);
pw.flush();
i++;
}
}
public synchronized void add_spam(String word) {
PrintWriter pw = hm.get(id);
int start = word.indexOf(" ") +1;
String spamword = word.substring(start);
spamwords.add(spamword);
pw.println("[" + time.format((new Date())) + "]" + " new spamword '"
+ spamword + "' is added to spamlist");
pw.flush();
}
public void usage() {
PrintWriter pw = hm.get(id);
pw.println("\t[How to use OSS Chat]");
pw.println("Type message to send message to another users\n
(/userlist) to print userlist\n(/spamlist) to print spamlist\n(/addspam) to add spamword");
pw.println("(/[username] msg) to send message to [username]");
pw.flush();
}
}