Sort to prefer ipv4 over ipv6.

This commit is contained in:
Koen J 2025-06-06 12:25:25 +02:00
commit 10753eb879
2 changed files with 6 additions and 3 deletions

View file

@ -241,8 +241,11 @@ fun getConnectedSocket(attemptAddresses: List<InetAddress>, port: Int): Socket?
return null; return null;
} }
val sortedAddresses: List<InetAddress> = addresses
.sortedBy { addr -> addressScore(addr) }
val sockets: ArrayList<Socket> = arrayListOf(); val sockets: ArrayList<Socket> = arrayListOf();
for (i in addresses.indices) { for (i in sortedAddresses.indices) {
sockets.add(Socket()); sockets.add(Socket());
} }
@ -250,7 +253,7 @@ fun getConnectedSocket(attemptAddresses: List<InetAddress>, port: Int): Socket?
var connectedSocket: Socket? = null; var connectedSocket: Socket? = null;
val threads: ArrayList<Thread> = arrayListOf(); val threads: ArrayList<Thread> = arrayListOf();
for (i in 0 until sockets.size) { for (i in 0 until sockets.size) {
val address = addresses[i]; val address = sortedAddresses[i];
val socket = sockets[i]; val socket = sockets[i];
val thread = Thread { val thread = Thread {
try { try {

View file

@ -434,7 +434,7 @@ private fun interfaceScore(nif: NetworkInterface): Int {
} }
} }
private fun addressScore(addr: InetAddress): Int { fun addressScore(addr: InetAddress): Int {
return when (addr) { return when (addr) {
is Inet4Address -> { is Inet4Address -> {
val octets = addr.address.map { it.toInt() and 0xFF } val octets = addr.address.map { it.toInt() and 0xFF }