|
|
@ -60,21 +60,22 @@ public class FourLetterWordMain { |
|
|
|
public static String send4LetterWord(String host, int port, String cmd, int timeout) |
|
|
|
public static String send4LetterWord(String host, int port, String cmd, int timeout) |
|
|
|
throws IOException { |
|
|
|
throws IOException { |
|
|
|
LOG.info("connecting to {} {}", host, port); |
|
|
|
LOG.info("connecting to {} {}", host, port); |
|
|
|
|
|
|
|
Socket sock = new Socket(); |
|
|
|
InetSocketAddress hostaddress= host != null ? new InetSocketAddress(host, port) : |
|
|
|
InetSocketAddress hostaddress= host != null ? new InetSocketAddress(host, port) : |
|
|
|
new InetSocketAddress(InetAddress.getByName(null), port); |
|
|
|
new InetSocketAddress(InetAddress.getByName(null), port); |
|
|
|
|
|
|
|
BufferedReader reader = null; |
|
|
|
try (Socket sock = new Socket(); |
|
|
|
try { |
|
|
|
OutputStream outstream = sock.getOutputStream(); |
|
|
|
|
|
|
|
BufferedReader reader = |
|
|
|
|
|
|
|
new BufferedReader( |
|
|
|
|
|
|
|
new InputStreamReader(sock.getInputStream()))) { |
|
|
|
|
|
|
|
sock.setSoTimeout(timeout); |
|
|
|
sock.setSoTimeout(timeout); |
|
|
|
sock.connect(hostaddress, timeout); |
|
|
|
sock.connect(hostaddress, timeout); |
|
|
|
|
|
|
|
OutputStream outstream = sock.getOutputStream(); |
|
|
|
outstream.write(cmd.getBytes()); |
|
|
|
outstream.write(cmd.getBytes()); |
|
|
|
outstream.flush(); |
|
|
|
outstream.flush(); |
|
|
|
// this replicates NC - close the output stream before reading
|
|
|
|
// this replicates NC - close the output stream before reading
|
|
|
|
sock.shutdownOutput(); |
|
|
|
sock.shutdownOutput(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reader = |
|
|
|
|
|
|
|
new BufferedReader( |
|
|
|
|
|
|
|
new InputStreamReader(sock.getInputStream())); |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
String line; |
|
|
|
String line; |
|
|
|
while((line = reader.readLine()) != null) { |
|
|
|
while((line = reader.readLine()) != null) { |
|
|
@ -83,6 +84,11 @@ public class FourLetterWordMain { |
|
|
|
return sb.toString(); |
|
|
|
return sb.toString(); |
|
|
|
} catch (SocketTimeoutException e) { |
|
|
|
} catch (SocketTimeoutException e) { |
|
|
|
throw new IOException("Exception while executing four letter word: " + cmd, e); |
|
|
|
throw new IOException("Exception while executing four letter word: " + cmd, e); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
sock.close(); |
|
|
|
|
|
|
|
if (reader != null) { |
|
|
|
|
|
|
|
reader.close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|