Results 1 to 5 of 5
  1. #1
    newbie14 is offline Member
    Join Date
    Feb 2010
    Posts
    63
    Rep Power
    0

    Default Too many files open error

    Dear All,
    I have java listener which keep reading for incoming data to the particular port. Most of the times it works fine only at times it gets too many open files error. Below is my codes what could be wrong? Thank you.

    Java Code:
    public class commServer {
     
     
    public static void main(String[] args) {
     
     try 
     {
       final ServerSocket serverSocketConn = new ServerSocket(8888);
     
     
       while (true) 
       {
        try 
        {
           Socket socketConn1 = serverSocketConn.accept();
           new Thread(new ConnectionHandler(socketConn1)).start(); 
        }
       catch(Exception e)
       {
          System.out.println(e.toString());
        }
      }
     
     
      } 
      catch (Exception e) 
      {
      System.out.println(e.toString());
      //System.exit(0); 
      }
    }
     
     
    }
    class ConnectionHandler implements Runnable 
    {
       private Socket receivedSocketConn1;
       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
       DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy"); 
       DateFormat inDf=new SimpleDateFormat("ddMMyyHHmmss"); 
       DateFormat outDf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     
       ConnectionHandler(Socket receivedSocketConn1) 
       {
       this.receivedSocketConn1=receivedSocketConn1;
       }
     
    //@Override
    public void run() 
    { 
          Connection dbconn = null;
          BufferedWriter w = null;
          BufferedReader r = null;
     try 
     {
     
     
             PrintStream out = System.out; 
             BufferedWriter fout = null;
             w = new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputStream()));
             r = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream()));
     
             int m = 0, count=0;
             String line="";
             String n="";
             w.write("$PA\n");
             w.flush();
                 while ((m=r.read()) != -1) 
                 {
                      Date dateIn = new Date();
                      n = n + (char) m;
                      int i = n.indexOf("GET");
                      if(i != -1) 
                      { 
                         break;
                      }
     
     
                     if (m==35)
                     {
     
     
                       String ori = n;
                       String noCheckSum = n.substring(0,(n.length()-4));
                       int addExist = n.indexOf("@");
     
                       String[] slave = null;
                       if(addExist!=-1)
                       {
                         slave = noCheckSum.split("@");
                         n = slave[0];
                       }
                       else
                       {
                         n = noCheckSum;
                       }
     
                       String[] result = n.split(",");
                       Statement stmt = null;
     
                       w.write("$PA\n");
                       w.flush();
     
                       int count1 = 0;
     
                       Date date = Calendar.getInstance().getTime(); 
                       String today = formatter.format(date); 
                       String filename= "MyDataFile"today".txt"; 
                       boolean append = true; 
     
     
                       FileWriter fw = null;
                       try
                       {
                        fw = new FileWriter(filename,append); 
                        fw.write(ori+" "dateFormat.format(dateIn)"\n");//appends the string to the file 
                        Date dateOut = new Date();
                        fw.write("$PA"+" "dateFormat.format(dateOut)"\n");//appends the string to the file 
                       }
                       catch (IOException ex) 
                       { 
                       System.out.println("MyError:IOException has been caught in in the file operation");
                       ex.printStackTrace();
                       } 
                      finally
                      {
                        try 
                        {
                         if ( fw != null ) 
                         {
                           fw.close();
                         }
                         else 
                         {
                           System.out.println("MyError:fw is null in finally close");
                         }
                      }
                      catch(IOException ex){
                          System.out.println("MyError:IOException has been caught in fw is null in finally close");
                          ex.printStackTrace();
                      }
                  }
                  try
                  {
     
     
                   String deviceID=result[3].trim(); 
                   String dateTime=result[4].trim(); 
                   String[] result2 = result[10].split("'"); 
                   String gpsDate = result2[1].trim().substring(0,6); 
                   String gpsTime = result2[1].trim().substring(6,12); 
                   String gpsLat = result2[1].trim().substring(13,20);
                   String latitude = result2[1].trim().substring(13,20).substring(0,2)+"."+result2[1].trim().substring(13,20).substring(2,7);
                   String gpsLong = result2[1].trim().substring(21,29);
                   String longitude = result2[1].trim().substring(21,29).substring(0,3)+"."+result2[1].trim().substring(21,29).substring(3,8);
                   String speed = result2[1].trim().substring(30,33); 
                   String course = result2[1].trim().substring(33,36);
                   String dateTimer = null;
                   try
                   { 
                       Date inDate=null; 
                       inDf.setTimeZone(TimeZone.getTimeZone("UTC")); 
                       inDate=inDf.parse(dateTime); 
                       outDf.setTimeZone(TimeZone.getTimeZone("Asia/Kuala_Lumpur")); 
                       dateTimer=outDf.format(inDate); 
                   }
                      catch(ParseException ex)
                   { 
                       System.out.println("MyError:Parse Error has been caught for date parse close");
                       ex.printStackTrace();
                   } 
     
                   dbconn = DriverManager.getConnection("jdbc:mysql://192.168.1.155:3306/db1?"+"user=db1&password=test1");
                   stmt = dbconn.createStatement();
     
                   String selectQuery2 = "Select * from tripData Where deviceID='"+ deviceID +"' and dateTimer>'"dateTimer"' Order By dateTimer Desc Limit 1"; 
                   ResultSet rs2 = stmt.executeQuery(selectQuery2);
     
     
                   String updateQuery = "";
                   if(rs2.next())
                   {
                      String previousLatitude="";
                      String previousLongitude="";
                      String previousSpeed="";
     
                      previousLatitude = rs2.getString("latitude");
                      previousLongitude = rs2.getString("longitude");
                      previousSpeed = rs2.getString("speed");
     
                      updateQuery = "UPDATE device SET " + 
                      "latitude='" + previousLatitude + 
                       "',longitude='" + previousLongitude + 
                       "',speed='" + previousSpeed + 
                       "' WHERE serialNumber='" + deviceID + "'";
                   }
                   else
                   {
     
     
                      updateQuery = "UPDATE device SET " + 
                       "latitude='" + latitude + 
                       "',longitude='" + longitude + 
                       "',speed='" + speed + 
                       "',course='" + course +
                       "',dateTimer='" + dateTimer +
                       "' WHERE serialNumber='" + deviceID + "'";
                    } 
                    count = stmt.executeUpdate(updateQuery); 
     
     
                    String insertQuery = "INSERT INTO tripData" + 
                     "(latitude,longitude,speed,course,dateTimer,deviceID)" + " VALUES ('" + 
                     latitude + "','" + longitude + "','" + speed + "','" + course + "','" + dateTimer + "','" + deviceID + "' )";
                    count = stmt.executeUpdate(insertQuery);
     
     
                   if(addExist!=-1)
                   {
                     for(int iSlave=1; iSlave<slave.length ; iSlave++)
                     {
                        String[] slaveDetails = slave[iSlave].split(",",-1);
                        String slaveEventType = slaveDetails[0];
                        String slaveGroup = slaveDetails[1];
                        String slaveUnitID = slaveDetails[2];
                        String slaveBattLevel = slaveDetails[3];
                        String slaveSwitchStat = slaveDetails[4];
                        String slaveTempHumid = slaveDetails[5];
                        String slaveTemp="",slaveHumid=""; 
                        if(slaveTempHumid.length()>0)
                         {
                          slaveHumid = slaveTempHumid.substring(5,7);
                          if(slaveTempHumid.charAt(0)=='P')
                          {
                            slaveTemp = "+"slaveTempHumid.substring(1,3)"."+slaveTempHumid.substring(3,5); 
                          }
                          else if(slaveTempHumid.charAt(0)=='M')
                          {
                            slaveTemp = "-"slaveTempHumid.substring(1,3)"."+slaveTempHumid.substring(3,5);
                           }
                         }
                        slaveBattLevel = slaveBattLevel.trim().substring(0,2)+"."+slaveBattLevel.trim().substring(2,3);
                        String insertQuery2 = "INSERT INTO tripDataSlave" + 
                         "(dateTimer,deviceID,slaveUnitID,slaveEventType,slaveGroup,slaveBattLevel,slaveSwitchStat,slaveTemp                     ,slaveHumidity)" + " VALUES ('" + dateTimer + "','" + deviceID + "','" + slaveUnitID + "','" +   slaveEventType + "','" + slaveGroup + "','" + slaveBattLevel + "','" + slaveSwitchStat + "','" + slaveTemp + "','" +  slaveHumid + "')";
                        count = stmt.executeUpdate(insertQuery2);
                      }
                   }
                }
               catch (SQLException ex) 
               { 
                System.out.println("MyError:Error : "+ex);
     
               } 
               finally
              {
               try 
               {
                  if ( stmt != null ) 
                  {
                   stmt.close();
                  }
                  else 
                  {
                    System.out.println("MyError:stmt is null in finally close");
                  }
                }
                catch(SQLException ex){
                      System.out.println("MyError:SQLException has been caught for stmt close");
                      ex.printStackTrace();
                }
                try 
                {
                 if ( dbconn != null ) 
                 {
                   dbconn.close();
                 }
                  else 
                 {
                    System.out.println("MyError:dbconn is null in finally close");
                  }
                }
                catch(SQLException ex){
                     System.out.println("MyError:SQLException has been caught for dbconn close");
                     ex.printStackTrace();
                }
     
     
          }
           n="";
         }
      }
     } 
     catch (IOException ex) 
     { 
     System.out.println("MyError:IOException has been caught in in the main first try");
     ex.printStackTrace();
     } 
     finally
     {
       try 
       {
        if ( w != null ) 
        {
          w.close();
        }
        else 
        {
         System.out.println("MyError:w is null in finally close");
        }
       }
        catch(IOException ex){
         System.out.println("MyError:IOException has been caught in w in finally close");
        ex.printStackTrace();
       }
     
      }
     }
    }

  2. #2
    newbie14 is offline Member
    Join Date
    Feb 2010
    Posts
    63
    Rep Power
    0

    Default

    Dear Alfie,
    Here the case is that it stops permanently not just for a while so what shall I do thank you.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    You are opening more files than your OS can handle.

    Are you closing the files you open?
    Are you closing them as soon as you are finished with them?

    ETA: That's some really bad JDBC there. You should be using PreparedStatements for 90% of that lot, possibly 100%.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    In fact, what system are you on?

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    In the OP, it doens't look like close is ever being called for receivedSocketConn1 in the ConnecvtionHandler class. On most OSs (all?), a socket uses a file descriptor and if you don't close them when you're done with them, the file handle doesn't get released and you eventually run out.

Similar Threads

  1. Error while trying to open workspace
    By rshah in forum Eclipse
    Replies: 0
    Last Post: 03-26-2010, 09:15 PM
  2. Replies: 1
    Last Post: 01-13-2010, 05:58 AM
  3. Read and Open *.txt Files? help!!
    By ashton in forum New To Java
    Replies: 9
    Last Post: 11-01-2009, 03:42 PM
  4. Using Java to open applications and files
    By StokedOnMe in forum New To Java
    Replies: 5
    Last Post: 09-04-2009, 01:34 AM
  5. Different Files Open in File -> Open Workspace
    By mgm2010 in forum JCreator
    Replies: 0
    Last Post: 04-11-2009, 02:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •