JiwonDev

#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) {
        }
    }
}

'๐ŸŒฑBackend > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

#11 Stream API  (0) 2021.07.12
#10 Modern Java (Java8~)  (0) 2021.07.12
#7 Exception (์˜ˆ์™ธ์ฒ˜๋ฆฌ)  (0) 2021.07.07
#6. Java - 2 ํด๋ž˜์Šค (์ž‘์„ฑ์˜ˆ์ •)  (0) 2021.07.07
#5. ์ž๋ฐ”์˜ Interface  (2) 2021.07.01

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ