steveohara / j2mod

Enhanced Modbus library implemented in the Java programming language
Apache License 2.0
267 stars 111 forks source link

Amend TCPMasterConnection socket handling #27

Closed djbr-david closed 8 years ago

djbr-david commented 8 years ago

When attempting a connection in the above class the Socket creation isn't respecting the timeout period. The result is that if a connection is attempted to an unavailable host the code hangs for 30-40s before issuing a timeout exception. The connect method should probably use:

        if (!isConnected()) {
            logger.debug("connect()");

            socket = new Socket();
            socket.setReuseAddress(true);
            socket.setSoLinger(true, 1);
            socket.setKeepAlive(true);
            setTimeout(timeout);

            socket.connect(new InetSocketAddress(address, port), timeout);

            prepareTransport(useRtuOverTcp);

            connected = true;
        }

which does respect the timeout.

steveohara commented 8 years ago

I've made the suggested changes and added a test case. Can you try out https://oss.sonatype.org/content/repositories/snapshots/com/ghgande/j2mod/2.2.0-SNAPSHOT/j2mod-2.2.0-20160715.114835-1-javadoc.jar

djbr-david commented 8 years ago

Thats working for me. Thanks Steve. BTW: The above link is for the javadoc content. The code content is: [https://oss.sonatype.org/content/repositories/snapshots/com/ghgande/j2mod/2.2.0-SNAPSHOT/j2mod-2.2.0-20160715.114835-1.jar]

steveohara commented 8 years ago

Whoops...