Ofek Ron
Ofek Ron

Reputation: 8578

Closing sockets connection

I have 2 threads running a chat program such that one is a server and the other is a client, everything works fine till i click the quit button.

since the Socket of the Thread in which the Quit button was clicked get asynchronously closed while the other socket trying to read data, the reading socket in the /living thread gets disturbed by the closure and fires an error mssg, while exepcted to properly close its own socket too and die quietly.

i need suggetions to over come this...

this is the error mssg i get:

java.net.SocketException: socket closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at chatSockets.ChatClient.run(ChatClient.java:56)
    at java.lang.Thread.run(Unknown Source)

and that is the code :

public void run() {
    try {
        if (type==Type.CLIENT) {
            socket = new Socket(HOST, PORT);
        } else {
            ServerSocket ss = new ServerSocket(PORT);
            socket = ss.accept();
        }
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));

        while (!socket.isInputShutdown()) {
            chat.append(in.readLine()+"\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void actionPerformed(ActionEvent e) {
    String s = name + " : " + chat.getMssg();
    JButton src=(JButton)e.getSource();
    if (src.getText()=="Quit") {
        out.close();
        try {
            in.close();
            socket.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        System.exit(0);
    }
    else {
        out.println(s);
        chat.append(s + "\n");
        chat.clearPanel();
    }

}

Upvotes: 0

Views: 1606

Answers (3)

Tudor
Tudor

Reputation: 62469

This is expected behavior. Just handle the closing of the socket on the other end in the catch block and continue execution.

From the javadoc:

public String readLine()
                throws IOException

Read a line of text. A line is considered to be terminated by any one of a 
line feed ('\n'), a carriage return ('\r'), or a carriage return followed 
immediately by a linefeed.

Returns:
    A String containing the contents of the line, not including any 
    line-termination characters, or null if the end of the stream has been 
    reached 
Throws:
    IOException - If an I/O error occurs

So you have two options here: either rely on the fact that an IOException is thrown when the other end closes the socket and do cleanup in the catch block or if you want a clean way of shutting down send a special "stop token" to the other thread signaling it that you are exiting and that it should close the socket on its end.

Upvotes: 3

user707549
user707549

Reputation:

Try to catch the exception in client side when the server is not up.

Upvotes: 0

emin
emin

Reputation: 752

Are you sure that client is working after the server is up ?

if you try to create a client socket connection before server socket. it will not open the socket.

Upvotes: 0

Related Questions