TCP Channel (Blocking)

NIO์˜ tcp๋Š” blocking, non-blocking, asyncronized์€ ์„œ๋กœ ๊ตฌํ˜„๋ฐฉ์‹์ด ์ „ํ˜€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

Blocking Socket

SocketChannel

ServerSocketChannel : buffer X blocking ๋ฐฉ์‹

SocketChannel : buffer O, blocking, non-blocking ๋ฐฉ์‹

Drawing

Connection

Server

ServerSocket์„ ์ƒ์„ฑํ•œ ๋’ค blocking์œผ๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(INET);
serverSocketChannel.configureBlocking(true);
serverSocketChannel.bind(new InetSocketAddress(8877));

ํด๋ผ์ด์–ธํŠธ์˜ ์—ฐ๊ฒฐ์„ ์Šน์ธํ•˜๊ธฐ ์œ„ํ•œ accept()๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

SocketChannel accept = serverSocketChannel.accept();

(InetSocketAddress) socketChannel.getRemoteAddress(); ๋ฅผ์‚ฌ์šฉํ•˜๋ฉด ํด๋ผ์ด์–ธํŠธ์˜ ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์—ฐ๊ฒฐ์„ ์ข…๋ฃŒํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ close๋ฅผ ์‹คํ–‰ํ•ด ์ค๋‹ˆ๋‹ค.

serverSocketChannel.close();

Client

Socket์„ ์ƒ์„ฑํ•œ ๋’ค blocking์œผ๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

SocketChannel socketChannel = SocketChannel.open(INET);
socketChannel.configureBlocking(true);

์„œ๋ฒ„์— ์—ฐ๊ฒฐ ์š”์ฒญ์„ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด connet()๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

socketChannel.connect(new InetSocketAddress("localhost", 8877));

์—ฐ๊ฒฐ ์ข…๋ฃŒ๋Š” close๋ฅผ ์‹คํ–‰ํ•ด ์ค๋‹ˆ๋‹ค.

if (socketChannel.isConnected()) {
    socketChannel.close();
}

how do code?

Server

public class Main {
    public static void main(String[] args) throws IOException{
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(INET);
        serverSocketChannel.configureBlocking(true);
        serverSocketChannel.bind(new InetSocketAddress(8877));

        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();
            InetSocketAddress inetSocketAddress = (InetSocketAddress) socketChannel.getRemoteAddress();

            System.out.println("CONNECTED !!! " + inetSocketAddress.getHostName());
        }
    }
}

Client

public class Main {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open(INET);
        socketChannel.configureBlocking(true);

        socketChannel.connect(new InetSocketAddress("localhost", 8877));
        System.out.println("COMPLETE CONNECT");
        if (socketChannel.isConnected()) {
            socketChannel.close();
            System.out.println("COMPLETE CLOSE");
        }
    }
}

Data Communication

Client์˜ ์—ฐ๊ฒฐ ์š”์ฒญ์ด Server์—์„œ ์ˆ˜๋ฝ๋˜์—ˆ๋‹ค๋ฉด, read(), write()๋ฅผ ์ด์šฉํ•ด ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ชจ๋‘ ๋ฒ„ํผ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฒ„ํผ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Write

๋ฐ์ดํ„ฐ ์ „์†ก์€ write๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „์†กํ•œ๋‹ค.

๋ฐ”์ดํŠธ์ฝ”๋“œ๋กœ ์ „์†กํ•˜๋ฉด๋œ๋‹ค.

socketChannel.write(Charset.defaultCharset().encode("This is junny land"));

Read

์ „์†ก๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๋Š” ๊ฒƒ์€ Read๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๋ฐ”์ดํŠธ ์ฝ”๋“œ๋กœ ๋“ค์–ด์˜จ ๊ฐ’์„ ์ฝ์œผ๋ฉด ๋œ๋‹ค.

ByteBuffer buffer = ByteBuffer.allocate(1024);

socketChannel.read(buffer);
buffer.flip();
String result = Charset.defaultCharset().decode(buffer).toString();

read๋ฅผ ํ˜ธ์ถœํ•œ ์‹œ์ ๋ถ€ํ„ฐ ์ƒ๋Œ€๋ฐฉ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด์ฃผ๊ธฐ์ „ ๊นŒ์ง€๋Š” ํ•ญ์ƒ Blocking์ƒํƒœ๊ฐ€ ๋˜๋Š”๋ฐ,

๋ธ”๋กœํ‚น์ด ํ•ด์ œ๋˜๊ณ  ์‘๋‹ต์ด ์˜ค๋Š” ๊ฒฝ์šฐ๋Š” ์„ธ๊ฐ€์ง€ ์ž…๋‹ˆ๋‹ค.

reason
response

๋ฐ์ดํ„ฐ ์ „์†ก๋ฐ›์Œ

๋ฐ”์ดํŠธ ์ˆ˜

์ƒ๋Œ€์ธก close()

-1

๋น„์ •์ƒ์  ์ข…๋ฃŒ

IOException

Thread Pool?

๋™๊ธฐ ๋ฐฉ์‹์˜ ์„œ๋ฒ„ ์ฑ„๋„์˜ read๋Š” ์‘๋‹ต์„ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋™์•ˆ blocking๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์—…์ด ๋ŽŒ๋””๋‹ค.

์ด๋Ÿฐ ๋ฌธ์ œ๋Š”๋ณ‘๋ ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹ ์ค‘ Tread pool์„ ์ด์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

Drawing
this.executorService = newFixedThreadPool(getRuntime().availableProcessors());

executorService์— ์Šค๋ ˆ๋“œํ’€ ์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•ด ์ค€๋’ค

Runnable runnable = () -> {
    CharBuffer answer = Charset.defaultCharset().decode(BUFFER);
    System.out.println("READ ="+ answer);
};
executorService.submit(runnable);

Runnable์— ์‹คํ–‰ํ•  ์ž‘์—…์„ ์ž‘์„ฑํ•œ ๋’ค, executorService์— ๋“ฑ๋กํ•ด์ค€๋‹ค

Last updated