Skip to content

Commit f993d7c

Browse files
Refactorization to Update TelnetCommand.java
1 parent ef2c995 commit f993d7c

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/main/java/com/mycmd/commands/TelnetCommand.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ public void execute(String[] args, ShellContext context) throws IOException {
3131
}
3232
}
3333

34+
Thread reader = null;
3435
try (Socket socket = new Socket(host, port)) {
3536
socket.setSoTimeout(0); // blocking reads
3637
System.out.println("Connected to " + host + ":" + port + " (type 'exit' to quit)");
3738

3839
// Reader thread: prints remote data to stdout
39-
Thread reader =
40+
reader =
4041
new Thread(
4142
() -> {
4243
try (InputStream in = socket.getInputStream();
@@ -77,10 +78,12 @@ public void execute(String[] args, ShellContext context) throws IOException {
7778
System.out.println("Connection failed: " + e.getMessage());
7879
}
7980

80-
try {
81-
reader.join(1000); // wait up to 1 second for reader to finish
82-
} catch (InterruptedException e) {
83-
Thread.currentThread().interrupt();
81+
if (reader != null) {
82+
try {
83+
reader.join(1000); // wait up to 1 second for reader to finish
84+
} catch (InterruptedException e) {
85+
Thread.currentThread().interrupt();
86+
}
8487
}
8588
}
8689

0 commit comments

Comments
 (0)