Friday 11 December 2020

java.io.IOException: socket not created while trying to write a response to a socket

I have a server and client — regular TCP socket or, in my case, LocalSocket (UNIX domain).

From the client side I want to:

  • Connect to the server
  • Send binary data
  • Receive binary data

And it should be done synchronously.

My server implementation:

                    socket
                        .accept()
                        ?.let { clientSocket ->
                            thread {
                                clientSocket
                                    .inputStream
                                    .use { it.readBytes() }
                                    .let(::processRequest)
                                    .let { data ->
                                        clientSocket.outputStream.use { it.write(data) } // <-- 1
                                    }
                            }
                        }

Client:

            socket.connect(LocalSocketAddress(LocalSocketConstants.SOCKET_NAME))
            socket.soTimeout = SOCKET_TIMEOUT
            socket.outputStream.use { it.write(data) }
            socket.inputStream.use { it.readBytes() } // <-- 2

It works fine until I try to write the response (see "1" above), as it's throwing an exception:

    java.io.IOException: socket not created
        at android.net.LocalSocketImpl.getOutputStream(LocalSocketImpl.java:373)
        at android.net.LocalSocket.getOutputStream(LocalSocket.java:200)
        at my.package.config.domain.ConfigServerLocalSocketImpl$ServerThread$run$$inlined$let$lambda$1.invoke(ConfigServer.kt:120)
        at my.package.config.domain.ConfigServerLocalSocketImpl$ServerThread$run$$inlined$let$lambda$1.invoke(ConfigServer.kt:90)
        at kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:30)

And I suppose that it may be caused by an exception in (see "2" above):

java.io.IOException: socket not created
        at android.net.LocalSocketImpl.getInputStream(LocalSocketImpl.java:352)
        at android.net.LocalSocket.getInputStream(LocalSocket.java:189)
        at my.package.config.domain.ConfigClientImpl.sendRequest(ConfigClient.kt:36)
        at my.package.config.domain.interactor.CanLaunchAppImpl.exec(CanLaunchApp.kt:16)
        at my.package.testapp.MainActivity.testClient(MainActivity.kt:55)
        at my.package.testapp.MainActivity.onCreate(MainActivity.kt:39)
        at android.app.Activity.performCreate(Activity.java:7144)
        at android.app.Activity.performCreate(Activity.java:7135)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

I believe that I've made some very stupid mistake, but I can't find it. What's wrong with this code?



from java.io.IOException: socket not created while trying to write a response to a socket

No comments:

Post a Comment