Can you please help me with the answer, please? I've read a lot, but can understand, how to organize the code. I want to create my own method to work with the Network and call it time to time in a program. But this compilation error makes me mad:
variable might be not have been initialized
I do understand why, but I can`t see solution(( My vision is to open Socket and related streams only one time and close it when needed.
public static void socket_r (String action, String ip_addr, String to_write, int port) throws IOException {
String s;
switch (action) {
case "Create": Socket socket = new Socket (ip_addr, port);
OutputStream out_from_socket = socket.getOutputStream();
PrintWriter writer_socket = new PrintWriter(out_from_socket, true);
InputStream input_socket = socket.getInputStream();
BufferedReader reader_socket = new BufferedReader(new InputStreamReader(input_socket));
break;
case "Write": writer_socket.println(to_write);
writer_socket.println();
break;
case "Read": while ((s = reader_socket.readLine()) != null) System.out.println(s);
break;
case "Close": writer_socket.println(to_write);
writer_socket.println();
writer_socket.close();
reader_socket.close();
break;
}
}