2

I'm creating server and client java applications. I would like to create an array to store my sockets in. I'm using eclipse, and when I type in this line:

Socket[] sockets = new Socket[3];

Eclipse gives me an error saying "The resource type Socket[] does not implement java.lang.AutoCloseable".

How can I fix this?

Thank you

Try/Catch Statement:

try (
                Socket[] sockets = new Socket[3]; //Line giving me error
                ServerSocket serverSocket =
                    new ServerSocket(Integer.parseInt(ip));
                Socket clientSocket = serverSocket.accept();     
                ServerClient client = new ServerClient(clientSocket);

                PrintWriter out =
                    new PrintWriter(clientSocket.getOutputStream(), true);                   
                BufferedReader in = new BufferedReader(
                    new InputStreamReader(clientSocket.getInputStream()));
                //User input
                BufferedReader stdIn =
                    new BufferedReader(
                        new InputStreamReader(System.in))
            ) {
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    out.println(inputLine);
                }
            } catch (IOException e) {
                System.out.println("Exception caught when trying to listen on port "
                    + port + " or listening for a connection");
                System.out.println(e.getMessage());
                continue;
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
7
  • did you use try catch block with resources? Commented Jul 4, 2014 at 0:57
  • I'm new to java, so I'm not quite sure what that means. I edited the post to show the try/catch statement it's in. Commented Jul 4, 2014 at 1:00
  • socket api are close-able as I have found right now, just can you take out what you have in parentheses in try and put them after { this to see what happens? Commented Jul 4, 2014 at 1:01
  • 1
    I do not see you using this array at all. Commented Jul 4, 2014 at 1:16
  • @PM77-1 The error isn't about Socket, it is about Socket[]. Commented Jul 4, 2014 at 1:21

4 Answers 4

2

While Socket class itself implements AutoCloseable interface, array of Sockets - does not.

To put it in simple terms: you cannot open or close an array.

Sign up to request clarification or add additional context in comments.

Comments

0

The resources defined in a try-with-resources block must all be auto-closeable. That's what it's for. Socket[] is not AutoCloseable, so it cannot be defined there. Move the declaration before the try. Ditto for any other resources you get the error on. Don't treat it as a general declaration block. It isn't.

Comments

0

When I run your code I recevie this error message

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: try-with-resources not applicable to variable type
    (java.net.Socket[] cannot be converted to java.lang.AutoCloseable)

I advice you not to use try catch block with resources when you want to define your socket array.

       try (
            your rest of code
        ) { 
    define your array here --->  Socket[] sockets = new Socket[3];
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                out.println(inputLine);
            }
        } catch (IOException e) {
           your rest of code 

Note: Socket class implements Closeable and AutoCloseable , yet array cannot be defined in try block like you tried to do

3 Comments

Could you tell me what try with resources is? I honestly don't get it. I think if I understood what it was, it would be easier for me to fix it.
it is new way of try catch block since java 7. if you have something closable you can put them between () and it will be closed automatically. like scanner , resutlset , so do not need to close them manually. check this out docs.oracle.com/javase/tutorial/essential/exceptions/…
0

You can outsmart this problem easily, just create a class containing a socket connection, then build an array of this class object.

Build the class:

Class example { Socket con;

The constructor and extra code here ...

}

Then just build the array:

example[] arr=new example[3];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.