Permalink
Browse files

[maven-release-plugin] copy for tag 2.0.6

git-svn-id: https://svn.apache.org/repos/asf/mina/mina/tags/2.0.6@1392839 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
2 parents f68b65f + ab9c4e9 commit 684d06fa15125e3ff892422d21664866338e94a0 Emmanuel Lecharny committed Oct 2, 2012
@@ -19,6 +19,9 @@
*/
package org.apache.mina.transport.socket.nio;
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.DatagramChannel;
@@ -139,7 +142,25 @@ protected boolean isWritable(DatagramChannel handle) {
@Override
protected SocketAddress localAddress(DatagramChannel handle) throws Exception {
- return handle.socket().getLocalSocketAddress();
+ InetSocketAddress inetSocketAddress = (InetSocketAddress) handle.socket().getLocalSocketAddress();
+ InetAddress inetAddress = inetSocketAddress.getAddress();
+
+ if ((inetAddress instanceof Inet6Address) && (((Inet6Address) inetAddress).isIPv4CompatibleAddress())) {
+ // Ugly hack to workaround a problem on linux : the ANY address is always converted to IPV6
+ // even if the original address was an IPV4 address. We do store the two IPV4 and IPV6
+ // ANY address in the map.
+ byte[] ipV6Address = ((Inet6Address) inetAddress).getAddress();
+ byte[] ipV4Address = new byte[4];
+
+ for (int i = 0; i < 4; i++) {
+ ipV4Address[i] = ipV6Address[12 + i];
+ }
+
+ InetAddress inet4Adress = Inet4Address.getByAddress(ipV4Address);
+ return new InetSocketAddress(inet4Adress, inetSocketAddress.getPort());
+ } else {
+ return inetSocketAddress;
+ }
}
@Override
@@ -62,14 +62,22 @@ private AvailablePortFinder() {
}
/**
- * Gets the next available port starting at the lowest port number.
+ * Gets an available port, selected by the system.
*
* @throws NoSuchElementException if there are no ports available
*/
public static int getNextAvailable() {
+ ServerSocket serverSocket = null;
+
try {
// Here, we simply return an available port found by the system
- return new ServerSocket(0).getLocalPort();
+ serverSocket = new ServerSocket(0);
+ int port = serverSocket.getLocalPort();
+
+ // Don't forget to close the socket...
+ serverSocket.close();
+
+ return port;
} catch (IOException ioe) {
throw new NoSuchElementException(ioe.getMessage());
}
@@ -107,6 +115,7 @@ public static boolean available(int port) {
ServerSocket ss = null;
DatagramSocket ds = null;
+
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);

0 comments on commit 684d06f

Please sign in to comment.