Socket call in CoroutineScope?

Which would be the right way to make a TCP request using Socket in kotlin by using CoroutineScope?

by using GlobalScope.async the call works properly but i get the inspector errors on socket which says: Inappropriate blocking method call

The socket call should be done from a handler function and i was trying to do it by using Coroutine IO but when i try to run it i get the error:

FATAL EXCEPTION: DefaultDispatcher-worker-1

Here is my code:

private fun handlePhoneCall(
    response: CallResponse.Builder,
    phoneNumber: String
): CallResponse.Builder {
    val ipCassa = PreferenceManager.getDefaultSharedPreferences(this).getString("server", "127.0.0.1")

    GlobalScope.launch(IO) {
        Socket(ipCassa, 50000).use { socket: Socket ->
            val outputStream = socket.getOutputStream()
            outputStream.write(data.toByteArray())
            outputStream.flush()
        }.runCatching {

        }.onFailure {
            Log.e("SOCKET", it.message.toString())
        }
    }

    return response
}

Answers 1

  • Use withContext(Dispatchers.IO) {} can get rid of the Inappropriate blocking method call warning. Following is my example of using Socket with my receipt printer:

    In ViewModel:

    class FirstViewModel : ViewModel() {
        private val uiScope = viewModelScope
    
        fun print() {
            uiScope.launch {
                //show loading dialog
    
                PrintService.print()  //suspend function, won't run next line until it finishes
    
                //hide loading dialog
            }
        }
    }
    

    In Util Class:

    object PrintService {
        suspend fun print() {
            withContext(Dispatchers.IO) {
                try {
                    val socket = Socket("192.168.1.111", 9100)
                    val outputStream = socket.getOutputStream()
    
                    outputStream.use {
                        it.write("xxx".toByteArray())
                    }
                } catch (e: IOException) {
                    //log your error
                }
            }
        }
    }
    

    Demo: https://youtu.be/U0RkzWesgnc


    Helpful reading:


Related Articles