val file = "${Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)}" + "/test.mp3"
val fileInputStream = FileInputStream(file)
val buffer = ByteArray(1024)
var readBytes = fileInputStream.read(buffer) // file에서 buffer 크기만큼 읽어 buffer에 저장하고 실제 읽은 Byte 수 반환
val socket = Socket(IP, Port) // e.g. Socket("192.168.100.100", 10000)
val outputStream = socket.outputStream
var totalReadBytes = 0L
while (readBytes > 0) { // 더 이상 file에서 읽을 Byte가 없을 때까지 반복
outputStream.write(buffer, 0, readBytes) // buffer[0] 부터 readBytes 만큼 데이터 전송
totalReadBytes += readBytes
Log.d("test", "totalReadBytes: $totalReadBytes")
readBytes = fileInputStream.read(buffer)
}
Log.d("test", "end")
fileInputStream.close()
outputStream.close()
socket.close()