Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.net.SocketException: Socket is closed

807607Nov 8 2006 — edited Nov 9 2006
I'm trying to write a client/server pair that works like this: the client sends a request to the server, the server executes that request and sends back the answer to the client. The problem is I get this exception on both sides.

On client side:
        // send the request to the server
        out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        out.write("playlist");
        JOptionPane.showMessageDialog(null,"The request for playlist has been successfully sent.");
        out.close();

        // wait for an answer from the server
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new BufferedWriter(new FileWriter("all.m3u"));

        String line;
        while ((line = in.readLine()) != null) {
            out.write(line);
            out.newLine();
        }
        out.flush();
        out.close();
        in.close();
On server side:
        try {
            while (true) {
                socket = serverSocket.accept();
                System.out.println("A client connected to the server");

                BufferedReader in = null;
                BufferedWriter out = null;

                // receive the request from the client
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String request = in.readLine();
                System.out.println("Request received: " + request);
                in.close();

                // execute the request and send the answer to the client
                if (request.equals("playlist")) {
                    System.out.println("Creating playlist...");
                    createPlaylist(new File("m:\\"));
                    System.out.println("The playlist file has been created.");
                    sendPlaylist();
                    System.out.println("The playlist file has been sent.");
                }

//                String songToPlay = null;
//                receiveSongToPlay(songToPlay);
//                System.out.println("Playing " + songToPlay + "...");
            }
        }
        catch (IOException e) {
            System.out.println(e.toString());  //To change body of catch statement use File | Settings | File Templates.
        }
I can give more code if needed, but does it look allright like this? Because I guess I'm doing something definetely wrong if I get this exception.
Thank you.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 7 2006
Added on Nov 8 2006
7 comments
770 views