Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java simple code: java.net.SocketException: Unexpected end of file from server

I wrote some simple code in Java, the method should connect to the website and return the BufferedReader.

private BufferedReader getConnection(String url_a) {         URL url;         try {             System.out.println("getting connection");             url = new URL(url_a);             HttpURLConnection urlConnection = (HttpURLConnection)                       url.openConnection();             urlConnection.addRequestProperty("User-Agent",                     "Mozilla/5.0 (X11; U; Linux i586; en-US; rv:1.7.3) Gecko/20040924"                      + "Epiphany/1.4.4 (Ubuntu)");             inStream = new InputStreamReader(urlConnection.getInputStream());             return new BufferedReader(inStream);         } catch (Exception ex) {             Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);         }         return null;  } 

When I use it on my PC, it works fine but when I put .jar file on the server I get this error:

java.net.SocketException: Unexpected end of file from server at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:718) at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579) at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:715) at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1322) at dataconverter.Reader.getConnection(Reader.java.260) 

Problem is quite strange because the exception isn't thrown each time, sometimes everything is OK and program works fine.

Has anybody got any ideas?

like image 603
Maciej Skrzypiński Avatar asked Nov 06 '13 22:11

Maciej Skrzypiński


People also ask

What causes Java net SocketException?

The java. net. SocketException: Connection reset error usually comes when one of the parties in TCP connection like client or server is trying to read/write data, but other parties abruptly close the connection like it was crashed, stopped or terminated.

What is Java net SocketException too many open files?

net. SocketException: Too many files open exception. This also means, if you are running Tomcat, Weblogic, Websphere, or any other web server in windows machine, you are more prone to this error than Linux based systems e.g. Solaris or Ubuntu. By the way, this error is the same as java. io.

What is Java connection reset?

Connection reset simply means that a TCP RST was received. TCP RST packet is that the remote side telling you the connection on which the previous TCP packet is sent is not recognized, maybe the connection has closed, maybe the port is not open, and something like these.


1 Answers

"Unexpected end of file" implies that the remote server accepted and closed the connection without sending a response. It's possible that the remote system is too busy to handle the request, or that there's a network bug that randomly drops connections.

It's also possible there is a bug in the server: something in the request causes an internal error, and the server simply closes the connection instead of sending a HTTP error response like it should. Several people suggest this is caused by missing headers or invalid header values in the request.

With the information available it's impossible to say what's going wrong. If you have access to the servers in question you can use packet sniffing tools to find what exactly is sent and received, and look at logs to of the server process to see if there are any error messages.

like image 130
Joni Avatar answered Sep 29 '22 04:09

Joni