#8 Java - 3 (์ฐ๋ ๋, IO, ๋คํธ์ํฌ)
by JiwonDev# Thread
java.lang.Thread ํด๋์ค๋ฅผ ์์ํ์ฌ ๋ฉํฐ ์ฐ๋ ๋๋ฅผ ๊ตฌํ ํ ์ ์๋ค.
public class Test extends Thread { // ๋ค๋ฅธ ์ฐ๋ ๋๋ก ์คํํ ์ฝ๋ public void run() { System.out.println("thread run."); } public static void main(String[] args) { Test test = new Test(); // ์ฐ๋ ๋ ๊ฐ์ฒด ์์ฑ test.start(); // ์ฝ๋ ์คํ } }
๋ชจ๋ ์๋ฐ๋ฅผ ๋ค๋ฃจ๋ฉฐ ๋ฐฐ์ฐ๊ฒ ์ง๋ง, Lambda๋ฅผ ์ด์ฉํ๋ฉด ์ข ๋ ๊น๋ํ๊ฒ ๊ตฌํ ํ ์ ์๋ค.
public class RunnableLambdaExample { public static void main(String[] args) { Thread thread = new Thread(() -> { String threadName = Thread.currentThread().getName(); System.out.println(threadName); }); thread.setName("Thread #1"); thread.start(); } }
๊ฐ๋ฐ์ ํ๋ค๋ณด๋ฉด ์ฐ๋ ๋๊ฐ ์ข ๋ฃ๋ ํ ์ฒ๋ฆฌํด์ผํ๋ ์์ ์ด ์์ ์ ์๋๋ฐ, Thread.join() ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ๋ชจ๋ ์ฐ๋ ๋๊ฐ ์ข ๋ฃ๋ ๋ ๊น์ง ๊ธฐ๋ค๋ฆฌ๊ฒ ๋ง๋ค ์ ์๋ค.
public static void main(String[] args) { ArrayList<Thread> threads = new ArrayList<Thread>(); for(int i=0; i<10; i++) { Thread t = new Test(i); t.start(); threads.add(t); } for(int i=0; i<threads.size(); i++) { Thread t = threads.get(i); try { t.join(); }catch(Exception e) { } } // ๋ชจ๋ ์ฐ๋ ๋๊ฐ ์ข
๋ฃ ๋ ํ end ์ถ๋ ฅ System.out.println("main end."); }
# Runnable
๋ณดํต ์ค์ ์ฌ์ฉ์์ ์์ด ์ ์ฐํ๊ฒ ํ๋ก๊ทธ๋๋ฐํ๊ธฐ ์ํด Thread ํด๋์ค๋ฅผ ์ง์ ์์ํด์ ๋ณ๊ฒฝํ๊ธฐ๋ณด๋จ ์๋์ ๊ฐ์ด Runnable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ณ ์ด๋ฅผ Thread ํด๋์ค์ ์์ฑ์๋ก ๋๊ธฐ๋ ๋ฐฉ์์ผ๋ก ์ฌ์ฉํ๋ค. ์ด๋ ๊ฒ ํจ์ผ๋ก์จ ํ์ํ ๋ค๋ฅธ ํด๋์ค๋ฅผ ์์๋ฐ๊ณ , Runnable๋ฅผ ๊ตฌํํ์ฌ ๋ค์ค ์์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐ ํ ์ ์๋ ์ฅ์ ๋ ์๋ค.
//class Test implements Runnable{} Thread t = new Thread(new Test(i));
import java.util.ArrayList; public class Test implements Runnable { int seq; public Test(int seq) { this.seq = seq; } public void run() { System.out.println(this.seq+" thread start."); try { Thread.sleep(1000); }catch(Exception e) { } System.out.println(this.seq+" thread end."); } public static void main(String[] args) { ArrayList<Thread> threads = new ArrayList<Thread>(); for(int i=0; i<10; i++) { Thread t = new Thread(new Test(i)); t.start(); threads.add(t); } for(int i=0; i<threads.size(); i++) { Thread t = threads.get(i); try { t.join(); }catch(Exception e) { } } System.out.println("main end."); } }
# Console ์ ์ถ๋ ฅ
์๋ฐ์ ์ฝ์ ์ ์ถ๋ ฅ ์คํธ๋ฆผ์ ์ ๊ณตํ๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก System.out , System.in, System.err ์คํธ๋ฆผ์ด ์ ๊ณต๋๋ค.
์คํธ๋ฆผ์ด ๋ญ์ฃ ?
์คํธ๋ฆผ(Stream)์ด๋?
ํ์ผ์ ์ถ๋ ฅ์ ๋ด๋นํ๋ ๊ฐ์ฒด์ด๋ค. ๋ชจ๋ ํ์ผ์ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ ค ์ฌ์ฉํ ์ ์์ผ๋ฏ๋ก, ํ์ํ ๋ ๋ง๋ค ํด๋น ํ์ผ์ ์ ์ถ๋ ฅ์ ๋ด๋นํ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ์ฌ์ฉํ๋ค. ์ ๋ ฅ์ ํ๋ฆ, ํ์ผ์ ๊ด์ ์ฐ๊ฒฐํ๋ค๋ ์๋ฏธ๋ก Stream์ด๋ผ๊ณ ๋ถ๋ฅด๋ฉฐ ์คํธ๋ฆผ ๊ฐ์ฒด๋ ์ด์์ฒด์ ์ ์์ฒญํ์ฌ ํ์ํ ํ์ผ์ ๋ฐ์์ค๊ณ ๊ฐ๋ฐ์์๊ฒ ์ ๊ณตํด์ค๋ค.
ํ๋ก๊ทธ๋๋ฐ์์๋ ๋ค์๊ณผ ๊ฐ์ ๊ฒ๋ค์ ์คํธ๋ฆผ์ด๋ผ๊ณ ํ ์ ์๋ค.
- ํ์ผ ๋ฐ์ดํฐ (ํ์ผ์ ๊ทธ ์์๊ณผ ๋์ด ์๋ ๋ฐ์ดํฐ์ ์คํธ๋ฆผ์ด๋ค.)
- HTTP ์๋ต ๋ฐ์ดํฐ (๋ธ๋ผ์ฐ์ ๊ฐ ์์ฒญํ๊ณ ์๋ฒ๊ฐ ์๋ตํ๋ HTTP ์๋ต ๋ฐ์ดํฐ๋ ์คํธ๋ฆผ์ด๋ค.)
- ํค๋ณด๋ ์ ๋ ฅ (์ฌ์ฉ์๊ฐ ํค๋ณด๋๋ก ์ ๋ ฅํ๋ ๋ฌธ์์ด์ ์คํธ๋ฆผ์ด๋ค.)
์ฝ์์ถ๋ ฅ์ ๋ฑํ ์ด๋ ค์ธ ๊ฒ์ด ์๋ค. ๊ทธ๋ฅ System.out ์์ ์๋ ์ถ๋ ฅ ๊ฐ์ฒด๋ฅผ ์ด์ฉํ๋ฉด ๋๋ค.
System.out.println() System.err.println() // err๋ ์ฝ์์ถ๋ ฅ์ด์ง๋ง, ์๋ฌ๋ฅผ ์ถ๋ ฅํ ๋ ์ฌ์ฉํ๋ ์คํธ๋ฆผ์ด๋ค.
์ฝ์ ์ ๋ ฅ์ System in์ ์ด์ฉํ์ฌ ์ ๋ ฅ ์คํธ๋ฆผ์ ๋ฐ์ ์ฌ ์ ์๋ค. ๋ค๋ง System.in์ InputStream ๊ฐ์ฒด๋ก ๋ฐํํ๋ฏ๋ก, ์ฌ์ฉํ๊ธฐ ์ํด์ java.io.InputStreamd์ import ํด์ผ ํ๋ค.
import java.io.InputStream; public class StreamTest { public static void main(String[] args) throws Exception { InputStream in = System.in; int a; a = in.read(); // ์ฝ์์
๋ ฅ ํ๋๋ฅผ ๋ฐ์์จ๋ค. byte[] buffer = new byte[3]; in.read(buffer); // ์ฝ์์
๋ ฅ๋ค์ ๋ฐ์ buffer์ ์ ์ฅํ๋ค. } }
์ด๋ฐ์์ผ๋ก ๋ฐ์์จ ์ ๋ ฅ๊ฐ์ ์ด์ง๋ฐ์ดํฐ (์ซ์, byte)์ด๋ฏ๋ก ์ฝ์์ ์ฌ์ฉ๋๋ ๋ฌธ์์ด์ ์ฒ๋ฆฌํ๊ธฐ์๋ ๋ถํธํ๋ค. ๊ทธ๋์ ๋ฌธ์์ด์ ์ฝ๊ฒ ๋ฐ์์ค๊ธฐ ์ํด java.io.InputStreamReader ๊ฐ์ ๊ฐ์ฒด๋ฅผ ์ด์ฉํ๋ค.
import java.io.InputStream; import java.io.InputStreamReader; public class StreamTest { // ๋น์ฐํ๊ฑฐ์ง๋ง, ํ์ผ ๊ฐ์ฒด๋ ์์ธ๋ฅผ ๋์ง๋ฏ๋ก ์ค์ ์ฌ์ฉ์์๋ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ์ด์ผํ๋ค. public static void main(String[] args) throws Exception { InputStreamReader reader = new InputStreamReader(System.in); // BufferedReader๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐ๋ก ๋ฒํผ์ฉ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ง ์์๋ ๋ฒํผ๋ง๋๋ค. BufferedReader bufferReader = new BufferedReader(reader); // InputStreamReader๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ char[] a = new char[3]; reader.read(a); // BufferedReader๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ String a = bufferReader.readLine(); } }
- InputStream - byte
- InputStreamReader - character
- BufferedReader - String
# Scanner
์ฝ์ ์ ๋ ฅ์ ์ฝ๊ฒํ๊ธฐ์ํด J2SE 5.0๋ถํฐ java.util.Scanner๋ผ๋ ์๋ก์ด ๊ฐ์ฒด๊ฐ ์ถ๊ฐ๋์๋ค. ํ ํฐ ๋จ์๋ก ์ ๋ ฅ์ ์ฝ์ด๋ค์ฌ ๊ธฐ์กด์ ๋ฐฉ์๋ณด๋ค ์ฝ๊ณ ์ง๊ด์ ์ด๊ฒ ์ฌ์ฉ ํ ์ ์๋ค.
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(sc.next()); } }
# File ์ ์ถ๋ ฅ
์ฝ์์ ์ถ๋ ฅ์๋ JVM์์ ์คํธ๋ฆผ(System.in, System.out)์ ์๋์ผ๋ก ์์ฑํด์ค์ ๊ทธ๋ฅ ์ฌ์ฉํ๋ฉด ๋์์ง๋ง, ํ์ผ ์ ์ถ๋ ฅ์ ์ํด์๋ ์ง์ ์คํธ๋ฆผ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ผ ํ๋ค. java.io.FileOutputStream์ ์ด์ฉํ์ฌ ์ถ๋ ฅํ ์ ์๋ค.
import java.io.FileOutputStream; import java.io.IOException; public class FileWrite { public static void main(String[] args) throws IOException { // FileOutput์คํธ๋ฆผ FileOutputStream output = new FileOutputStream("c:/out.txt"); output.close(); // FileWriter FileWriter fw = new FileWriter("c:/out.txt"); for(int i=1; i<11; i++) { String data = i+" ๋ฒ์งธ ์ค์
๋๋ค.\r\n"; fw.write(data); } fw.close(); } }
ํ์ผ ์ ์ถ๋ ฅ๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์ด์ง๋ฐ์ดํฐ๋ฅผ ์ฝ๊ฒ ์ฒ๋ฆฌํ๊ธฐ์ํด PrintWrite๋ฑ ๋ค์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ ๊ณต๋๋ค.
import java.io.IOException; import java.io.PrintWriter; public class FileWrite { public static void main(String[] args) throws IOException { PrintWriter pw = new PrintWriter("c:/out.txt"); for(int i=1; i<11; i++) { String data = i+" ๋ฒ์งธ ์ค์
๋๋ค."; pw.println(data); // ๊ณต๋ฐฑ๋ฌธ์๋ฅผ ์๋์ผ๋ก ์
๋ ฅํด์ค๋ค. } pw.close(); } }
ํ์ผ ์ ๋ ฅ๋ ์คํธ๋ฆผ์ ์ง์ ์์ฑํด์ฃผ๋ ๊ฒ์ ์ ์ธํ๋ฉด, ์ฝ์ ์ ๋ ฅ์ด๋ ํฌ๊ฒ ๋ค๋ฅผ ๊ฒ์ด ์๋ค.
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class FileRead { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("c:/out.txt")); while(true) { String line = br.readLine(); if (line==null) break; System.out.println(line); } br.close(); } }
# Fast IO (์ฝ๋ฉํ ์คํธ์ฉ, ๋ฉํฐ์ค๋ ๋)
Java๋ Cpp๊ณผ ๋ฌ๋ฆฌ ์ฝ์์ ์ถ๋ ฅ ์๊ฐ์ ์ฌ์ ๊ฐ ์์ผ๋ฏ๋ก ๊ทธ๋ฅ Scanner๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ ์ผ ํธํ๊ณ ๊น๋ํ๋ค.
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); System.out.println(a + b); } }
์ ๋ง ์ฝ์์ ์ถ๋ ฅ ๋ฐ์ดํฐ๊ฐ ๋ง์์ ์ต์ ํ๊ฐ ํ์ํ๋ค๋ฉด BufferedReader์ StringTokenize๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์๋ค.
// Working program using BufferedReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { // BufferedReader ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); // ํ์ค์ ์ฌ๋ฌ ๊ฐ์ ์
๋ ฅํ๋ค๋ฉด StringTokenizer๋ฅผ ์ฌ์ฉํ๋ค. StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); int count = 0; while (n-- > 0) { // ๊ฐ์ด ํ๋๋ผ๋ฉด ์๋์ ๊ฐ์ด ์ฌ์ฉํ๋ฉด ๋๋ค. int x = Integer.parseInt(br.readLine()); if (x % k == 0) count++; } System.out.println(count); } }
์ค์ ๋๋์ ํ์ผ์ ์ถ๋ ฅ์์ ์๋๊ฐ ํ์ํ๋ค๋ฉด ํ์ผ์ ์ฌ๋ฌ๊ฐ๋ก ์ชผ๊ฐ ๋ฉํฐ์ฐ๋ ๋๋ฅผ ๋๋ฆฌ๋ฉด ๋๋ค. ์ด๋๋ PipedInputStream, PipedOutputStream์ ์ด์ฉํ๋ค.
- PipedInputStream() : ์์ง ์ฐ๊ฒฐ๋์ง ์์ PipedInputStream์ ์์ฑ.
- PipedInputStream(PipedOutputStream src) : PipedOutputStream์ ์ฐ๊ฒฐํ๋ PipedInputStream์ ์์ฑ.
- void connect(PipedOutputStream src) : PipedInputStream๊ณผ ์ฐ๊ฒฐํ PipedOutputStream src๋ฅผ ์ง์
- PipedOutputStream() : ์์ง ์ฐ๊ฒฐ๋์ง ์์ PipedOutputStream์ ์์ฑ.
- PipedOutputStream(PipedInputStream src) : PipedInputStream์ ์ฐ๊ฒฐํ๋ PipedOutputStream์ ์์ฑํฉ๋๋ค.
- void connect(PipedInputStream snk) : PipedOutputStream๊ณผ ์ฐ๊ฒฐํ PipedInputStream์ ์ง์ .
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class PipedInputOutputTest extends Thread { InputStream input; OutputStream output; PipedInputOutputTest(InputStream input, OutputStream output) { this.input = input; this.output = output; } public void run() { byte[] buf = new byte[1024]; int i = 0; try { while (true) { i = input.read(buf); if (i != -1) output.write(buf, 0, i); else return; } } catch (Exception e) { // ํ์ผ์ด ์กด์ฌํ์ง ์์ ๋ ๋ฑ๋ฑ์ ์์ธ์ฒ๋ฆฌ } } public static void main(String[] args) throws IOException { PipedInputStream pis = new PipedInputStream(); PipedOutputStream pos = new PipedOutputStream(pis); // pis, pos ๊ฐ์ฒด๋ฅผ ๋ฐ๋ก ๋ง๋ค๊ณ pis.connect(pos)๋ก ์ฐ๊ฒฐํด์ฃผ์ด๋ ๋๋ค. PipedInputOutputTest p1 = new PipedInputOutputTest(System.in, pos); PipedInputOutputTest p2 = new PipedInputOutputTest(pis, System.out); p1.start(); p2.start(); } }
# ๋คํธ์ํฌ ์์ผ ํ๋ก๊ทธ๋๋ฐ
์ด ๋ด์ฉ์ ๋คํธ์ํฌ์ ๋ํ ์ง์์ด ์๋ค๋ฉด ์ฝ๊ณ , ์๋ค๋ฉด ์ด๋ ต๊ฒ ๋๊ปด์ง ๊ฒ์ด๋ค.
๊ธฐ๋ณธ์ง์
ํ ์ปดํจํฐ์์ ๋ค๋ฅธ ์ปดํจํฐ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์กํ ๋๋ OSI 7 Layer๋ฅผ ์ด์ฉํ์ฌ ์ํ๋ ์ ๋ณด๋ฅผ ์ ๋ฌํ๋ค.
๋ฐ์ดํฐ ํต์ ์ ํ๋๋ฐ OSI 7 Layer๊ฐ ์ฌ์ฉ๋๊ณ , ์ธํฐ๋ท ๊ธฐ๋ฐ์ผ๋ก ์๋ฑกํฅ ํต์ ์ ํ๊ธฐ์ํด IP์ ํฌํธ๋ฅผ ์ด์ฉํ TCP/IP 4๊ณ์ธต์ ์ด์ฉํ์ฌ ๋คํธ์ํฌ ํต์ ์ ํ๊ฒ๋๋ค.
- ๋คํธ์ํฌ๋?
1. ๋คํธ์ํฌ๋ ๋ค๋ฅธ ์ฅ์น๋ก ๋ฐ์ดํฐ๋ฅผ ์ด๋์ํฌ ์ ์๋ ์ปดํจํฐ๋ค๊ณผ ์ฃผ๋ณ ์ฅ์น๋ค์ ์งํฉ์ ๋๋ค.
2. ๋คํธ์ํฌ์ ์ฐ๊ฒฐ๋ ๋ชจ๋ ์ฅ์น๋ค์ ๋ ธ๋๋ผ๊ณ ํฉ๋๋ค.
3. ๋ค๋ฅธ ๋ ธ๋์๊ฒ ํ๋ ์ด์์ ์๋น์ค๋ฅผ ํด์ฃผ๋ ๋ ธ๋๋ฅผ ํธ์คํธ๋ผ ๋ถ๋ฆ ๋๋ค.
4. ํ๋์ ์ปดํจํฐ์์ ๋ค๋ฅธ ์ปดํจํฐ๋ก ๋ฐ์ดํฐ๋ฅผ ์ด๋์ํฌ ๋ ๋ณต์กํ ๊ณ์ธต์ ํตํด ์ ์ก๋๋๋ฐ, ์ด๋ฐ ๋ณต์กํ ๋ ์ด์ด์ ๋ํ์ ์ธ ๋ชจ๋ธ์ด OSI ๊ณ์ธต ๋ชจ๋ธ์ ๋๋ค.
5. OSI ๊ณ์ธต ๋ชจ๋ธ์ ๋ชจ๋ 7๊ณ์ธต์ผ๋ก ์ด๋ฃจ์ด์ ธ ์์ต๋๋ค.
6. ๋ฐ์ดํฐ ํต์ ์ ์ดํดํ๋๋ฐ OSI ๊ณ์ธต ๋ชจ๋ธ์ ์๋นํ ์ญํ ์ ํ์ง๋ง, ์ธํฐ๋ท ๊ธฐ๋ฐ์ ํ์ค ๋ชจ๋ธ๋ก ์ฌ์ฉํ๋ TCP/IP ๊ณ์ธต ๋ชจ๋ธ์ ์ฃผ๋ก ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
7. ์๋ฐ์์ ์ด์ผ๊ธฐํ๋ ๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ์ TCP/IP๋ชจ๋ธ์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
2021.01.04 - [Front/๊ธฐ๋ณธ ์ง์] - 1. ์ธํฐ๋ท์ ๋์์๋ฆฌ
1. ์ธํฐ๋ท์ ๋์์๋ฆฌ
์น์ฌ์ดํธ์ ๋์ ์๋ฆฌ๋ฅผ ์ดํดํ๊ธฐ ์ , ์ธํฐ๋ท์ด ์ด๋ป๊ฒ ๋คํธ์ํฌ๋ฅผ ๊ตฌ์ฑํ๋์ง ๊ฐ๋จํ๊ฒ ์์๋ณด๊ณ ์ ํ๋ค. 1. Web ์ฐธ๊ณ ๋ก ์น์ World Wide Web์ ์ค์ฌ ๋ถ๋ฅด๋ ๊ฒ์ผ๋ก 1989๋ ํ ๋ฒ๋์ค๋ฆฌ๊ฐ ์ฐ๊ตฌ์๊ฐ ์
jiwondev.tistory.com
์๋ฐ์์๋ TCP Socket ๊ฐ์ฒด๋ฅผ ํ์ฉํ์ฌ Client ์๋ฐ ์ฑ - Server ์๋ฐ ์ฑ ๊ฐ์ ๋คํธ์ํฌ๋ฅผ ๊ตฌ์ฑํ ์ ์๋ค.

1. Server์ธก์์๋ ServerSocket์ ์์ฑํ๊ณ accept() ๋ฉ์๋๋ฅผ ํธ์ถํจ์ผ๋ก์จ Client์ ์ ์์ ๋๊ธฐํฉ๋๋ค.
2. Client์ธก์์๋ Server์ ์ ์์ ํจ์ผ๋ก์จ Server์์ ํต์ ์ ์ํ Socket์ ์์ฑํฉ๋๋ค.
3. ๋ง์ฐฌ๊ฐ์ง๋ก Server์ธก์์ Client ์ ์์ด ์ด๋ฃจ์ด์ง๋ฉด ํด๋น Client์ ํต์ ํ ์ ์๋ Socket์ ๋ฐํ๋ฐ์ต๋๋ค.
4. Clinet์ Server๋ ์์ฑ๋ ์์ผ์ ํตํ์ฌ ๊ฐ๊ฐ ์๋์๊ฒ ๋ฐ์ดํฐ๋ฅผ ๋ด๋ณด๋ด๊ธฐ ์ํ ์ถ๋ ฅ ์คํธ๋ฆผ๊ณผ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด ๋ค์ด๊ธฐ ์ํ ์
๋ ฅ ์คํธ๋ฆผ์ ์์ฑํฉ๋๋ค.
5. ์์ฑ๋ ์คํธ๋ฆผ์ ํตํ์ฌ Server/Client ์๋ก๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ก์์ ํฉ๋๋ค.
6. ํต์ ์ด ๋๋๋ฉด Client์ Server์ธก์์ ๊ฐ๊ฐ socket.close()๋ฅผ ํด์ค์ผ๋ก์จ ํต์ ์ ์ข
๋ฃํ๊ฒ ๋ฉ๋๋ค.
Server
package Threads; import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String arg[]) { Socket socket = null; //Client์ ํต์ ํ๊ธฐ ์ํ Socket ServerSocket server_socket = null; //์๋ฒ ์์ฑ์ ์ํ ServerSocket BufferedReader in = null; //Client๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด๋ค์ด๊ธฐ ์ํ ์
๋ ฅ์คํธ๋ฆผ PrintWriter out = null; //Client๋ก ๋ฐ์ดํฐ๋ฅผ ๋ด๋ณด๋ด๊ธฐ ์ํ ์ถ๋ ฅ ์คํธ๋ฆผ try { server_socket = new ServerSocket(8888); } catch (IOException e) { System.out.println("ํด๋น ํฌํธ๊ฐ ์ด๋ ค์์ต๋๋ค."); } try { System.out.println("์๋ฒ ์คํ!!"); socket = server_socket.accept(); //์๋ฒ ์์ฑ , Client ์ ์ ๋๊ธฐ in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //์
๋ ฅ์คํธ๋ฆผ ์์ฑ out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))); //์ถ๋ ฅ์คํธ๋ฆผ ์์ฑ String str = null; str = in.readLine(); //Client๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ด System.out.println("Client๋ก ๋ถํฐ ์จ ๋ฉ์ธ์ง : " + str); out.write(str); out.flush(); socket.close(); } catch (IOException e) { } } }
Client
import java.io.*; import java.net.InetAddress; import java.net.Socket; public class Client { public static void main(String[] arg) { Socket socket = null; //Server์ ํต์ ํ๊ธฐ ์ํ Socket BufferedReader in = null; //Server๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด๋ค์ด๊ธฐ ์ํ ์
๋ ฅ์คํธ๋ฆผ BufferedReader in2 = null; //ํค๋ณด๋๋ก๋ถํฐ ์ฝ์ด๋ค์ด๊ธฐ ์ํ ์
๋ ฅ์คํธ๋ฆผ PrintWriter out = null; //์๋ฒ๋ก ๋ด๋ณด๋ด๊ธฐ ์ํ ์ถ๋ ฅ ์คํธ๋ฆผ InetAddress ia = null; try { ia = InetAddress.getByName("์๋ฒ ์ฃผ์ ์
๋ ฅ"); //์๋ฒ๋ก ์ ์ socket = new Socket(ia, 8888); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); in2 = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))); System.out.println(socket.toString()); } catch (IOException e) { } try { System.out.print("์๋ฒ๋ก ๋ณด๋ผ ๋ฉ์ธ์ : "); String data = in2.readLine(); //ํค๋ณด๋๋ก๋ถํฐ ์
๋ ฅ out.println(data); //์๋ฒ๋ก ๋ฐ์ดํฐ ์ ์ก out.flush(); String str2 = in.readLine(); //์๋ฒ๋ก๋ถํฐ ๋๋์์ค๋ ๋ฐ์ดํฐ ์ฝ์ด๋ค์ System.out.println("์๋ฒ๋ก๋ถํฐ ๋๋์์จ ๋ฉ์ธ์ง : " + str2); socket.close(); } catch (IOException e) { } } }
๋ธ๋ก๊ทธ์ ์ ๋ณด
JiwonDev
JiwonDev