Skip to content

Commit c26d3b9

Browse files
authored
Merge pull request #57 from jpolnetpl/prefer-wifi
Prefer IPv4 addresses on wlan* interfaces
2 parents c53af3e + 317ea79 commit c26d3b9

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

ShareViaHttp/app/src/main/java/com/MarcosDiez/shareviahttp/MyHttpServer.java

+20-7
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ public synchronized void stopServer() {
168168
}
169169

170170
public CharSequence[] listOfIpAddresses() {
171-
ArrayList<String> arrayOfIps = new ArrayList<String>();
171+
ArrayList<String> arrayOfIps = new ArrayList<String>(); // preferred IPs
172+
ArrayList<String> arrayOfIps4 = new ArrayList<String>(); // IPv4 addresses
173+
ArrayList<String> arrayOfIps6 = new ArrayList<String>(); // IPv6 addresses
174+
ArrayList<String> arrayOfIpsL = new ArrayList<String>(); // loopback
172175

173176

174177
try {
@@ -184,17 +187,27 @@ public CharSequence[] listOfIpAddresses() {
184187
String theIpTemp = inetAddress.getHostAddress();
185188
String theIp = getServerUrl(theIpTemp);
186189

187-
if (inetAddress instanceof Inet6Address
188-
|| inetAddress.isLoopbackAddress()) {
189-
190-
arrayOfIps.add(theIp);
190+
if (inetAddress instanceof Inet6Address) {
191+
arrayOfIps6.add(theIp);
192+
continue;
193+
} else if (inetAddress.isLoopbackAddress()) {
194+
arrayOfIpsL.add(theIp);
195+
continue;
196+
} else if (intf.getDisplayName().matches("wlan.*")) {
197+
arrayOfIps.add(0,theIp); // prefer IPv4 on wlan interface
198+
continue;
199+
} else {
200+
arrayOfIps4.add(theIp);
191201
continue;
192202
}
193-
194-
arrayOfIps.add(0, theIp); // we prefer non local IPv4
195203
}
196204
}
197205

206+
// append IP lists in order of preference
207+
arrayOfIps.addAll(arrayOfIps4);
208+
arrayOfIps.addAll(arrayOfIps6);
209+
arrayOfIps.addAll(arrayOfIpsL);
210+
198211
if (arrayOfIps.size() == 0) {
199212
String firstIp = getServerUrl("0.0.0.0");
200213
arrayOfIps.add(firstIp);

0 commit comments

Comments
 (0)