io stream ๊ณผ socket ์ ์ด์ฉํ tcp ํต์ ์ java ๋ฅผ ์ฒ์ ๊ณต๋ถํ ๋, ๊ฐ๋จํ๊ฒ ๋ฉํฐ ์ค๋ ๋ ์ฑํ ์ ๋์ ์์ ๋ก ์ดํดํ๋ฉฐ ๋์ด๊ฐ๋ค..
์ฌ๋งํ ํด๋ผ์ด์ธํธ - ์๋ฒ ๊ฐ์ ํต์ ์ http ๋ก ์ด๋ฃจ์ด์ง๋ฏ๋ก ์ด๋ฅผ ๋ ๊น๊ฒ ํ๋ฉฐ ๊ณต๋ถํ์๋ค.
์ด๋ฒ์ ์ด์งํ ํ์ฌ์์ ๋ณธ๊ฒฉ์ ์ผ๋ก ๋งก๊ฒ ๋ ์ฒซ ๊ณผ์ ๋ TCP ์ ๋ฌธ ํต์ ์ ๊ตฌ์ถํ๋ ๊ฒ์ด๋ค.
์์ผ ํต์ ์ ๊ตฌํํ๋๋ฐ, java socket api ๋ฅผ ์ด์ฉํ์ฌ ์์ ๊ตฌํํ ์ ์์ง๋ง, kotlin ์์๋ okio ๋ผ๋ ํต์ ์ ์ฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์๋ค๊ณ ํด์ ์ด๋ฅผ ํ๋ฒ ์ฌ์ฉํด์ ์ ๋ฌธ ํต์ ์ ์ํ ์์ผ ํด๋ผ์ด์ธํธ ์ด์์ ๊ตฌํํด๋ดค๋ค.
์ ํด๋ณด์ง ์์ socket ํต์ ์ ์ดํดํ๊ณ ๊ตฌํํ๋ฉฐ ์ดํ๊ฐ ๋ง์ด ๊ณ ํต ๋ฐ์๊ณ , ๊ทธ ๊ณผ์ ์์ ์ข ๋ ํธ๋ฆฌํ kotlin okio ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋จํ๊ฒ ์์ผ ํต์ ์ ํ ์ ์๋ ์์ ์ฝ๋๋ฅผ ๋ณด์.
ํน์๋ okio ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ byte ๋ฐฐ์ด socket ํต์ ์ ํ์๋ ค๋ ๋ถ๋ค์๊ฒ ๋์์ด ๋๊ธธ ๋ฐ๋๋ค.
์ฐธ๊ณ [Okio ๋ผ์ด๋ธ๋ฌ๋ฆฌ]
https://github.com/square/okio
GitHub - square/okio: A modern I/O library for Android, Java, and Kotlin Multiplatform.
A modern I/O library for Android, Java, and Kotlin Multiplatform. - GitHub - square/okio: A modern I/O library for Android, Java, and Kotlin Multiplatform.
github.com
https://square.github.io/okio/
Okio
Okio Okio is a library that complements java.io and java.nio to make it much easier to access, store, and process your data. It started as a component of OkHttp, the capable HTTP client included in Android. Itโs well-exercised and ready to solve new prob
square.github.io
Server ์ฝ๋
import okio.buffer
import okio.sink
import okio.source
import java.net.ServerSocket
import java.util.concurrent.TimeUnit
fun main() {
Server().server()
}
class Server {
fun server() {
val serverSocket = ServerSocket(9999)
while (true) {
val client = serverSocket.accept()
val bufferedSource = client.source().buffer() // ๋ด๋ถ์ ์ผ๋ก getInputStream() ์ ํธ์ถํ๋ค.
bufferedSource.timeout().timeout(READ_TIME_OUT, TimeUnit.MILLISECONDS)
val readByteArray = bufferedSource.readByteArray() // byte ๋ฐฐ์ด๋ก ์ฝ๊ธฐ
println(String(readByteArray))
val bufferedSink = client.sink().buffer() // ๋ด๋ถ์ ์ผ๋ก getOutputStream() ์ ํธ์ถํ๋ค.
bufferedSink.timeout().timeout(WRITE_TIME_OUT, TimeUnit.MILLISECONDS)
bufferedSink.write(readByteArray) // ์์ฝ ์๋ฒ ๋ง๋ฅ ๋ฐ์ byte ๋ฐฐ์ด์ ํด๋ผ์ด์ธํธ๋ก ๋ณด๋ด๊ธฐ
bufferedSink.flush() // flush ๋ฅผ ํธ์ถํด์ buffer ๋ฅผ ๋น์ฐ์.
client.shutdownOutput() // eof ๋ง๋ฅ, write ์ด ๋๋ฌ๋ค๋ ๊ฒ์ socket ์ shutdownOutput() ์ ํธ์ถํด์ผ ํ๋ค.
}
}
}
- ์์์ close() ํจ์๋ฅผ ๋ช
์ํ์ง ์์๋ค.
- ์ค์ ์ฝ๋์์๋ ํธ์ถํด์ผ.
- okio ์์๋ input, output stream ๊ฐ์ ๊ฒ์ ํ๋ฒ ๋ํํ์ฌ ์ถ์ํํ๋ค.
- source, sink ๋ผ๋ ๊ฐ์ฒด๋ก ์ถ์ํ
![](http://t1.daumcdn.net/tistory_admin/static/images/xBoxReplace_250.png)
![](http://t1.daumcdn.net/tistory_admin/static/images/xBoxReplace_250.png)
Client ์ฝ๋
import okio.buffer
import okio.sink
import okio.source
import java.io.IOException
import java.net.InetSocketAddress
import java.net.Socket
import java.util.concurrent.TimeUnit
const val SOCKET_CONNECT_TIME_OUT = 5000
const val WRITE_TIME_OUT: Long = 12000
const val READ_TIME_OUT: Long = 12000
fun main() {
print(
String(
requestToTrsServer(
niceVanIp = "127.0.0.1",
niceVanPort = 9999,
buffer = "Hello Server!".toByteArray()
)
)
)
}
fun requestToTrsServer(niceVanIp: String, niceVanPort: Int, buffer: ByteArray): ByteArray {
Socket().use { socket ->
try {
socket.connect(InetSocketAddress(niceVanIp, niceVanPort), SOCKET_CONNECT_TIME_OUT)
val bufferedSink = socket.sink().buffer()
with(bufferedSink) {
timeout().timeout(WRITE_TIME_OUT, TimeUnit.MILLISECONDS)
write(buffer)
flush()
socket.shutdownOutput()
}
// ์๋ฒ๋ก bytearray ๋ณด๋ด๊ธฐ
val bufferedSource = socket.source().buffer()
val responseBytes =
with(bufferedSource) {
timeout().timeout(READ_TIME_OUT, TimeUnit.MILLISECONDS)
readByteArray()
}
// ์๋ฒ์์ bytearray ๋ฐ๊ธฐ
bufferedSink.close()
bufferedSource.close()
return responseBytes
} catch (e: IOException) {
println("์์ผ ์๋ฒ์ TCP ํต์ ์ค IOException ๋ฐ์")
return ByteArray(1)
}
}
}
- ํด๋ผ์ด์ธํธ๋ ๋ง์ฐฌ๊ฐ์ง
- bytearray ๋ฅผ ๋ณด๋ด๊ณ , ๋ฐ๋ ์ฝ๋์ด๋ค.
- ์์ ๊ฐ๋ ๊ณผ ๋์ผํ๊ฒ, source, sink ๋ฅผ Socket ๊ฐ์ฒด์์ ์ป์ ์ ์๋ค.
![](http://t1.daumcdn.net/tistory_admin/static/images/xBoxReplace_250.png)
์ด๋ ๊ฒ, java ์ฝ๋๋ณด๋ค ํจ์ฌ ๊ฐ๊ฒฐํ๊ฒ ์์ฑํ ์ ์๋ค.
์์ผ ํต์ ์ ์ ๋๋ก ์ฒ์ ๊ตฌํํด๋ดค๋๋ฐ, ์ญ์ ๊ณต๋ถํ ๊ฑด ๋์ด ์๊ณ , ํ์ ์๋ ๊ฒ์ ์ด๋์๋ ์๋ค.
"์ด๊ฒ๋ง ํ๋ฉด ๋์ง~"
๋ผ๋ ๋ง์ธ๋๋ ๊ฐ๋ฐ์๋ก์๋ ๋ถ์ ํฉํ ๋ง์ธ๋ ๊ฐ๋ค.
ํ์ํ ๋ชจ๋ ๊ฒ์ ์ํฉ์ ๋ฐ๋ผ ํ์ตํ๊ณ ์ ์ฉํ ์ ์์ด์ผ ํ๋ค.