0

I'm asking out of a "this can't be right" kind of mindset. This is the Code I'm trying to write:

final Socket client; // Constructor public ClientHandler(Socket client) { this.client = client; } @Override public void run() { // Instantiating Readers and Writers InputStreamReader binaryReader; //for primitive Datatypes DataOutputStream binaryWriter; //for primitive Datatypes BufferedReader asciiReader; //for ASCII Strings PrintWriter asciiWriter; //for ASCII Strings // Assigning Readers and Writers boolean IOInitialized = false; while (!IOInitialized) { try { binaryReader = new InputStreamReader(client.getInputStream()); binaryWriter = new DataOutputStream(client.getOutputStream()); asciiReader = new BufferedReader(binaryReader); asciiWriter = new PrintWriter(client.getOutputStream()); IOInitialized = true; } catch (IOException ex) { ex.printStackTrace(); } } //Testing asciiWriter.println("Testing Writer"); } 

I'm trying to initialize IOStreams from a Socket. Their initialisation requires a Try-Catch block, and using the new IOStreams requires a guarantee that they're initialized, which I solved with the while loop but the compiler doesn't accept this. When trying to use the asciiWriter to print something to the Client, the compiler tells me that "asciiWriter might not have been initialized". Encompassing the entire run()-method would solve the problem but seems like a very unclean solution. Is there any way I can get this to work in a better way?

13
  • Use a try-with-resources. Commented Jun 5, 2021 at 13:41
  • "... the IDE doesn't accept this." - This is pretty irrelevant. What does the compiler say? Commented Jun 5, 2021 at 13:44
  • 1
    This is not a solution but a horribly kludge. When the attempt to create a stream already produces an exception, the chances that you can use them without problems are low. In the worst case, this code will loop infinitely. It’s also unclear why you create all these objects when you don’t use them. On the other hand, using multiple streams to write into the same channel will quickly lead to corrupted data. Commented Jun 7, 2021 at 11:43
  • 1
    You should not have multiple streams. In your example, they are exhibiting a completely wrong understanding, both, binaryReader and asciiReader, have a Reader type for Unicode character data and nothing else, not “for primitive Datatypes” and not limited to ASCII in any way. The only difference is that the latter is a buffered version of the former, so when you use the former, you’ll bypass the buffer and distort the data. And one side using a DataOutputStream to write data would be entirely incompatible to the other side using a Reader to read the data. You should not mix them up. Commented Jun 8, 2021 at 12:57
  • 1
    Don’t try to continue when getInputStream() or getOutputStream() throw an exception. Consider the entire Socket as being in an erroneous state and bail out. In case of a client, decide to try to connect again or to quit, in case of the server, wait for such a new attempt or proceed without that particular client. Commented Jun 8, 2021 at 13:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.