Hello there! I'm trying to send a UDP message usin...
# ktor
k
Hello there! I'm trying to send a UDP message using ktor and getting this exception
Copy code
Exception in thread "main" java.net.BindException: Can't assign requested address
	at java.base/sun.nio.ch.DatagramChannelImpl.send0(Native Method)
	at java.base/sun.nio.ch.DatagramChannelImpl.sendFromNativeBuffer(DatagramChannelImpl.java:587)
	at java.base/sun.nio.ch.DatagramChannelImpl.send(DatagramChannelImpl.java:549)
	at java.base/sun.nio.ch.DatagramChannelImpl.send(DatagramChannelImpl.java:532)
	at io.ktor.network.sockets.DatagramSendChannel$send$2$1.invokeSuspend(DatagramSendChannel.kt:80)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
This is my code:
Copy code
fun main() {
	runBlocking {
	    val selectorManager = SelectorManager(<http://Dispatchers.IO|Dispatchers.IO>)
        val socket = aSocket(selectorManager)
            .udp()
            .bind(InetSocketAddress("127.0.0.1", 4756))

        launch(<http://Dispatchers.IO|Dispatchers.IO>) {
            for (datagram in socket.incoming) {
                val bytes = datagram.packet.readBytes()
                val decoded = decode(bytes)
                <http://logger.info|logger.info>("Received: $decoded from ${datagram.address}")
            }
        }


        val packet = BytePacketBuilder().apply { request }.build() // Here I create the packet to send, just raw bytes, nothing related to the problem
        val message = Datagram(
            packet = packet,
            address = InetSocketAddress("<http://router.bittorrent.com|router.bittorrent.com>", 6881)
        )

        socket.send(message)
	}
}
No idea why this is happening, can any one help me please?
a
Can you try to change the
127.0.0.1
address in the
.bind(InetSocketAddress("127.0.0.1", 4756))
to
0.0.0.0
?
k
@Aleksei Tirman [JB] wow, that helped, thanks a lot! Could you please give an axplanation why?
a
Unfortunately, I don’t understand the problem. I’ve just found this answer.
🙏 1
544 Views