我遇到Socket接受失败的问题,我使用RESTFul技术开发了一个应用程序,它涉及服务器端和客户端代码。
服务器端我维护文本文件以存储0,1,2,3,4 ...
等状态客户端使用URL向服务器提供RESTFul请求,服务器根据该请求读取该文本文件和响应。我有很多客户端(超过300个)来访问此请求。所有客户都可以同时询问请求。
错误:
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)
用于检查服务器URL是否可访问的代码:
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();
}*/
}
检查内容有效:
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();
}
}
阅读文本文件:
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;
}
}
我所尝试的答案仍然是我得到的错误......
我正确维护数据库连接,上面的代码是否有任何泄漏?
答案 0 :(得分:1)
您需要确保在所有情况下都执行in.close()。它应该在最后的块中。
使用disconnect()的建议会适得其反,因为它会禁用HTTP keep-alive并且会产生更多连接。
答案 1 :(得分:1)
请现在看下面
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;
}