Socket closed exception [duplicate]
Asked Answered
E

2

13

I wrote a simple server and client example as below .

Client :

  • Open a connection
  • Get outputstream , write to stream and close the output stream
  • Get inputstream and read from the stream. Getting exception at this point

    public class DateServer {
    public static void main(String[] args) throws InterruptedException {
    ServerSocket serverSocket = null;
    Socket client = null;
    try {
        serverSocket = new ServerSocket(6013);
        while (true) {
            client = serverSocket.accept();
            OutputStream outputStream = client.getOutputStream();
            InputStream inputStream = client.getInputStream();
    
            System.out.println("" + outputStream + "-" + inputStream);
    
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(inputStream));
    
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                System.out
                        .println("Message recieved from client ::" + line);
            }
    
            PrintWriter printWriter = new PrintWriter(outputStream, true);
    
            printWriter.println(new java.util.Date().toString());
    
            client.close();
        }
    } catch (IOException exception) {
        exception.printStackTrace();
        System.err.println(exception);
    }
        }
    
        }
    

Client :

    public class DateClient {

public static void main(String[] args) throws UnknownHostException,
        IOException, InterruptedException {
    Socket sock = new Socket("127.0.0.1", 6013);
    OutputStream outputStream = sock.getOutputStream();
    InputStream inputStream = sock.getInputStream();

    System.out.println("" + outputStream + "-" + inputStream);

    PrintWriter printWriter = new PrintWriter(outputStream, true);
    printWriter.println("Hi Server");

    outputStream.close();

    System.out.println(sock.isConnected());

    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(inputStream));
    String line;
    while ((line = bufferedReader.readLine()) != null) { // Exception 
        System.out.println(line);
    }

}

}

Getting below socket closed exception in Client . Could you please let me know what would be the reason.

  Exception in thread "main" java.net.SocketException: Socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:146)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:282)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:324)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:176)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:153)
at java.io.BufferedReader.readLine(BufferedReader.java:316)
at java.io.BufferedReader.readLine(BufferedReader.java:379)
at edu.iub.cs.httpserver.DateClient.main(DateClient.java:32)
Ezekielezell answered 24/10, 2013 at 4:48 Comment(0)
R
14

java.net.SocketException socket is closed This exception means that you closed the socket, and then continued to try to use it.

os.close();

And you closed it here. Closing either the input or the output stream of a Socket closes the other stream and the Socket.

Rafe answered 24/10, 2013 at 4:52 Comment(2)
But System.out.println(sock.isConnected()); is printing trueEzekielezell
@Ezekielezell Irrelevant. isConnected() tells you whether you ever connected the Socket. Not whether it is now connected. (The isConnected() and isClosed() methods are about the state of the socket, not the state of the connection.)Codger
B
4

java.net.SocketException arises only when u close the OutputStream (or) Socket before reading from the server.

From your code, remove:

OutputStream.close(); 

and then try..

Blush answered 4/2, 2014 at 6:48 Comment(1)
Thanks for the info, you saves my time :)Trimeter

© 2022 - 2024 — McMap. All rights reserved.