- 자바(JAVA)로 채팅방 서버 개설하기
- TcpIpMultiChatServer.java
package chap16;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
public class TcpIpMultiChatServer {
HashMap<String, DataOutputStream> clients;
public TcpIpMultiChatServer() {
clients = new HashMap<>();
Collections.synchronizedMap(clients);
}
public static void main(String[] args) {
new TcpIpMultiChatServer().start();
}
public void start() {
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(80);
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();
}
}
void sendToAll(String msg) {
Iterator<String> it = clients.keySet().iterator();
while(it.hasNext()) {
try {
DataOutputStream out = clients.get(it.next());
out.writeUTF(msg);
} catch (IOException e) {}
}
}
class ServerReceiver extends Thread {
Socket socket;
DataInputStream in;
DataOutputStream out;
public ServerReceiver(Socket socket) {
this.socket = socket;
try {
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (Exception e) {}
}
@Override
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 (Exception e) {}
finally {
sendToAll("#" + name + "님이 나가셨습니다.");
clients.remove(name);
System.out.println("[" + socket.getInetAddress() + " :: " + name + "]에서 접속을 종료했습니다.");
System.out.println("현재 서버접속자 수는 " + clients.size() + "입니다.");
}
}
}
}