Menu

TCPMasterConnection attempts to connect without timeout causing it to hang

2016-03-10
2016-03-10
  • paul stanton

    paul stanton - 2016-03-10

    Version: j2mod-1.06.jar

    The implementation of TCPMasterConnection.connect means that there is no timeout for the 'Socket.connect' called by the 'Socket' constructor. This causes the thread to hang indefinitely.

        public synchronized void connect() throws Exception {
            if (! isConnected()) {
                if (Modbus.debug)
                    System.out.println("connect()");
    
                m_Socket = new Socket(m_Address, m_Port);
                m_Socket.setReuseAddress(true);
                m_Socket.setSoLinger(true, 1);
                m_Socket.setKeepAlive(true);
    
                prepareTransport();
    
                m_Connected = true;
            }
        }// connect
    

    I suspect this can be solved by replacing

                m_Socket = new Socket(m_Address, m_Port);
    

    with

                m_Socket = new Socket();
                setTimeout(m_Timeout);
                m_Socket.connect(new InetSocketAddress(m_Address, m_Port), m_Timeout);
    

    I have not tested this, please investigate.

    PS; This code snippet is up to date, checked against:
    https://sourceforge.net/p/j2mod/code/HEAD/tree/trunk/src/com/ghgande/j2mod/modbus/net/TCPMasterConnection.java

     
    • Julie Haugh

      Julie Haugh - 2016-03-10

      Thanks! Judging from the closing "// connect" comment on the end of that method, that's code I've never had a chance to review / clean up since forking the project. I'll take a look and hopefully push a change tonight or tomorrow.

      Note that I will be migrating the entire project to GitHub in the near future, but this change should be made here.

       
  • paul stanton

    paul stanton - 2016-03-10

    also, please note there is no tag for the 1.06 release which makes it hard to create a patched jar

     
    • Julie Haugh

      Julie Haugh - 2016-03-10

      Thanks - I'll try and remember to tag the new change when I make it.

       

Log in to post a comment.