FULL PRODUCT VERSION :
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]
A DESCRIPTION OF THE PROBLEM :
I create a server socket. I create 2 client SocketChannels. I bind both channels to the exact same SocketAddress, 127.0.0.1 port=8000. I expect the second bind to fail, but it passes. Failure does not occur until the connect.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run my JUnit test that I put together for this.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
BindException should happen on second bind to the same SocketAddress.
ACTUAL -
BindException happens on the second connect.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
/*
* Created on May 1, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.test.nio.suns;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.logging.Logger;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
*
*/
public class TestBindExceptionOnConnect extends TestCase {
private final static Logger log = Logger.getLogger(TestBindExceptionOnConnect.class.getName());
/**
* @param name
*/
public TestBindExceptionOnConnect(String name) {
super(name);
}
public void setUp() {
}
public void tearDown() {
}
/**
* This tests BindException is thrown on connect. This one
* is thrown because setReuse(true) and binding to an already used
* socket.
*
* This happens on windows. need to test on linux.
* @throws Exception
*/
public void testBindExceptionOnConnect1() throws Exception {
InetAddress loopBack = InetAddress.getByName("127.0.0.1");
ServerSocketChannel svr = ServerSocketChannel.open();
svr.socket().bind(new InetSocketAddress(loopBack, 0));
SocketAddress svrAddr = svr.socket().getLocalSocketAddress();
log.info("svrAddr = "+svrAddr);
SocketChannel channel1 = SocketChannel.open();
SocketChannel channel2 = SocketChannel.open();
channel1.socket().setReuseAddress(true);
channel2.socket().setReuseAddress(true);
int port = 8000;
InetSocketAddress addr1 = new InetSocketAddress(loopBack, port);
channel1.socket().bind(addr1);
channel2.socket().bind(addr1);
channel1.connect(svrAddr);
//let it connect and just discard the connection...
svr.accept();
//NOTE: At home I keep the try....catch so my test fails when the bug is fixed.....
// try {
channel2.connect(svrAddr); //results in BindException
// fail("Should have thrown a BindException");
// } catch(BindException e) {
// //gulp
// }
channel1.close();
}
public static void main(String[] args) {
TestSuite suite = new TestSuite();
suite.addTest(new TestDisconnectAfterRead("testReflection"));
junit.textui.TestRunner.run(suite);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
I am not sure there is one....just be ready for bind failures on the connect I guess though this can be difficult if bind and connect are done at different times to due protocols like H323 or something.