๐Ÿ’ป

Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์ฑ„ํŒ… ํ”„๋กœ๊ทธ๋žจ ๋ณธ๋ฌธ

KITRI/JAVA

Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์ฑ„ํŒ… ํ”„๋กœ๊ทธ๋žจ

๋˜ํšจ๋‹ˆ 2020. 5. 25. 15:37

 

์ฑ„ํŒ…ํ”„๋กœ๊ทธ๋žจ

 

ServerReceiver.java

package network06.Chat;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;

public class ServerReceiver extends Thread{
	private Socket socket;
	
	public ServerReceiver(Socket socket) {
		this.socket = socket;
	}
	@Override
	public void run() {
		BufferedReader br = null;
		
		try {
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()), 512);
			
			while(true) {
				String inputMsg = br.readLine();
				
				if(inputMsg.equalsIgnoreCase("Q")) {
					System.out.println("์ฑ„ํŒ…๋ฐฉ์„ ๋‚˜๊ฐ€์…จ์Šต๋‹ˆ๋‹ค.");
					break;
				}
				System.out.println(inputMsg);
			}
		} catch (NullPointerException | SocketException e) {
			System.out.println("์ฑ„ํŒ…์ด ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."); //ํด๋ผ์ด์–ธํŠธ์—์„œ socket close null ๋ฐ˜ํ™˜
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(br!=null) br.close();
				if(socket!=null) socket.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

 

ServerEx.java

package network06.Chat;

import java.net.ServerSocket;
import java.net.Socket;

public class ServerEx {

	public static void main(String[] args) {
		ServerSocket serverSocket = null;
		
		try {
			serverSocket = new ServerSocket(8989);
			System.out.println("์„œ๋ฒ„ ์ฑ—ํŒ… ์ค€๋น„์ค‘...");
			
			Socket socket = serverSocket.accept();
			
			ServerReceiver receiver = new ServerReceiver(socket);
			receiver.start();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(serverSocket!=null) serverSocket.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

	}

}

 

ClientSender.java

package network06.Chat;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class ClientSender extends Thread{
	private Socket socket;
	private String nickName;
	
	public ClientSender(Socket socket, String nickName) {
		this.socket = socket;
		this.nickName = nickName;
	}
	
	@Override
	public void run() {
		BufferedWriter bw = null;
		PrintWriter pw = null;
		Scanner sc = null;
		
		try {
			bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()), 512); 
			pw = new PrintWriter(bw);
			sc = new Scanner(System.in);
			
			while(true) {
				String outputMsg = sc.nextLine();
				
				if(outputMsg.equalsIgnoreCase("q")) {
					pw.println(outputMsg);
					pw.flush();
					break;
				}
				
				pw.println(nickName + ">>>" + outputMsg);
				pw.flush();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(bw!=null) bw.close();
				if(pw!=null) bw.close();
				if(sc!=null) bw.close();
				if(socket!=null) socket.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

 

ClientEx.java

package network06.Chat;

import java.net.Socket;

public class ClientEx {

	public static void main(String[] args) {
		if(args.length!=1) {
			System.out.print("๋‹‰๋„ค์ž„ ํ•˜๋‚˜๋งŒ ์ž…๋ ฅํ•˜์„ธ์š”:");
			return;
		}
		
		try {
			String nickName = args[0];
			Socket socket = new Socket("๋‚ด ์•„์ดํ”ผ ์ฃผ์†Œ", 8989);
			
			ClientSender sender = new ClientSender(socket, nickName);
			sender.start();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

๋‚ด ip ์ฃผ์†Œ ์ฒดํฌ

 

 

network > chat ํด๋”์— ์ž‘์„ฑํ•œ ์ž๋ฐ”ํŒŒ์ผ์„ ๋ณต์‚ฌํ•ด์„œ ๋ถ™์—ฌ๋„ฃ๊ธฐํ•œ๋‹ค.

๋ฉ”๋ชจ์žฅ์œผ๋กœ ์—ด๊ณ  ๋‚œ ํ›„ ํŒจํ‚ค์ง€๋ช…์„ ์ง€์šฐ๊ณ , ๋‹ค๋ฅธ์ด๋ฆ„์œผ๋กœ ์ €์žฅ > ansi๋กœ ํ•˜๊ณ  ๋ชจ๋“ ํŒŒ์ผ 

 

C:\sohyunkim\java\network\chat

 

์ปดํŒŒ์ผ(java filename.java๋ช…๋ น ์‹คํ–‰) ํ›„์— .class ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

๋‹จ์ฒด ์ฑ„ํŒ… ํ”„๋กœ๊ทธ๋žจ

 

ClientEx.java

package network07.ChatMulti;

import java.net.Socket;

public class ClientEx {

	public static void main(String[] args) {
		
		try {
			
			Socket socket = new Socket("192.168.0.23", 7575);
			
			ClientSender sender = new ClientSender(socket, "haha");
			sender.start();
			
			ClientReceiver receiver = new ClientReceiver(socket);
			receiver.start();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


ClientReceiver.java

package network07.ChatMulti;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;


public class ClientReceiver extends Thread{
	private Socket socket;
	public ClientReceiver(Socket socket) {
		this.socket = socket;
	}
	
	@Override
	public void run() {
		BufferedReader br = null;
		
		try {
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()), 512);
			while(true) {
				String inputMsg = br.readLine();
				
				if(inputMsg.equalsIgnoreCase("Q")) {
					System.out.println("์ฑ„ํŒ…๋ฐฉ์„ ๋‚˜๊ฐ€์…จ์Šต๋‹ˆ๋‹ค.");
					break;
				}
				System.out.println(inputMsg);
			}
		} catch (NullPointerException | SocketException e) {
			System.out.println("์ฑ„ํŒ…์ด ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(br!=null) br.close();
				if(socket!=null) socket.close();
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
	}
}

ClientSender.java

package network07.ChatMulti;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class ClientSender extends Thread{
	private Socket socket;
	private String nickName;
	
	public ClientSender(Socket socket, String nickName) {
		this.socket = socket;
		this.nickName = nickName;
		
		System.out.println("์ฑ„ํŒ… ์‹œ์ž‘์ž…๋‹ˆ๋‹ค.");
	}
	
	@Override
	public void run() {
		BufferedWriter bw = null;
		PrintWriter pw = null;
		Scanner sc = null;
		
		try {
			bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()), 512); 
			pw = new PrintWriter(bw);
			sc = new Scanner(System.in);
			
			while(true) {
				String outputMsg = sc.nextLine();
				
				if(outputMsg.equalsIgnoreCase("q")) {
					pw.println(outputMsg);
					pw.flush();
					break;
				}
				
				pw.println(outputMsg);
				pw.flush();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(bw!=null) bw.close();
				if(pw!=null) pw.close();
				if(sc!=null) sc.close();
				if(socket!=null) socket.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

ServerArr.java

package network07.ChatMulti;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class ServerArr extends Thread{
	private Socket socket;
	public static List<PrintWriter> list = Collections.synchronizedList(new ArrayList<PrintWriter>());
	private PrintWriter pw;
	
	public ServerArr(Socket socket) { //[0]pw ํ™๊ธธ๋™/ [1]pw ๊น€๊ธธ๋™
		this.socket = socket;
		sendArr();
	}
	
	public void sendArr() {
		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()), 152);
			pw = new PrintWriter(bw);
			
			list.add(pw);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void sendPrintAll(String str) {
		for(PrintWriter pw:list) {
			pw.println(str);
			pw.flush();
		}
	}
	
	@Override
	public void run() {
		BufferedReader br = null;
		String nickName = null;
		
		try {
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()), 512);
			nickName = br.readLine();
			
			sendPrintAll("#" + nickName + "๋‹˜์ด ๋“ค์–ด์˜ค์…จ์Šต๋‹ˆ๋‹ค.");
			
			while(true) {
				String inputMsg = br.readLine();
				if(inputMsg.equalsIgnoreCase("Q")) {
					sendPrintAll("#" + nickName + "๋‹˜์ด ์ฑ„ํŒ…๋ฐฉ์„ ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค.");
					break;
				}
				
				sendPrintAll(nickName + ">>" + inputMsg);
			}
		} catch(SocketException | NullPointerException e) {
			sendPrintAll("#" + nickName + "๋‹˜์ด ์ฑ„ํŒ…๋ฐฉ์„ ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค.");
		} catch (Exception e) {

		}
	}
	
}

ServerEx.java

package network07.ChatMulti;

import java.net.ServerSocket;
import java.net.Socket;

public class ServerEx {

	public static void main(String[] args) {
		ServerSocket serverSocket = null;
		
		try {
			serverSocket = new ServerSocket(7575);
			System.out.println("์„œ๋ฒ„ ์ฑ—ํŒ… ์ค€๋น„์ค‘...");			
			
			while(true) {
				Socket socket = serverSocket.accept(); 
				
				ServerArr arr = new ServerArr(socket);
				arr.start();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(serverSocket!=null) serverSocket.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

	}

}

cmd ์ฐฝ์—์„œ

javac ServerArr.java, javac ServerEx.java, java ServerEx(์„œ๋ฒ„์„ธํŒ…)

์—ฌ๋Ÿฌ๊ฐœ์˜ cmd์ฐฝ์—์„œ

javac ClientReceiver.java , javac ClientSender.java, javac ClientEx.java, java ClienEx 

๋ฐ˜์‘ํ˜•
Comments