Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.net.SocketException: Too many open files

800271Apr 17 2010 — edited Nov 27 2011
Dear All,
I am running a java socket application where it reads the data and write to text file and a database at the same time. After some time I get this error of java.net.SocketException: Too many open files ? I am totally lost I dont know where is my error? Any help please? I am runnig on linux platform and the frequency of data coming is high. I have close the reader,write,socket,db,statement,file.What else I might not have close any idea? Thank you.
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Date;
import java.text.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;
 
public class connection5 {
//public static String dri = "";
public static int pollCount = 0;
private ServerSocket serverSocketCon = null; 
private Socket conn = null; 
private String port = null; 
 
public static void main(String[] args) {
 
try {
final ServerSocket serverSocket5 = new ServerSocket(9000);
 
while (true) 
{
try 
{
 
 
Socket conn = serverSocket5.accept();
new Thread(new Client1(conn)).start(); 
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
 
} 
catch (Exception e) 
{
System.out.println(e.toString());
//System.exit(0); 
}
}
 
}
class Client1 implements Runnable {
private Socket server;
private String line,input;
 
Client1(Socket server) {
this.server=server;
}
 
 
//@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(server.getOutputStream()));
r = new BufferedReader(new InputStreamReader(server.getInputStream()));
 
 
 
 
int m = 0, count=0;
String line="",ipAdd="",ipPort="";
String n="";
w.write("$PA\n");
w.flush();
while ((m=r.read()) != 0) 
{
 
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date dateIn = new Date();
n = n + (char) m;
int i = n.indexOf("GET");
if(i != -1) { 
break;
}
 
// System.out.println("joinig the N : "+n);
// System.out.println("\nM : "+m);
if (m==35)
{
 
String[] result = n.split(",");
 
Statement stmt = null;
 
 
w.write("$PA\n");
w.flush();
 
 
int count1 = 0;
 
 
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy"); 
Date date = Calendar.getInstance().getTime(); 
String today = formatter.format(date); 
String filename= "MyDataFile"+today+".txt"; 
//System.out.println("Filename:"+filename);
boolean append = true; 
 
FileWriter fw = new FileWriter(filename,append); 
fw.write(n+" "+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 
fw.close();
 
 
try
{
 
float hdop = 0;
String driverID = "",chksum="";
 
String reportType=result[1].trim();
 
String deviceID=result[3].trim();
 
String dateTime=result[4].trim();
 
String statusBits=result[5].trim();
 
String rssi = statusBits.substring(2,4);
 
String mcc = statusBits.substring(4,9);
 
String fullBatStatus=result[6].trim();
 
String battVolt=result[6].trim().substring(0,2)+"."+result[6].trim().substring(2,3);
 
String ignVolt=result[6].trim().substring(3,5)+"."+result[6].trim().substring(5,6);
 
String digitalInput=result[7].trim().substring(0,2);
 
String digitalOutput=result[7].trim().substring(2,4);
 
String[] result2 = result[10].split("'");
 
String gpsDate = result2[1].trim().substring(0,6);
 
String gpsTime = result2[1].trim().substring(6,12);
 
String gpsStat = result2[1].trim().substring(12,13);
 
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);
 
 
Date inDate=null; 
DateFormat inDf=new SimpleDateFormat("ddMMyyHHmmss"); 
inDf.setTimeZone(TimeZone.getTimeZone("UTC")); 
 
try{ 
 
inDate=inDf.parse(dateTime); 
 
 
}catch(ParseException e)
{ 
 
} 
 
DateFormat outDf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
 
outDf.setTimeZone(TimeZone.getTimeZone("Asia/Kuala_Lumpur")); 
 
 
String dateTimer=outDf.format(inDate); 
 
 
dbconn = DriverManager.getConnection("jdbc:mysql://192.168.1.21:3306/testdb?"+"user=test&password=test12345");
stmt = dbconn.createStatement();
 
String updateQuery = "UPDATE device SET " + 
"latitude='" + latitude + 
"',longitude='" + longitude + 
"',rssi='" + rssi +
"',hdop='" + hdop +
"',dateTimer='" + dateTimer +
"',gpsStat='" + gpsStat +
"',battVolt='" + battVolt +
"',ignVolt='" + ignVolt +
"',chkSum='" + chksum +
"',driverID='" + driverID +
"',geoFences='" + reportType + 
"' WHERE serialNumber='" + deviceID + "'";
 
count = stmt.executeUpdate(updateQuery);
 
 
String insertQuery = "INSERT INTO tripData" + 
"(latitude,longitude,rssi,hdop,dateTimer,gpsStat,battVolt,ignVolt,deviceID,chkSum,driverID,geoFences)" + " VALUES ('" + 
latitude + "','" + longitude + "','" + rssi + "','" + hdop + "','" + dateTimer + "','" + gpsStat + "','" + battVolt + "','" + ignVolt + "','" + deviceID + "','" + chksum + "','"+ driverID + "','"+reportType+"')";
count = stmt.executeUpdate(insertQuery);
}
catch (SQLException e) 
{ 
System.out.println("Error of insert : "+e);
} 
 
finally
{
try 
{
if ( stmt != null ) 
{
stmt.close();
}
}
catch(SQLException ex){
 
}
try 
{
if ( dbconn != null ) 
{
dbconn.close();
}
}
catch(SQLException ex){
 
}
 
}
 
n="";
 
}
}
 
 
} 
catch (IOException e) 
{ 
 
} 
 
 
finally
{
 
try 
{
 
if ( r != null ) 
{
r.close();
}
}
catch(IOException ex){
 
}
try 
{
 
if ( w != null ) 
{
w.close();
}
}
catch(IOException ex){
 
}
try
{
 
if(server!=null)
{
server.close();
}
}
catch(IOException ex){
 
}
 
}
 
}
 
 
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 30 2010
Added on Apr 17 2010
31 comments
1,694 views