Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions ChatClient.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
import java.net.*;
import java.io.*;
import java.util.Scanner;

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);
}

//#1
Scanner input = new Scanner(System.in);
System.out.print("your name >> ");
String name = input.next();
System.out.print("server ip >> ");
String ip = input.next();

Socket sock = null;
BufferedReader br = null;
PrintWriter pw = null;
boolean endflag = false;
try{
sock = new Socket(args[1], 10001);
sock = new Socket(ip, 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.println(name);
pw.flush();
InputThread it = new InputThread(sock, br);
it.start();
String line = null;
while((line = keyboard.readLine()) != null){
pw.println(line);
pw.flush();
if(line.equals("/userlist"))
send_userlist();
if(line.equals("/quit")){
endflag = true;
break;
Expand All @@ -50,6 +57,11 @@ public static void main(String[] args) {
}catch(Exception ex){}
} // finally
} // main

public void send_userlist(){

}

} // class

class InputThread extends Thread{
Expand Down
55 changes: 37 additions & 18 deletions ChatServer.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import java.net.*;
import java.text.SimpleDateFormat;
import java.io.*;
import java.util.*;

public class ChatServer {

public static void main(String[] args) {
try{
ServerSocket server = new ServerSocket(10001);
System.out.println("Waiting connection...");
HashMap hm = new HashMap();
ServerSocket server = new ServerSocket(10001); //port번호 10001로 새로운 ServerSocket을 만든다
System.out.println("Waiting connection..."); // Waiting connection...이라는 메세지를 터미널에 출력
HashMap hm = new HashMap(); //새 해쉬맵을 만듦
while(true){
Socket sock = server.accept();
ChatThread chatthread = new ChatThread(sock, hm);
chatthread.start();
Socket sock = server.accept(); // sock라는 소켓은 server라는 서버소켓의 요청을 받아들인다.
ChatThread chatthread = new ChatThread(sock, hm); // 새로운 ChatThread Object를 만든다.
chatthread.start();
} // while
}catch(Exception e){
System.out.println(e);
}
} // main

}

class ChatThread extends Thread{
Expand All @@ -26,15 +28,17 @@ class ChatThread extends Thread{
private BufferedReader br;
private HashMap hm;
private boolean initFlag = false;


public ChatThread(Socket sock, HashMap hm){
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("[Server] User (" + id + ") entered.");
PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream())); // sock의 OutputStream으로 PrintWriter를 만듦
br = new BufferedReader(new InputStreamReader(sock.getInputStream())); // sock의 InputStream으로 Input Buffer를 만듦
id = br.readLine(); //input buffer로 id를 읽음
broadcast(id + " entered.");
System.out.println("[Server] User (" + id + ") entered."); //
synchronized(hm){
hm.put(this.id, pw);
}
Expand All @@ -43,6 +47,8 @@ public ChatThread(Socket sock, HashMap hm){
System.out.println(ex);
}
} // construcor


public void run(){
try{
String line = null;
Expand All @@ -67,29 +73,42 @@ public void run(){
}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);
String msg2 = msg.substring(end + 1);
Object obj = hm.get(to);
if(obj != null){
PrintWriter pw = (PrintWriter)obj;
PrintWriter pw = (PrintWriter) obj;
pw.println(id + " whisphered. : " + msg2);
pw.flush();
} // if
}
} // sendmsg


public void broadcast(String msg){
SimpleDateFormat s = new SimpleDateFormat("[a h:mm]");

synchronized(hm){
Collection collection = hm.values();
Iterator iter = collection.iterator();
while(iter.hasNext()){
Collection collection = hm.values(); //Collection의 값은 HashMap이다.
Iterator iter = collection.iterator(); //Iterator 만들기
while(iter.hasNext()){
PrintWriter pw = (PrintWriter)iter.next();
pw.println(msg);
pw.flush();

//#2
pw.println(s.format(System.currentTimeMillis()));

pw.println(msg); //pw outputStream으로 msg를 전송
pw.flush(); // 버퍼 비우기
}
}

} // broadcast


}