๐ก๋ฉํฐ ์ค๋ ๋ ํ์ฉ
์๋ฐฉํฅ ํต์ ์ ์ง์์ ์ผ๋ก ์ํํ๊ธฐ ์ํด ์๋ฒ์ ํด๋ผ์ด์ธํธ ๋ชจ๋์์ ํค๋ณด๋ ์ ๋ ฅ์ ๋ฐ์ ์๋๋ฐฉ์๊ฒ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๊ณ ๋ฐ์ ์ ์๋๋ก ์ค๋ ๋์ while ๋ฌธ์ ํ์ฉํ์ฌ ์ฝ๋๋ฅผ ์์ฑํด๋ด ์๋ค.
์๋ฒ ์ธก ์ฝ๋
โผ ServerSocket ์ ์์ฑํ๊ณ ํด๋ผ์ด์ธํธ์ ์ฐ๊ฒฐ์ ๊ธฐ๋ค๋ฆฝ๋๋ค.
โผ BufferedReader ๋ฅผ ์ฌ์ฉํ์ฌ ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ๋ฉ์์ง๋ฅผ ์ฝ๊ณ , PrintWriter๋ฅผ ์ฌ์ฉํ์ฌ ํด๋ผ์ด์ธํธ์๊ฒ ๋ฉ์์ง๋ฅผ ๋ณด๋ ๋๋ค.
โผ ํค๋ณด๋ ์ ๋ ฅ์ ๋ฐ๊ธฐ ์ํด BufferedReader๋ฅผ ์ฌ์ฉํฉ๋๋ค.
โผ ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ๋ ์ค๋ ๋์ ํค๋ณด๋ ์ ๋ ฅ์ ํด๋ผ์ด์ธํธ๋ก ๋ณด๋ด๋ ์ค๋ ๋๋ฅผ ๊ฐ๊ฐ ์คํํฉ๋๋ค.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MultiThreadServer {
// ๋ฉ์ธ ํจ์
public static void main(String[] args) {
System.out.println("=== ์๋ฒ ์คํ === ");
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(5001);
socket = serverSocket.accept();
System.out.println("ํฌํธ ๋ฒํธ - 5001 ํ ๋น ์๋ฃ");
// 1. ํด๋ผ์ด์ธํธ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ์
๋ ฅ ์คํธ๋ฆผ ํ์
// 2. ํด๋ผ์ด์ธํธ์ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ผ ์ถ๋ ฅ ์คํธ๋ฆผ ํ์
// ์๋ฒ์ธก - ํค๋ณด๋ ์
๋ ฅ์ ๋ฐ๊ธฐ ์ํ ์
๋ ฅ ์คํธ๋ฆผ ํ์
BufferedReader socketReader =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter socketWriter =
new PrintWriter(socket.getOutputStream(), true);
BufferedReader keyboardReader =
new BufferedReader(new InputStreamReader(System.in));
// ๋ฉํฐ ์ค๋ ๋ฉ ๊ฐ๋
์ ํ์ฅ
// ํด๋ผ์ด์ธํธ๋ก ๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ๋ ์ค๋ ๋
Thread readThread = new Thread(() -> {
try {
String clientMessage;
while( (clientMessage = socketReader.readLine()) != null ) {
System.out.println("์๋ฒ์ธก ์ฝ์ : " + clientMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
});
// ํด๋ผ์ด์ธํธ์๊ฒ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๋ ์ค๋ ๋ ์์ฑ
Thread writeThread = new Thread(() -> {
try {
String serverMessage;
while( ( serverMessage = keyboardReader.readLine() ) != null) {
// 1. ๋จผ์ ํค๋ณด๋๋ฅผ ํตํด์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ
// 2. ์ถ๋ ฅ ์คํธ๋ฆผ์ ํ์ฉํด์ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด์ผ ํ๋ค.
socketWriter.println(serverMessage);
}
} catch (Exception e2) {
}
});
// ์ค๋ ๋ ๋์ -> start() ํธ์ถ
readThread.start();
writeThread.start();
// Thread join() ๋ฉ์๋๋ ํ๋์ ์ค๋ ๋๊ฐ ์ข
๋ฃ๋ ๋ ๊น์ง ๊ธฐ๋ค๋ฆฌ๋๋ก ํ๋
// ๊ธฐ๋ฅ์ ์ ๊ณต ํ๋ค.
readThread.join();
writeThread.join();
System.out.println("--- ์๋ฒ ํ๋ก๊ทธ๋จ ์ข
๋ฃ --- ");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}// end of class
Thread์ join() ๋ฉ์๋ - ์ญํ ๊ณผ ๊ธฐ๋ฅ
1. ์ค๋ ๋ ๋๊ธฐํ:
join() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ์ค๋ ๋๊ฐ ์์๋๋ก ์ข ๋ฃ๋๋๋ก ํ ์ ์์ต๋๋ค. ๋ฉ์ธ ์ค๋ ๋๋ join() ๋ฉ์๋๋ฅผ ํธ์ถํ ์ค๋ ๋๊ฐ ์์ ์ ๋ง์น ๋๊น์ง ๊ธฐ๋ค๋ฆฝ๋๋ค.
2. ํ๋ก๊ทธ๋จ ํ๋ฆ ์ ์ด:
join() ๋ฉ์๋๋ฅผ ํตํด ์ค๋ ๋๊ฐ ์๋ฃ๋๊ธฐ ์ ๊น์ง ๋ฉ์ธ ์ค๋ ๋๊ฐ ์ข ๋ฃ๋์ง ์๋๋ก ๋ณด์ฅํ ์ ์์ต๋๋ค. ์ด๋ ํ๋ก๊ทธ๋จ์ด ๋ชจ๋ ์์ ์ ์๋ฃํ๊ธฐ ์ ์ ์ข ๋ฃ๋๋ ๊ฒ์ ๋ฐฉ์งํฉ๋๋ค.
3. ์ ํํ ์ข ๋ฃ ์์ :
join() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ํน์ ์ค๋ ๋๊ฐ ์๋ฃ๋๊ธฐ ์ ๊น์ง ๋ค๋ฅธ ์์ ์ ์งํํ์ง ์๋๋ก ์ ์ดํ ์ ์์ต๋๋ค. ์ด๋ฅผ ํตํด ์ ํํ ์ข ๋ฃ ์์ ์ ํ์ธํ ์ ์์ต๋๋ค.
ํด๋ผ์ด์ธํธ ์ธก ์ฝ๋
โผ Socket ์ ์์ฑํ์ฌ ์๋ฒ์ ์ฐ๊ฒฐํฉ๋๋ค.
โผ BufferedReader๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฒ๋ก๋ถํฐ ๋ฉ์์ง๋ฅผ ์ฝ๊ณ , PrintWriter๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฒ์๊ฒ ๋ฉ์์ง๋ฅผ ๋ณด๋ ๋๋ค.
โผ ํค๋ณด๋ ์ ๋ ฅ์ ๋ฐ๊ธฐ ์ํด BufferedReader๋ฅผ ์ฌ์ฉํฉ๋๋ค.
โผ ์๋ฒ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ๋ ์ค๋ ๋์ ํค๋ณด๋ ์ ๋ ฅ์ ์๋ฒ๋ก ๋ณด๋ด๋ ์ค๋ ๋๋ฅผ ๊ฐ๊ฐ ์คํํฉ๋๋ค.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class MultiThreadClient {
public static void main(String[] args) {
System.out.println("### ํด๋ผ์ด์ธ ์คํ ###");
try {
Socket socket = new Socket("192.168.0.48", 5000);
System.out.println("*** connected to the Server ***");
PrintWriter socketWriter = new PrintWriter(socket.getOutputStream(), true);
BufferedReader socketReader =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader keyboarReader =
new BufferedReader(new InputStreamReader(System.in));
// ์๋ฒ๋ก ๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ๋ ์ค๋ ๋
Thread readThread = new Thread(() -> {
// while <----
try {
String serverMessage;
while( (serverMessage = socketReader.readLine()) != null ) {
System.out.println("์๋ฒ์์ ์จ MSG : " + serverMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
});
// ์๋ฒ์๊ฒ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๋ ์ค๋ ๋
Thread writeThread = new Thread(() -> {
try {
String clientMessage;
while( (clientMessage = keyboarReader.readLine()) != null) {
// 1. ํค๋ณด๋์์ ๋ฐ์ดํฐ๋ฅผ ์์ฉํ๋ก๊ทธ๋จ ์์ผ๋ก ์
๋ ฅ ๋ฐ์์
// 2. ์๋ฒ์ธก ์์ผ๊ณผ ์ฐ๊ฒฐ ๋์ด์๋ ์ถ๋ ฅ ์คํธ๋ฆผ ํตํด ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ธ๋ค.
socketWriter.println(clientMessage);
}
} catch (Exception e2) {
e2.printStackTrace();
}
});
readThread.start();
writeThread.start();
readThread.join();
writeThread.join();
System.out.println(" ํด๋ผ์ด์ธํธ ์ธก ํ๋ก๊ทธ๋จ ์ข
๋ฃ ");
} catch (Exception e) {
}
}// end of main
}// end of class
์๋ฒ์ธก ์ฝ๋ ๋ฆฌํฉํ ๋ง 1๋จ๊ณ - ํจ์๋ก ๋ถ๋ฆฌํด๋ณด๊ธฐ
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MultiThreadServer {
public static void main(String[] args) {
System.out.println("========== ์๋ฒ ์คํ =========");
// ์๋ฒ์ธก ์์ผ์ ๋ง๋ค๊ธฐ ์ํ ์ค๋น๋ฌผ
// ์๋ฒ ์์ผ, ํฌํธ ๋ฒํธ
try (ServerSocket serverSocket = new ServerSocket(5000)) {
// ํด๋ผ์ด์ธํธ ๋๊ธฐ --> ์ฐ๊ฒฐ ์์ฒญ -- ์์ผ ๊ฐ์ฒด๋ฅผ ์์ฑ(ํด๋ผ์ด์ธํธ์ ์ฐ๊ฒฐ๋ ์ํ)
Socket socket = serverSocket.accept();
System.out.println("---- client connected ----");
// ํด๋ผ์ด์ธํธ์ ํต์ ์ ์ํ ์คํธ๋ฆผ์ ์ค์ (๋์ ์์ผ์ ์ป์๋ค)
BufferedReader readerStream =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writerStream =
new PrintWriter(socket.getOutputStream(), true);// true -> (์คํ + ์ญํ ์ ํด์ค)
// ํค๋ณด๋ ์คํธ๋ฆผ ์ค๋น
BufferedReader keyboardReader =
new BufferedReader(new InputStreamReader(System.in)); // ํค๋ณด๋ -> System.in
// ์ค๋ ๋๋ฅผ ์์ํฉ๋๋ค
startReadThread(readerStream);
startWriteThread(writerStream, keyboardReader);
System.out.println("main ์ค๋ ๋ ์์
์๋ฃ...");
} catch (Exception e) {
}
}// end of main
//////////////////////////////////////////////
// ํด๋ผ์ด์ธํธ๋ก ๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ๋ ์ค๋ ๋ ๋ถ๋ฆฌ
private static void startReadThread(BufferedReader bufferedReader) {
Thread readThread = new Thread(() -> {
try {
String clientMessage;
while ((clientMessage = bufferedReader.readLine()) != null) {
// ์๋ฒ์ธก ์ฝ์์ ํด๋ผ์ด์ธํธ๊ฐ ๋ณด๋ธ ๋ฌธ์ ๋ฐ์ดํฐ ์ถ๋ ฅ
System.out.println("ํด๋ผ์ด์ธํธ์์ ์จ MSG" + clientMessage);
}
} catch (Exception e) {
e.printStackTrace();
}
});
readThread.start(); // ์ค๋ ๋ ์คํ -> run() ๋ฉ์๋ ์งํ
// ๋ฉ์ธ ์ค๋ ๋ ๋๊ธฐ ์ฒ๋ฆฌ --> join() --> ๊ณ ๋ฏผ --> 2๋ฒ์ ๋ฐ๋ณต ๋ ๋ฏ??
waitForThreadToEnd(readThread);
}
// ์๋ฒ์ธก์์ -> ํด๋ผ์ด์ธํธ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๋ ๊ธฐ๋ฅ
private static void startWriteThread(PrintWriter printWriter, BufferedReader keyboardReader) {
Thread writeThread = new Thread(() -> {
try {
String serverMessage;
while ((serverMessage = keyboardReader.readLine()) != null) {
printWriter.println(serverMessage);
printWriter.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
});
writeThread.start();
// ๋ฉ์ธ ์ค๋ ๋ ๋๊ธฐ
}
// ์์ปค ์ค๋ ๋๊ฐ ์ข
๋ฃ ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ๋ ๋ฉ์๋
private static void waitForThreadToEnd(Thread thread) {
try {
thread.join();
} catch (Exception e) {
e.printStackTrace();
}
}
}// end of class
waitForThreadToEnd(writeThread); ← ์ ๊ฑฐ ๋์ or ๋ฆฌํฉํ ๋ง ๋์
์๋ฒ์ธก ์ฝ๋ ๋ฆฌํฉํ ๋ง 2๋จ๊ณ - ์์ ํ์ฉ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
// ์์์ ํ์ฉ
public abstract class AbstractServer {
private ServerSocket serverSocket;
private Socket socket;
private BufferedReader readerStream;
private PrintWriter writerStream;
private BufferedReader keyboardReader;
// set ๋ฉ์๋
// ๋ฉ์๋ ์์กด ์ฃผ์
(๋ฉค๋ฒ ๋ณ์์ ์ฐธ์กฐ ๋ณ์ ํ ๋น)
protected void setServerSocket(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}
// ๋ฉ์๋ ์์กด ์ฃผ์
(๋ฉค๋ฒ ๋ณ์์ ์ฐธ์กฐ ๋ณ์ ํ ๋น)
protected void setSocket(Socket socket) {
this.socket = socket;
}
// get ๋ฉ์๋
protected ServerSocket getServerSocket() {
return this.serverSocket;
}
// ์คํ์ ํ๋ฆ์ด ํ์ํ๋ค. (์์๊ฐ ์ค์)
public final void run() {
// 1. ์๋ฒ ์
ํ
- ํฌํธ ๋ฒํธ ํ ๋น
try {
setupServer();
connection();
setupStream();
startService(); // ๋ด๋ถ์ ์ผ๋ก while ๋์
} catch (IOException e) {
e.printStackTrace();
} finally {
cleanup();
}
}
// 1. ํฌํธ ๋ฒํธ ํ ๋น (๊ตฌํ ํด๋์ค์์ ์ง์ ์ค๊ณ)
protected abstract void setupServer() throws IOException;
// 2. ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ๋๊ธฐ ์คํ (๊ตฌํ ํด๋์ค)
protected abstract void connection() throws IOException;
// 3. ์คํธ๋ฆผ ์ด๊ธฐํ (์ฐ๊ฒฐ๋ ์์ผ์์ ์คํธ๋ฆผ์ ๋ฝ์์ผ ํจ) - ์ฌ๊ธฐ์ ํจ(๊ทธ๋์ private๋ก ์ค์ ํจ)
private void setupStream() throws IOException {
readerStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writerStream = new PrintWriter(socket.getOutputStream(),true);
keyboardReader = new BufferedReader(new InputStreamReader(System.in));
}
// 4. ์๋น์ค ์์
private void startService() {
Thread readThread = createReadThread();
Thread writeThread = createWriteThread();
readThread.start();
writeThread.start();
try {
readThread.join();
writeThread.join();
} catch (Exception e) {
e.printStackTrace();
}
}
private Thread createReadThread() {
return new Thread(() -> {
try {
String msg;
while((msg = readerStream.readLine()) != null) {
// ์๋ฒ์ธก ์ฝ์์ ์ถ๋ ฅ
System.out.println("ํด๋ผ์ด์ธํธ ์ธก msg : " + msg);
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
// ์บก์ํ
private Thread createWriteThread() {
return new Thread(() -> {
try {
String msg;
// scnner.nextLine(); <-- ๋ฌดํ ๋๊ธฐ(์ฌ์ฉ์๊ฐ ์ฝ์์ ๊ฐ ์
๋ ฅ๊น์ง ๋๊ธฐ) ...
// ์๋ฒ์ธก ํค๋ณด๋์์ ๋ฐ์ดํฐ๋ฅผ ํ์ค๋ผ์ธ์ผ๋ก ์ฝ์
while((msg = keyboardReader.readLine()) != null) {
// ํด๋ผ์ด์ธํธ์ ์ฐ๊ฒฐ๋ ์์ผ์๋ค๊ฐ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋
writerStream.println(msg);
writerStream.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
// ์บก์ํ - ์์ผ ์์ ์ข
๋ฃ
private void cleanup() {
try {
if(socket != null) {
socket.close();
}
if(serverSocket != null) {
serverSocket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
๊ตฌํ ํด๋์ค - AbstractServer ์์ ํ์ฉ
import java.io.IOException;
import java.net.ServerSocket;
public class MyThreadServer extends AbstractServer {
@Override
protected void setupServer() throws IOException {
// ์ถ์ ํด๋์ค --> ๋ถ๋ชจ --> ์์ (๋ถ๋ชจ ๊ธฐ๋ฅ์ ํ์ฅ ๋๋ ์ฌ์ฉ)
// ์๋ฒ์ธก ์์ผ ํต์ -- ์ค๋น๋ฌผ : ์๋ฒ ์์ผ
super.setServerSocket(new ServerSocket(5000));
System.out.println(">>> Server started on Port 5000 <<<");
}
@Override
protected void connection() throws IOException {
// ์๋ฒ ์์ผ.accept() ํธ์ถ์ด๋ค !!!
super.setSocket(super.getServerSocket().accept());
}
public static void main(String[] args) {
MyThreadServer myThreadServer = new MyThreadServer();
myThreadServer.run();
}
}
๋ณต์กํ ์ ํ๋ฆฌ์ผ์ด์ ์์๋ ์ถ์ ํด๋์ค์ ๊ตฌํ ํด๋์ค๋ฅผ ๋ถ๋ฆฌํ๋ ๊ฒ์ด ์ ์ฉํ ์ ์์ง๋ง, ๊ฐ๋จํ ๊ฒฝ์ฐ์๋ ๋จ์ผ ํด๋์ค ์ค๊ณ๊ฐ ๋ ์ ํฉํ ์ ์๋ค. ์ํฉ์ ๋ฐ๋ผ ์ ์ ํ ์ค๊ณ๋ฅผ ์ ํํ๋ ๊ฒ์ด ์ค์ํ๋ค.
๋์ ๊ณผ์ - ํด๋ผ์ด์ธํธ ์ธก ์ฝ๋ ๋ฆฌํฉํ ๋ง
1๋จ๊ณ, 2๋จ๊ณ๋ก ์งํํด ๋ณด๊ธฐ
ํด๋ผ์ด์ธํธ ์ธก ์ฝ๋ ๋ฆฌํฉํ ๋ง 1 ๋จ๊ณ
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
// 1๋จ๊ณ - ํจ์๋ก ๋ถ๋ฆฌ ํด์ ๋ฆฌํฉํ ๋ง ์งํ
public class MultiThreadClient2 {
// ๋ฉ์ธ ํจ์
public static void main(String[] args) {
System.out.println("### ํด๋ผ์ด์ธํธ ์คํ ###");
try (Socket socket = new Socket("localhost", 5000)) { // ํด๋ผ์ด์ธํธ๋ socket๋ง ์์ผ๋ฉด ๋จ
System.out.println("connected to the server !!");
// ์๋ฒ์ ํต์ ์ ์ํ ์คํธ๋ฆผ ์ด๊ธฐํ
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter printWriter =
new PrintWriter(socket.getOutputStream());
BufferedReader keyboardReader =
new BufferedReader(new InputStreamReader(System.in));
// ๋ฉ์๋ ํธ์ถ -> main๊ฐ ํจ // ์์ปค 1,2๊ฐ ์์ฑ๋จ
startReadThread(bufferedReader);
startWriteThread(printWriter, keyboardReader);
// ๋ฉ์ธ ์ค๋ ๋ ๊ธฐ๋ค๋ ค๋ ์ด๋์ ์์ง?? -> ๊ฐ๋
์ฑ์ด ๋จ์ด์ง
// startWriteThread() <-- ๋ด๋ถ์ ์์
} catch (Exception e) {
e.printStackTrace();
}
}// end of main
// 1. ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ๋ ์ค๋ ๋ ์์ ๋ฉ์๋ ์์ฑ
private static void startReadThread(BufferedReader reader) {
Thread readThread = new Thread(() -> {
try {
String msg;
while ((msg = reader.readLine()) != null) {
System.out.println("์๋ฒ์์ ์จ msg : " + msg);
}
} catch (Exception e) {
e.printStackTrace();
}
});
readThread.start();
}
// 2. ํค๋ณด๋์์ ์
๋ ฅ์ ๋ฐ์ ํด๋ผ์ด์ธํธ ์ธก์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๋ ์ค๋ ๋
private static void startWriteThread(PrintWriter writer, BufferedReader keyboardReader) {
Thread writerThread = new Thread(() -> {
try {
String msg;
while ((msg = keyboardReader.readLine()) != null) {
// ์ ์ก
writer.println(msg);
writer.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
});
writerThread.start();
}
}// end of class
ํด๋ผ์ด์ธํธ ์ธก ์ฝ๋ ๋ฆฌํฉํ ๋ง 2๋จ๊ณ (์์ ํ์ฉ)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
// 2๋จ๊ณ - ์์ ํ์ฉ ๋ฆฌํฉํ ๋ง ๋จ๊ณ
public abstract class AbstractClient {
private Socket socket;
private BufferedReader readerStream;
private PrintWriter writerStream;
private BufferedReader keyboardReader;
// set
protected void setSocket(Socket socket) {
this.socket = socket;
}
// get
protected Socket getSocket() {
return this.socket;
}
public final void run() {
try {
connection();
setupStream();
startService();
} catch (IOException e) {
e.printStackTrace();
} finally {
cleanup();
}
}
// ์ฐ๊ฒฐ ๋๊ธฐ
protected abstract void connection() throws IOException;
// ์คํธ๋ฆผ ์ด๊ธฐํ
private void setupStream() throws IOException {
readerStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writerStream = new PrintWriter(socket.getOutputStream());
keyboardReader = new BufferedReader(new InputStreamReader(System.in));
}
// ์๋น์ค ์์
private void startService() {
Thread readThread = createReadThread();
Thread writeThread = createWriteThread();
readThread.start();
writeThread.start();
try {
readThread.join();
writeThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private Thread createReadThread() {
return new Thread(() -> {
try {
String msg;
while((msg = readerStream.readLine()) != null) {
System.out.println("์๋ฒ ์ธก msg : " + msg);
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
private Thread createWriteThread() {
return new Thread(() -> {
try {
String msg;
while((msg = keyboardReader.readLine()) != null) {
writerStream.println(msg);
writerStream.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
private void cleanup() {
try {
if(socket != null) {
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}// end of class
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
// 2-1 ์์์ ํ์ฉํ ๊ตฌํ ํด๋์ค ์ค๊ณ ํ๊ธฐ
public class MyClient extends AbstractClient {
@Override
protected void connection() throws IOException {
super.setSocket(new Socket("localhost", 5000));
}
public static void main(String[] args) {
MyClient myClient = new MyClient();
myClient.run();
}
}
์คํ ๊ฒฐ๊ณผ______
'JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] ๋คํธ์ํฌ ํ๋กํ ์ฝ (0) | 2024.05.24 |
---|---|
[JAVA] 1: N ์์ผ ์๋ฐฉํฅ ํต์ (1) | 2024.05.24 |
[JAVA] 1:1 ์๋ฐฉํฅ ํต์ (0) | 2024.05.24 |
[JAVA] 1:1 ๋จ๋ฐฉํฅ ํต์ (ํด๋ผ์ด์ธํธ์ธก) (0) | 2024.05.24 |
[JAVA] 1:1 ๋จ๋ฐฉํฅ ํต์ (์๋ฒ์ธก) (0) | 2024.05.24 |