Kotlin将字节数组写入BufferedWriter套接字

时间:2019-01-16 06:19:20

标签: android kotlin

我要发送的字节是TCP套接字。
数据格式为字节数组。

但是BufferedWrite.write()没有ByteArray。它具有intCharArrayString 这是我的代码。

    socket = Socket("192.168.9.34", 5000)
    networkReader = BufferedReader(InputStreamReader(socket!!.getInputStream()))
    networkWriter = BufferedWriter(OutputStreamWriter(socket!!.getOutputStream()))

    val json = """
        {"type":"REQ","code":"DP1200","key":"e7aa7f5e-15df-4ea1-9e7b-e4f05d2ac288","ip":"192.168.9.33","port":"31147","serviceid":"WINIXRND1D"}
    """.trimIndent()
    val jsonByte = json.toString().toByteArray()
    val header: ByteArray = byteArrayOf(0x76,0x31,0x30,0x30,0x0,0x0,0x0,jsonByte.size.toByte())
    val payload: ByteArray = header + jsonByte 
    try {
        networkWriter!!.write(payload)  //<== I want to write byte arra here!  but write's argument no ByteArray
    } catch (e: Exception) {
    }

请正确地给我建议。

0 个答案:

没有答案