Fix for long timeout opening TCPMasterConnection
Brought to you by:
wimpi
Running jamod-1.2rc1,
while trying to connect to an offline device using
TCPMasterConnection.connect()
I experienced very long (30 seconds or more) delays,
before connect() threw an exception.
My solution was to change replace this line in TCPMasterConnection.java connect()
m_Socket = new Socket(m_Address, m_Port);
with
// Create an unbound socket, THEN connect, blocking for max of m_Timeout milliseconds
// (if timeout occurs, SocketTimeoutException is thrown)
m_Socket = new Socket();
java.net.InetSocketAddress sockaddr = new java.net.InetSocketAddress( m_Address, m_Port );
m_Socket.connect( sockaddr, m_Timeout );
This defaults to a timeout of Modbus.DEFAULT_TIMEOUT (3000 milliseconds).
The problem with this approach is the fact that it would make jamod Java 1.4 dependent.
Embedded devices we are working on offer Java 1.1, or reduced versions of it.
This problem was fixed with the first release of the Java2 dependent version.
See HEAD on SVN:
http://jamod.svn.sourceforge.net/viewvc/jamod/trunk/src/main/java/net/wimpi/modbus/net/TCPMasterConnection.java?revision=23&view=markup
/**
* Returns the timeout for this <tt>TCPMasterConnection</tt>.
*
* @return the timeout as <tt>int</tt>.
*/
public int getTimeout()
/**
* Sets the timeout for this <tt>TCPMasterConnection</tt>.
*
* @param timeout the timeout as <tt>int</tt>.
*/
public void setTimeout(int timeout)