๐ŸŒŠ InputStream ๊ณผ OutputStream

๋‹จ์ผ ๋ฐฉํ–ฅ์œผ๋กœ ํ˜๋Ÿฌ๊ฐ€๋Š” ๋ฐ์ดํ„ฐ์˜ ํ๋ฆ„์„ ํ‘œํ˜„ํ•˜๋Š”๊ฒƒ์œผ๋กœ Stream์ด๋ผ๊ณ ํ•œ๋‹ค.

์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์˜ ์ถœ๋ฐœ์ง€ (ํ‚ค๋ณด๋“œ, ํŒŒ์ผ) ์—์„œ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์˜ ๋„์ฐฉ์ง€ (๋ชจ๋‹ˆํ„ฐ, ํŒŒ์ผ, ํ”„๋กœ๊ทธ๋žจ) ์˜ ๊ด€๊ณ„๋ฅผ ๋ˆ๋‹ค.

enter image description here

InputStream

Method descrption
read() inputstream ์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ์˜ byte๋ฅผ ์ฝ์–ด๋‚ธ๋‹ค
read(byte b[]) inputstream๊ณผ buffer ๋ฐฐ์—ด b ๋กœ๋ถ€ํ„ฐ byte๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด๋‚ธ๋‹ค

OutputStream

Method descrption
flush() ๋ฒ„ํผ์— ์ž”๋ฅ˜ํ•˜๋Š” ๋ชจ๋“  ๋ฐ”์ดํŠธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค
write(byte b[]) outputstream์ด ๊ฐ–๊ณ ์žˆ๋Š” ๋ฐ”์ดํŠธ ๋ฐฐ์—ด b๋ฅผ ๋ชจ๋‘ write ํ•œ๋‹ค

์†Œ์ผ“ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ์˜ I/O Stream


InputStream input  = socket.getInputStream();
OutputStream output = socket.getOutputStream();

while(true) {
	//5. ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ
	byte buffer[] = new byte[256];

	int readByteCount = input.read(buffer);//blocking.

	if(readByteCount == -1) {
		// ํด๋ผ์ด์–ธํŠธ ์ •์ƒ์ข…๋ฃŒ ํ•œ ๊ฒฝ์šฐ
		System.out.println("[server] closed by client.");
		break;
	}

	String data = new String(buffer, 0, readByteCount, "utf-8");
	System.out.println("[server] received " + data);

	//6. ๋ฐ์ดํ„ฐ ์“ฐ๊ธฐ
	output.write(data.getBytes("utf-8"));

	}

์†Œ์ผ“ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ์˜ IOStream์€ ์ƒ์„ฑํ•œ ์†Œ์ผ“์œผ๋กœ๋ถ€ํ„ฐ accept() ์ดํ›„ ์—ฐ๊ฒฐ์ด ๋˜๋ฉด ๊ฐ๊ฐ์˜ stream๊ฐ์ฒด๋ฅผ ์–ป๊ฒŒ๋˜๊ณ  read/write Method๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ ํ†ต์‹ ์ด ์ด๋ฃจ์–ด์ง„๋‹ค.