[KOSTA] Spring 기반 Cloud 서비스 개발자 양성 과정 21일차 - 쓰레드, TCP/IP

JUNBEOM PARK·2022년 3월 3일
0
post-thumbnail

🎲 채팅방

쓰레드와 TCP/IP를 활용하여 채팅방 구현

📃 풀이

ClientExam.java


package kosta.net2;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ClientExam {

	public static void main(String[] args) {
		Socket socket = null;
		
		try {
			socket = new Socket("127.0.0.1", 9000);
			
			Thread receive = new ClientReceive(socket);
			Thread sender = new ClientSender(socket);
			
			receive.start();
			sender.start();		
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				//socket.close();	
			} catch (Exception e2) {}
		}
	}

}

ClientReceive.java

package kosta.net2;

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

public class ClientReceive extends Thread {
	Socket socket;
	
	public ClientReceive() {}
	
	public ClientReceive(Socket socket) {
		super();
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			BufferedReader br =
					new BufferedReader(new InputStreamReader(socket.getInputStream()));
			
			while(true) {
				String str = br.readLine();
				if(str == null) break;
				
				System.out.println("서버가 보낸 메시지: " + str);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

ClientSender.java

package kosta.net2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientSender extends Thread {
	Socket socket;
	
	public ClientSender() {}	

	public ClientSender(Socket socket) {
		super();
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			BufferedReader br =
					new BufferedReader(new InputStreamReader(System.in));
			
			PrintWriter writer = new PrintWriter(socket.getOutputStream());
			//BufferedWriter writer = new BufferedWriter(socket.getOutputStream());
			
			while(true) {
				String str = br.readLine();
				if(str.equals("bye")) break;
				writer.println(str);
				writer.flush();
			}
			
			System.out.println("클라이언트 종료");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

ServerExam

package kosta.net2;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerExam {

	public static void main(String[] args) {
		ServerSocket serverSocket = null;
		Socket socket = null;
		
		try {
			serverSocket = new ServerSocket(9000);
			System.out.println("서버 실행 중");
			socket = serverSocket.accept();
			
			Thread receive = new ServerReceive(socket);
			Thread sender = new ServerSender(socket);
			
			receive.start();
			sender.start();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				//socket.close();
				//serverSocket.close();
			} catch (Exception e2) {}
		}
	}

}

ServerReceive.java

package kosta.net2;

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

public class ServerReceive extends Thread {
	Socket socket;
	
	public ServerReceive() {}	

	public ServerReceive(Socket socket) {
		super();
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			BufferedReader br =
					new BufferedReader(new InputStreamReader(socket.getInputStream()));
			
			while(true) {
				String str = br.readLine();
				if(str == null) break;
				
				System.out.println("클라이언트가 보낸 메시지 : " + str);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

ServerSender.java

package kosta.net2;

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


public class ServerSender extends Thread {
	Socket socket;
	
	public ServerSender() {}	
	
	public ServerSender(Socket socket) {
		super();
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			BufferedReader br =
					new BufferedReader(new InputStreamReader(System.in));
			
			PrintWriter writer = new PrintWriter(socket.getOutputStream());
			
			while(true) {
				String str = br.readLine();
				if(str.equals("bye")) break;
				writer.println(str);
				writer.flush();
			}
			
			System.out.println("서버 종료");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

profile
DB 엔지니어👍

0개의 댓글