如何使用 Kotlin 使用协程扫描本地 IP 地址

发布于 2025-01-10 17:31:23 字数 1408 浏览 1 评论 0原文

我有一个应用程序可以扫描本地 IP 地址以连接到开放端口 8102。我已经能够获取正确的 IP 地址,但需要很长时间,因为每次轮询的超时时间为 200 毫秒。这是我成功达到的最低水平。

我想我的问题是有没有办法使用协程来分割工作并更快地获取地址?现在大约需要 3 秒,而我的目标地址仅为 192.168.0.21。

这是我的代码:

fun init() = GlobalScope.launch(Dispatchers.IO) {
            //Get local ip
            DatagramSocket().use { socket ->
                socket.connect(InetAddress.getByName("8.8.8.8"), 10002)
                ip = socket.getLocalAddress().getHostAddress().split(".") as MutableList<String>
            }
            //Go through local addresses to find receiver
            txtOutput.text = ip.toString()
            prefix = ip[0] + "." + ip[1] + "." + ip[2] + "."

            var i = 1
            do {
                try {
                    client = Socket()
                    client.connect(InetSocketAddress(prefix + i.toString(), 8102), 200)
                } catch (e: Exception) {
                    print(e.toString())
                    i++
                }
            } while (!(client.isConnected) or (i > 254))
            targetIP = prefix + i.toString()
            client = Socket()
            try{
            client.connect(InetSocketAddress(targetIP, 8102), 150)
                if(client.isConnected){
                client.keepAlive = true}}
            catch (e:IOException){
                cancel("Could not connect")
            }

I have an app that scans local IP addresses to connect to open port 8102. I've been able to get the correct IP address, but it takes a long time because each poll has a timeout of 200 milliseconds. That's the lowest I've been able to get it with success.

I guess my question is is there a way to use coroutines to split up the work and get the address sooner? Right now it's taking about 3 seconds and the address I'm targeting is only 192.168.0.21.

Here's my code:

fun init() = GlobalScope.launch(Dispatchers.IO) {
            //Get local ip
            DatagramSocket().use { socket ->
                socket.connect(InetAddress.getByName("8.8.8.8"), 10002)
                ip = socket.getLocalAddress().getHostAddress().split(".") as MutableList<String>
            }
            //Go through local addresses to find receiver
            txtOutput.text = ip.toString()
            prefix = ip[0] + "." + ip[1] + "." + ip[2] + "."

            var i = 1
            do {
                try {
                    client = Socket()
                    client.connect(InetSocketAddress(prefix + i.toString(), 8102), 200)
                } catch (e: Exception) {
                    print(e.toString())
                    i++
                }
            } while (!(client.isConnected) or (i > 254))
            targetIP = prefix + i.toString()
            client = Socket()
            try{
            client.connect(InetSocketAddress(targetIP, 8102), 150)
                if(client.isConnected){
                client.keepAlive = true}}
            catch (e:IOException){
                cancel("Could not connect")
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萧瑟寒风 2025-01-17 17:31:23

理论上是可以的,但是需要测试一下

fun init() = GlobalScope.launch(Dispatchers.IO) {
    //Get local ip
    DatagramSocket().use { socket ->
        socket.connect(InetAddress.getByName("8.8.8.8"), 10002)
        ip = socket.getLocalAddress().getHostAddress().split(".") as MutableList<String>
    }
    //Go through local addresses to find receiver
    txtOutput.text = ip.toString()
    prefix = ip[0] + "." + ip[1] + "." + ip[2] + "."

//    var i = 1
//    do {
//        try {
//            client = Socket()
//            client.connect(InetSocketAddress(prefix + i.toString(), 8102), 200)
//        } catch (e: Exception) {
//            print(e.toString())
//            i++
//        }
//    } while (!(client.isConnected) or (i > 254))
    
    ///#########
    val answer= Channel<Int>()

    for (i in 0..254){
        launch {
            try { 
                client = Socket()
                client.connect(InetSocketAddress(prefix + i.toString(), 8102), 200)
                answer.send(i)
            }catch (e:Exception){ }

        }
    }
    val i=answer.receive()
    ///#########

    targetIP = prefix + i.toString()
    client = Socket()
    try {
        client.connect(InetSocketAddress(targetIP, 8102), 150)
        if (client.isConnected) {
            client.keepAlive = true
        }
    } catch (e: IOException) {
        cancel("Could not connect")
    }

}

It is theoretically possible but you have to test it

fun init() = GlobalScope.launch(Dispatchers.IO) {
    //Get local ip
    DatagramSocket().use { socket ->
        socket.connect(InetAddress.getByName("8.8.8.8"), 10002)
        ip = socket.getLocalAddress().getHostAddress().split(".") as MutableList<String>
    }
    //Go through local addresses to find receiver
    txtOutput.text = ip.toString()
    prefix = ip[0] + "." + ip[1] + "." + ip[2] + "."

//    var i = 1
//    do {
//        try {
//            client = Socket()
//            client.connect(InetSocketAddress(prefix + i.toString(), 8102), 200)
//        } catch (e: Exception) {
//            print(e.toString())
//            i++
//        }
//    } while (!(client.isConnected) or (i > 254))
    
    ///#########
    val answer= Channel<Int>()

    for (i in 0..254){
        launch {
            try { 
                client = Socket()
                client.connect(InetSocketAddress(prefix + i.toString(), 8102), 200)
                answer.send(i)
            }catch (e:Exception){ }

        }
    }
    val i=answer.receive()
    ///#########

    targetIP = prefix + i.toString()
    client = Socket()
    try {
        client.connect(InetSocketAddress(targetIP, 8102), 150)
        if (client.isConnected) {
            client.keepAlive = true
        }
    } catch (e: IOException) {
        cancel("Could not connect")
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文