Skip to content

Commit b8f5a10

Browse files
committed
Add timeout when reading response to request_program
Without this the URCap could potentially connect to another service running on the target port not answering to "request_program" Which would leave PolyScope hanging.
1 parent 1a7cd7a commit b8f5a10

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/main/java/com/fzi/externalcontrol/impl/RequestProgram.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.DataOutputStream;
3131
import java.io.IOException;
3232
import java.io.InputStreamReader;
33+
import java.net.SocketTimeoutException;
3334
import java.net.InetSocketAddress;
3435
import java.net.Socket;
3536

@@ -80,6 +81,8 @@ public String sendCommand(String command) {
8081
System.out.println("hostIp is: " + this.hostIp);
8182
System.out.println("portNr is: " + this.portNr);
8283

84+
// set socket timeout to 500 ms when reading from buffer
85+
socket.setSoTimeout(500);
8386

8487
if (socket.isConnected()) {
8588
// output stream creation
@@ -103,6 +106,10 @@ public String sendCommand(String command) {
103106
out.close();
104107
}
105108
socket.close();
109+
} catch (SocketTimeoutException e) {
110+
result = String.format("popup(\"Timeout while reading requested program from " + this.hostIp + ":" + this.portNr + "\","
111+
+ "\"Receive program failed\", False, True, blocking=True)\n"
112+
+ "sync()", e.getMessage());
106113
} catch (IOException e) {
107114
result = String.format("popup(\"The connection to the remote PC at " + this.hostIp + ":" + this.portNr + " could not be established. Reason: %s\","
108115
+ "\"Receive program failed\", False, True, blocking=True)\n"

0 commit comments

Comments
 (0)