diff --git a/src/com/intelligt/modbus/jlibmodbus/net/ModbusMasterConnectionTCP.java b/src/com/intelligt/modbus/jlibmodbus/net/ModbusMasterConnectionTCP.java index 7d2c2e9..acf58f9 100644 --- a/src/com/intelligt/modbus/jlibmodbus/net/ModbusMasterConnectionTCP.java +++ b/src/com/intelligt/modbus/jlibmodbus/net/ModbusMasterConnectionTCP.java @@ -61,15 +61,20 @@ public ModbusTransport getTransport() { protected void openImpl() throws ModbusIOException { if (!isOpened()) { if (parameters != null) { - Socket socket = new Socket(); InetSocketAddress isa = new InetSocketAddress(parameters.getHost(), parameters.getPort()); + Socket socket = new Socket(); try { - socket.connect(isa, Modbus.MAX_CONNECTION_TIMEOUT); + socket.connect(isa, parameters.getConnectionTimeout()); socket.setKeepAlive(parameters.isKeepAlive()); transport = ModbusTransportFactory.createTCP(socket); setReadTimeout(getReadTimeout()); } catch (Exception e) { + try { + socket.close(); + } catch (IOException e1) { + // ignored + } throw new ModbusIOException(e); } } else { diff --git a/src/com/intelligt/modbus/jlibmodbus/tcp/TcpParameters.java b/src/com/intelligt/modbus/jlibmodbus/tcp/TcpParameters.java index 40321b8..fe37eae 100644 --- a/src/com/intelligt/modbus/jlibmodbus/tcp/TcpParameters.java +++ b/src/com/intelligt/modbus/jlibmodbus/tcp/TcpParameters.java @@ -30,6 +30,7 @@ public class TcpParameters { private InetAddress host = null; private int port; private boolean keepAlive; + private int connectionTimeout = Modbus.MAX_CONNECTION_TIMEOUT; public TcpParameters() { try { @@ -43,6 +44,7 @@ public TcpParameters() { public TcpParameters(TcpParameters p) { this(p.getHost(), p.getPort(), p.isKeepAlive()); + setConnectionTimeout(p.getConnectionTimeout()); } public TcpParameters(InetAddress host, int port, boolean keepAlive) { @@ -84,4 +86,12 @@ public boolean isKeepAlive() { public void setKeepAlive(boolean keepAlive) { this.keepAlive = keepAlive; } + + public int getConnectionTimeout() { + return connectionTimeout; + } + + public void setConnectionTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + } }