JDK-6402102 : HttpServer doesn't release socket connections???
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-03-22
  • Updated: 2011-02-16
  • Resolved: 2006-05-05
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0-beta2"
Java(TM) SE Runtime Environment (build 1.6.0-beta2-b75)
Java HotSpot(TM) Client VM (build 1.6.0-beta2-b75, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
If I send to the new HttpServer continuously a lot of http request after some hundred requests there are exceptions
on the client side (there is no exception on the server side):

.
.
.
.
.
.
Send: 981: 120
Received: 120
Send: 982: 120
Received: 120
Send: 983: 120
Received: 120
Send: 984: 120
Received: 120
Send: 985: 120
Received: 120
Send: 986: 120
java.net.SocketException: Unexpected end of file from server
        at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
        at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
        at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
        at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown So
urce)
        at httpserverTest.Client.main(Client.java:37)
Send: 987: 120
java.net.SocketException: Unexpected end of file from server
        at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
        at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
        at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
        at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown So
urce)
        at httpserverTest.Client.main(Client.java:37)
Send: 988: 120
java.net.SocketException: Unexpected end of file from server
        at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
        at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
        at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
        at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown So
urce)
        at httpserverTest.Client.main(Client.java:37)
.
.
.
.
.


________________________________________________________________________________

If I stop the client and restart it again the exceptions come immediately. After restarting the server
the exceptions come again after some hundred (or thousand) requests.

If the client and the server run on the same pc the following additional exception is thrown on the client side:

java.net.SocketException: No buffer space available (maximum connections reached?): connect
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
	at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
	at java.net.Socket.connect(Socket.java:516)
	at sun.net.NetworkClient.doConnect(NetworkClient.java:152)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:369)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:481)
	at sun.net.www.http.HttpClient.<init>(HttpClient.java:214)
	at sun.net.www.http.HttpClient.New(HttpClient.java:287)
	at sun.net.www.http.HttpClient.New(HttpClient.java:299)
	at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:785)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:726)
	at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:651)
	at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:829)
	at httpserverTest.Client.main(Client.java:23)


#############################################################

So it seems that the server doesn't release the used sockets.



REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package httpserverTest;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Client {
	
	public static void main(String[] args) {
		
		int counter = 1;
		
		while(true) {
			try {
				
				HttpURLConnection connection = getHttpURLConnection(new URL("http://fintat/server/"), 10000);
				
				OutputStream os = connection.getOutputStream();
				
				DataOutputStream dos = new DataOutputStream(os);
				
				short s = 120;
				
				System.out.println("Send: " + counter + ": "+ s);
				
				counter++;
				
				dos.writeShort(s);
				
				dos.close();
				
				InputStream is = connection.getInputStream();
				
				DataInputStream dis = new DataInputStream(is);
				
				short ret = dis.readShort();
				
				System.out.println("Received: " + ret);
				
				dis.close();
				
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	
	static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException {

		HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

		httpURLConnection.setConnectTimeout(40000);
		httpURLConnection.setReadTimeout(timeout);
		httpURLConnection.setDoOutput(true);
		httpURLConnection.setDoInput(true);
		httpURLConnection.setUseCaches(false);
		httpURLConnection.setAllowUserInteraction(false);
		httpURLConnection.setRequestMethod("POST");

		// HttpURLConnection httpURLConnection = new MyHttpURLConnection(url);

		return httpURLConnection;
	}
}

#########################################################
package httpserverTest;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class Server {
	
	static class MyHandler implements HttpHandler {

		public MyHandler() {
			
		}
		
		public void handle(HttpExchange arg0) throws IOException {
			InputStream is = arg0.getRequestBody();
			OutputStream os = arg0.getResponseBody();
			
			DataInputStream dis = new DataInputStream(is);
			
			short input = dis.readShort();
			
			System.out.println(input);
			
			DataOutputStream dos = new DataOutputStream(os);
			
			arg0.sendResponseHeaders(200, 2);
			
			dos.writeShort(input);
			
			dos.close();
		}
		
	}
	
	public static void main(String[] args) {
		try {
			final HttpServer server = HttpServer.create(new InetSocketAddress(80), 400);
			server.createContext("/server/", new MyHandler());
			
			server.setExecutor(null);//Executors.newFixedThreadPool(3));//(new MyExecutor(50));

			server.start();

		} catch (IOException e) {

			e.printStackTrace();
		}

	}
}

---------- END SOURCE ----------