The code executes as expected, but I am not sure it is considered good code:)
I am thinking especially of the two streams on the socket, if the first one is successfully opened and the next one for some reason throws an exception,
the finally block will never run.
If someone would validate whether or not this code is okay, I would really appreciate it
:)
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
clientSocket = new Socket("192.168.0.104", 7);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
try {
BufferedReader userInBuffer= new BufferedReader(new InputStreamReader(System.in));
String userInput;
if ((userInput = userInBuffer.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
} finally {
out.close();
in.close();
clientSocket.close();
}
} catch (UnknownHostException e) {
System.err.println("Host name unavailable.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection.");
System.exit(1);
}
}
}