import java.net.*;
import java.io.*;
import java.util.*;
public class TcpIpMultichatServer {
HashMap clients;
TcpIpMultichatServer() {
clients = new HashMap();
Collections.synchronizedMap(clients);
}
public void start() {
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(7777);
System.out.println("서버가 시작되었습니다.");
while(true) {
socket = serverSocket.accept();
System.out.println("["+socket.getInetAddress() + ":"+socket.getPort()+"]"+"에서 접속하였습니다.");
ServerReceiver thread = new ServerReceiver(socket);
thread.start();
}
} catch(Exception e) {
e.printStackTrace();
}
} // start()
void sendToAll(String msg) {
Iterator it = clients.keySet().iterator();
while(it.hasNext()) {
try {
DataOutputStream out = (DataOutputStream)clients.get(it.next());
out.writeUTF(msg);
} catch(IOException e){}
} // while
} // sendToAll
public static void main(String args[]) {
new TcpIpMultichatServer().start();
}
class ServerReceiver extends Thread {
Socket socket;
DataInputStream in;
DataOutputStream out;
ServerReceiver(Socket socket) {
this.socket = socket;
try {
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch(IOException e) {}
}
public void run() {
String name = "";
try {
name = in.readUTF();
sendToAll("#"+name+"님이 들어오셨습니다.");
clients.put(name, out);
System.out.println("현재 서버접속자 수는 " + clients.size() + "입니다.");
while(in!=null) {
sendToAll(in.readUTF());
}
} catch(IOException e) {
// ignore
} finally {
sendToAll("#"+name+"님이 나가셨습니다.");
clients.remove(name);
System.out.println("["+socket.getInetAddress()+":"+socket.getPort()+"]"+"에서 접속을 종료하였습니다.");
System.out.println("현재 서버접속자 수는 "+ clients.size()+"입니다.");
} // try
} // run
} // ReceiverThread
} // class
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class TcpIpMultichatClient {
public static void main(String args[]) {
if(args.length!=1) {
System.out.println("USAGE: java TcpIpMultichatClient 대화명");
System.exit(0);
}
try {
String serverIp = "127.0.0.1";
// 소켓을 생성하여 연결을 요청한다.
Socket socket = new Socket(serverIp, 7777);
System.out.println("서버에 연결되었습니다.");
Thread sender = new Thread(new ClientSender(socket, args[0]));
Thread receiver = new Thread(new ClientReceiver(socket));
sender.start();
receiver.start();
} catch(ConnectException ce) {
ce.printStackTrace();
} catch(Exception e) {}
} // main
static class ClientSender extends Thread {
Socket socket;
DataOutputStream out;
String name;
ClientSender(Socket socket, String name) {
this.socket = socket;
try {
out = new DataOutputStream(socket.getOutputStream());
this.name = name;
} catch(Exception e) {}
}
public void run() {
Scanner scanner = new Scanner(System.in);
try {
if(out!=null) {
out.writeUTF(name);
}
while(out!=null) {
out.writeUTF("["+name+"]"+scanner.nextLine());
}
} catch(IOException e) {}
} // run()
}
static class ClientReceiver extends Thread {
Socket socket;
DataInputStream in;
ClientReceiver(Socket socket) {
this.socket = socket;
try {
in = new DataInputStream(socket.getInputStream());
} catch(IOException e) {}
}
public void run() {
while(in!=null) {
try {
System.out.println(in.readUTF());
} catch(IOException e) {}
}
} // run
}
} // class
'Study > Programming' 카테고리의 다른 글
JSP 날짜 구하기 (0) | 2009.05.27 |
---|---|
JSP 구구단 (0) | 2009.05.27 |
자바 Server, Client (0) | 2009.05.26 |
자바 Network Chat (0) | 2009.05.26 |
자바 Swing 달력 스케줄 관리 (0) | 2009.05.25 |