public void Bind_internal(EndPoint sa, out int error) { error = 0; IPEndPoint addr = sa as IPEndPoint; if (addr == null) { error = 10044; //WSAESOCKTNOSUPPORT (Socket type not supported) return; } if (jSocket == null || jSocket.isBound() || jSocket.isConnected()) { error = 10022; //WSAEINVAL (Invalid argument) return; } try { jSocket.bind(new java.net.InetSocketAddress(java.net.InetAddress.getByName(addr.Address.ToString()), addr.Port)); } catch (Exception e) { error = 10048; //WSAEADDRINUSE (Address already in use) #if DEBUG Console.WriteLine("Caught exception during Bind_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace); #endif } }
public void Bind_internal(EndPoint sa, out int error) { error = 0; IPEndPoint addr = sa as IPEndPoint; if (addr == null) { error = 10044; //WSAESOCKTNOSUPPORT (Socket type not supported) return; } if (jSocket == null || jSocket.isBound() || jSocket.isConnected() || jSocketChannel.isConnectionPending()) { error = 10022; //WSAEINVAL (Invalid argument) return; } try { // This code I need because a bug in the java.nio.channels.SocketAdapter, which // returns local port 0 if the socket is not connected (even if the socket is bound) // so I need temporary use regular socket (not channel socket) to bind it to the // local address and use this address in the LocalPoint property and to create the // actual client/server channel sockets // The bug #5076965 (SocketChannel does not report local address after binding to a wildcard ) // See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5076965 java.net.Socket jTempSocket = new java.net.Socket(); jTempSocket.bind(new java.net.InetSocketAddress(java.net.InetAddress.getByName(addr.Address.ToString()), addr.Port)); jTempLocalSocketAddress = (java.net.InetSocketAddress)jTempSocket.getLocalSocketAddress(); jTempSocket.close(); jSocket.bind(jTempLocalSocketAddress); } catch (Exception e) { error = 10048; //WSAEADDRINUSE (Address already in use) #if DEBUG Console.WriteLine("Caught exception during Bind_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace); #endif } }