R D
R D

Reputation: 1332

SocketException: Too many open files After Some time the server started

I having problem of Socket accept failed, I have developed an application using RESTFul technology it involves the server side and client code.

Server side I am maintain the text file to store the states like 0, 1, 2, 3, 4...

Client side gives the RESTFul request to server using URL, the server reads that text file and response based on that. I have many client(more than 300) to access this request. All client may ask the request at the same time.

Error :

14 Jun, 2014 8:24:12 PM org.apache.tomcat.util.net.JIoEndpoint$Acceptor run
SEVERE: Socket accept failed
    java.net.SocketException: Too many open files
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408)
    at java.net.ServerSocket.implAccept(ServerSocket.java:462)
    at java.net.ServerSocket.accept(ServerSocket.java:430)
    at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket
          (DefaultServerSocketFactory.java:60)
    at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:216)
    at java.lang.Thread.run(Thread.java:662)

Bellow the code for checking the server URL reachable or not :

public boolean isInternetReachable() {      
    try {          
        URL url = new URL("http://myhost.com/myapp/");
        httpcon = (HttpURLConnection) url.openConnection();
        httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
        return true;            
    } catch (IOException ex) {
         return false;
    }/*finally{
        if(httpcon!=null)httpcon.disconnect();
    }*/
}

Checking content Active :

public  boolean isContentActive() {
    BufferedReader in  =null;
    try {          
        URL url = new URL("http://myhost.com/myapp/status.txt");
        httpcon = (HttpURLConnection) url.openConnection();
        httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
        in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
        return true;
    } catch (Exception ex) {
        return false;
    }finally{
         if(in!=null){
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
         }
         //if(httpcon!=null)httpcon.disconnect();
     }
}

Reading the Text File :

public String readTxtForJson() 
throws IOException{
    String versionRead="";
    boolean reachableflag=isInternetReachable();
  if(reachableflag){
    boolean flag=isContentActive();
    BufferedReader in=null;
    if(flag){
      URL url = new URL("http://myhost.com/myapp/status.txt");
      try{
        httpcon = (HttpURLConnection) url.openConnection();
        httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
        in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
        versionRead = in.readLine();                
      }catch(Exception e){
        System.out.println("Error : In access workshop URL");
        e.printStackTrace();                    
      }finally{
         if(in!=null)in.close();
         //if(httpcon!=null)httpcon.disconnect();
      }
      return versionRead;               
    }else{
          versionRead="11"; // 11 stands for No live content
          return versionRead;
        }
  }else{            
       versionRead="12"; //12 stands for Network Not Reachable
       return versionRead;
  }     
}

What ever the answer suggested i have tried still i get this error....

I am properly maintain the database connection, Is it any leakage in the above code ?

Upvotes: 0

Views: 3350

Answers (2)

java seeker
java seeker

Reputation: 1266

please look below now

String curl = "http://www.tempsite.com/status/status.txt";
HttpURLConnection httpcon=null;
public String getstatus()
{
    String versionRead="";
    URL url = new URL(curl);
    try{
            if(httpcon==null) 
            {
              httpcon= (HttpURLConnection) url.openConnection();
            }
            httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");

            if(httpcon.getResponseCode()==HTTP_OK)
            {
              BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
              versionRead = in.readLine();
              in.close();
            }
    }
    catch(Exception e)
    {
                System.out.println("Stream may not Closed");
    }
    return versionRead;
}

Upvotes: 1

user207421
user207421

Reputation: 310885

You need to ensure that in.close() executes in all cases. It should be in a finally block.

The suggestion to use disconnect() is counter-productive, as it disables HTTP keep-alive and there produces many more connections.

Upvotes: 1

Related Questions