https://kotlinlang.org logo
#announcements
Title
# announcements
j

jkbbwr

10/31/2017, 2:27 PM
Anyone know why I am not getting messages from a multicast socket?
Copy code
package io.codehop.fx

import java.net.DatagramPacket
import java.net.InetAddress
import java.net.MulticastSocket
import kotlin.concurrent.thread

val group = InetAddress.getByName("224.1.1.1")

fun server() {
    val socket = MulticastSocket(5001)
    socket.joinGroup(group)
    val buf = ByteArray(1024)
    while (true) {
        val packet = DatagramPacket(buf, buf.size)
        socket.receive(packet)
        println(packet)
    }
}

fun client(message: String) {
    val socket = MulticastSocket()
    socket.joinGroup(group)
    val bytes = message.toByteArray()
    val packet = DatagramPacket(bytes, bytes.size, group, 5001)
    socket.send(packet)
}

fun main(args: Array<String>) {
    val serverThread = thread {
        server()
    }
    Thread.sleep(1000)
    thread {
        client("hi bob")
        print("sent")
    }

    serverThread.join()
}
stackoverflow 2
3 Views