Bhavesh Gadoya
Bhavesh Gadoya

Reputation: 306

How to handle connection reset error in socket program

I have written a program given below. It accept some data from client & returns success in response. Sometimes it throws connection reset error & due to which some socket connection remain unclose result. Any idea how to handle connection reset error when client code tries to communicate & connection is closed by client automatically?

import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class tcp_server implements Runnable {

    private final Socket server;
    private PrintWriter toClient = null;
    private BufferedReader fromClient = null;

    public tcp_server(Socket s) {
        this.server = s;
    }

    @Override
    public void run() {
        String name = "";
        synchronized (server) {
            try {
                server.setSoTimeout(6000);
                toClient
                        = new PrintWriter(server.getOutputStream(), true);
                fromClient
                        = new BufferedReader(
                                new InputStreamReader(server.getInputStream()));
                String line = "";
                String data = "";
                while ((line = fromClient.readLine()) != null) {
                    data = data + line;
                    toClient.println("{status:success}");
                    break;
                }
            } catch (Exception eb) {

                System.out.println("{status:error,Reason:" + eb.getMessage() + "}");
            } finally {
                // System.out.println("Finally not called if timeout occurs");
                if (toClient != null) {                    
                    toClient.close();
                }
                if (fromClient != null) {
                    try {                        
                        fromClient.close();
                    } catch (IOException ex) {
                        Logger.getLogger(tcp_server.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                try {
                  server.close();                    
                } catch (IOException ex) {
                    Logger.getLogger(tcp_server.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        int serverPort = 40820;
        ServerSocket serverSocket = new ServerSocket(serverPort);
        synchronized (serverSocket) {
            for (;;) {
                Socket server = serverSocket.accept();
                new Thread(new tcp_server(server)).start();
            }
        }
    }
}

This is better code ... but still with same error "Connection reset" though applied timeout to 60sec

Upvotes: 3

Views: 5817

Answers (1)

user207421
user207421

Reputation: 310907

You handle it by closing the connection, of course.

The real question is why did you get it? There are several common causes:

  • you wrote to a connection that had already been closed by the peer
  • you closed a connection without reading data that had already arrived in the socket receive buffer. This will reset the peer.

Both of these are application protocol errors that should be fixed. There isn't any point in sending data that won't be read.

Upvotes: 3

Related Questions