Skip to content

Commit

Permalink
Implement basic fallback behavior for OSX and WinXP SP2
Browse files Browse the repository at this point in the history
  • Loading branch information
GICodeWarrior committed Jan 30, 2011
1 parent e868628 commit 31b3735
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/simpleserver/Player.java
Expand Up @@ -21,8 +21,10 @@
package simpleserver;

import java.io.IOException;
import java.net.BindException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

Expand Down Expand Up @@ -437,8 +439,13 @@ public void run() {

private static final class LocalAddressFactory {
private static final int[] octets = { 0, 0, 1 };
private static Boolean canCycle = null;

private synchronized String getNextAddress() {
if (!canCycle()) {
return "127.0.0.1";
}

if (octets[2] >= 255) {
if (octets[1] >= 255) {
if (octets[0] >= 255) {
Expand All @@ -460,5 +467,40 @@ private synchronized String getNextAddress() {

return "127." + octets[0] + "." + octets[1] + "." + octets[2];
}

private boolean canCycle() {
if (canCycle == null) {
InetAddress testDestination;
InetAddress testSource;
try {
testDestination = InetAddress.getByName(null);
testSource = InetAddress.getByName("127.0.1.2");
}
catch (UnknownHostException e) {
System.out.println("[SimpleServer] Notice: Unable to bind with 127.0.1.2.");

canCycle = false;
return false;
}

try {
Socket testSocket = new Socket(testDestination, 80, testSource, 0);
testSocket.close();
}
catch (BindException e) {
System.out.println("[SimpleServer] Notice: Unable to bind with 127.0.1.2.");

canCycle = false;
return false;
}
catch (IOException e) {
// Probably nothing listening on port 80
}

canCycle = true;
}

return canCycle;
}
}
}

0 comments on commit 31b3735

Please sign in to comment.