Allen Jiang wrote:
> Hi,Laurinolli,
> Thank you for your suggestion,and I have create a test case as following:
Still missing imports. I fixed that, and the target address. I also
added some more diagnostics on what the result of the ConnectFuture
actually was. Improved test case at the bottom.
> The print result is different in different PC,
> In notebook PC(ThinkPad T61,T8300, 2 CPU) the result is :
> 0
> In desktop PC(2 CPU) the result is :
> 5015'
> the result seems right.
> I don't know why ?
Both results seem plausible. Note that you aren't checking the result of
the connectFuture. It could be a) that the connection was established,
or b) that the connection was not-established quickly.
See my results with better diagnostics below. They coincide with what I
would expect from await(). Can you repeat your tests with the improved
test case?
Expectged results in various cases:
Illegal address, the connection attempt fails immediately:
Waited: 0
Connected: false
Also got exception:
java.nio.channels.UnresolvedAddressException
at sun.nio.ch.Net.checkAddress(Net.java:30)
at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:487)
at
org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:156)
at
org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:46)
at
org.apache.mina.core.polling.AbstractPollingIoConnector.connect0(AbstractPollingIoConnector.java:321)
Host is not present on the network, the packets are lost. There is no
exception, and the connection attempt waits about 5000 ms as expected,
on both a single-CPU desktop and quad-CPU server:
Waited: 5001
Connected: false
Host is present but does not have a daemon listening on the expected port:
Waited: 1
Connected: false
Also got exception:
java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at
sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:574)
at
org.apache.mina.transport.socket.nio.NioSocketConnector.finishConnect(NioSocketConnector.java:190)
at
org.apache.mina.transport.socket.nio.NioSocketConnector.finishConnect(NioSocketConnector.java:46)
at
org.apache.mina.core.polling.AbstractPollingIoConnector.processSessions(AbstractPollingIoConnector.java:420)
at
org.apache.mina.core.polling.AbstractPollingIoConnector.access$500(AbstractPollingIoConnector.java:64)
at
org.apache.mina.core.polling.AbstractPollingIoConnector$Worker.run(AbstractPollingIoConnector.java:468)
at
org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51)
at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
And finally the case where the host is present and is listening:
Waited: 13
Connected: true
Improved test case:
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoHandlerAdapter;
import java.net.InetSocketAddress;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
class ConnectFutureTest {
public static void main(String[] args) {
NioSocketConnector connector = new NioSocketConnector();
connector.setHandler(new IoHandlerAdapter());
for(int i=0;i<5;i++){
ConnectFuture connectFuture =
connector.connect(new InetSocketAddress("192.168.18.9", 4567));
try {
long time=System.currentTimeMillis();
connectFuture.await(5000);//5000
millisesonds
System.err.println("Waited: " +
(System.currentTimeMillis()-time));
System.err.println("Connected: " +
connectFuture.isConnected());
if (connectFuture.getException() != null) {
System.err.println("Also got
exception:");
connectFuture.getException().printStackTrace();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.exit(0);
}
}