|
|
@ -17,10 +17,7 @@ |
|
|
|
package moe.nekojimi.chords; |
|
|
|
package moe.nekojimi.chords; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.net.InetAddress; |
|
|
|
import java.net.*; |
|
|
|
import java.net.ServerSocket; |
|
|
|
|
|
|
|
import java.net.Socket; |
|
|
|
|
|
|
|
import java.net.UnknownHostException; |
|
|
|
|
|
|
|
import javax.net.SocketFactory; |
|
|
|
import javax.net.SocketFactory; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -42,6 +39,18 @@ public class LocalBindSocketFactory extends SocketFactory |
|
|
|
this.localAddress = localAddress; |
|
|
|
this.localAddress = localAddress; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public Socket createSocket() throws IOException |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return new LocalBoundSocket(); |
|
|
|
|
|
|
|
// throw new RuntimeException("Trying to create an empty socket!");
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private InetSocketAddress findFreeSocketAddress() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return new InetSocketAddress(localAddress, findFreePort(localAddress)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Socket createSocket(String remoteAddr, int remotePort) throws IOException, UnknownHostException |
|
|
|
public Socket createSocket(String remoteAddr, int remotePort) throws IOException, UnknownHostException |
|
|
|
{ |
|
|
|
{ |
|
|
@ -63,7 +72,10 @@ public class LocalBindSocketFactory extends SocketFactory |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Socket createSocket(InetAddress remoteAddr, int remotePort, InetAddress x, int localPort) throws IOException |
|
|
|
public Socket createSocket(InetAddress remoteAddr, int remotePort, InetAddress x, int localPort) throws IOException |
|
|
|
{ |
|
|
|
{ |
|
|
|
return new Socket(remoteAddr, remotePort, localAddress, localPort); |
|
|
|
System.out.println("Requested socket; " + localAddress + ":" + localPort + " -> " + remoteAddr + ":" + remotePort); |
|
|
|
|
|
|
|
Socket socket = new Socket(remoteAddr, remotePort, localAddress, localPort); |
|
|
|
|
|
|
|
System.out.println("Returned socket; " + socket.getLocalSocketAddress() + ":" + socket.getLocalPort() + " -> " + socket.getRemoteSocketAddress() + ":" + socket.getPort()); |
|
|
|
|
|
|
|
return socket; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static int findFreePort(InetAddress localAddr) |
|
|
|
private static int findFreePort(InetAddress localAddr) |
|
|
@ -85,4 +97,16 @@ public class LocalBindSocketFactory extends SocketFactory |
|
|
|
throw new RuntimeException("Could not find a free port"); |
|
|
|
throw new RuntimeException("Could not find a free port"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class LocalBoundSocket extends Socket |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void bind(SocketAddress bindpoint) throws IOException |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
InetSocketAddress localAddress = findFreeSocketAddress(); |
|
|
|
|
|
|
|
System.err.println("LocalBoundSocket NOT binding to " + bindpoint + ", using " + localAddress + " instead!"); |
|
|
|
|
|
|
|
super.bind(localAddress); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|