-
Java - Network, MultiChatJava 2018. 2. 6. 10:29반응형import java.io.IOException;import java.net.Socket;public class MultiClient {public static void main(String[] args) throws IOException {Socket socket = null;// 1. 소켓 생성 + 2. 연결 요청try {socket = new Socket("127.0.0.1", 9000);// 3. 할일 하기 + 스레드 생성 및 실행System.out.println("연결 수락");ClientSender sender = new ClientSender(socket);ClientReceiver receiver = new ClientReceiver(socket);sender.start();receiver.start();} catch (Exception e) {e.printStackTrace();socket.close(); // 문제 발생시 소켓 종료}}}import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;import java.util.Scanner;public class ClientSender extends Thread {Socket socket;DataOutputStream out;public ClientSender(Socket socket) {this.socket = socket;try {out = new DataOutputStream(socket.getOutputStream());} catch (IOException e) {e.printStackTrace();}}@Overridepublic void run() {while(out != null) {try {System.out.println("당신 -> ");Scanner scan = new Scanner(System.in);out.writeUTF(scan.nextLine());} catch(IOException e){e.printStackTrace();break;}}}}import java.io.DataInputStream;import java.io.IOException;import java.net.Socket;public class ClientReceiver extends Thread {Socket socket;DataInputStream in;public ClientReceiver(Socket socket) {this.socket = socket;try {// 소켓에서 뽑고 넣기in = new DataInputStream(socket.getInputStream());} catch (IOException e) {e.printStackTrace();}}@Overridepublic void run() {while(in != null) {try {// 받아온 내용을 출력System.out.println(in.readUTF());} catch (IOException e) {e.printStackTrace();break; // 문제가 생겼을때 종료}}}}import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;public class MultiServer {static ArrayList<Socket> list = new ArrayList<Socket>();public static void main(String[] args) {try {// 1. 소켓생성 + 특정 IP 오픈ServerSocket server = new ServerSocket(9000);Socket socket;// 2. 연결수락(소켓)while(true) {System.out.println("요청 대기 중");socket = server.accept();// 3. 생성된 소켓 저장list.add(socket);// 4. 서버 스레드 생성ServerManager manager = new ServerManager(socket);manager.start();}} catch (IOException e) {e.printStackTrace();}}}import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;public class ServerManager extends Thread {Socket socket;DataInputStream in;DataOutputStream out;public ServerManager(Socket socket) {this.socket = socket;try {in = new DataInputStream(socket.getInputStream());sendMsg(socket.getPort()+" 님이 입장 했습니다.");} catch (IOException e) {e.printStackTrace();}}@Overridepublic void run() {try {while( in != null) {sendMsg(socket.getPort()+" -> "+in.readUTF());}} catch (IOException e) {e.printStackTrace();}}// 전체 메세지public void sendMsg(String msg) throws IOException {System.out.println(msg);// 1. 소켓을 뽑아오고for(Socket s : MultiServer.list) {// 2. 스트림을 뽑아오고out = new DataOutputStream(s.getOutputStream());out.writeUTF(msg); // 3. 메세지 전송out.flush();}}}반응형
'Java' 카테고리의 다른 글
Java - Network - File I/O (0) 2018.02.07 Java - UDP (0) 2018.02.06 Java - Network - Chat (0) 2018.02.06 Java - Network (0) 2018.02.05 Java - Stream IO (0) 2018.02.02 댓글