1

Is it possible to close http connection on the server side. I have java application with the sun java http server embedded. However after some research, I cannot see api to close socket connection on server side.

There is something similar in Apache Tomcat configuration, where you can close a connection on server side after X amount of requests.

But I have been unable to see something similar on the Sun java HttpServer.

Any ideas if this functionality is available? Or is it assumed to be left to client(s) to close connection(s).

Thanks

EDIT: adding the Tomcat info for reference: The tomcat config is in relation to the maxKeepAliveRequests.

The maximum number of HTTP requests which can be pipelined until the connection is closed by the server. Setting this attribute to 1 will disable HTTP/1.0 keep-alive, as well as HTTP/1.1 keep-alive and pipelining. Setting this to -1 will allow an unlimited amount of pipelined or keep-alive HTTP requests. If not specified, this attribute is set to 100.

Sample from a tomcat server.xml for Connector config: <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" acceptCount="2000" maxKeepAliveRequests="500000" allowTrace="true"/>

Charles
  • 50,943
  • 13
  • 104
  • 142
  • Welcome to Stack Overflow! Do you have an example of the tomcat configuration to have an idea of what it does? – arturomp Sep 19 '13 at 16:35
  • Thanks. The tomcat config is in relation to the maxKeepAliveRequests. **The maximum number of HTTP requests which can be pipelined until the connection is closed by the server. Setting this attribute to 1 will disable HTTP/1.0 keep-alive, as well as HTTP/1.1 keep-alive and pipelining. Setting this to -1 will allow an unlimited amount of pipelined or keep-alive HTTP requests. If not specified, this attribute is set to 100.** – user2796263 Sep 19 '13 at 16:53
  • sample from a tomcat server.xml for Connector config: **** – user2796263 Sep 19 '13 at 16:57
  • please edit your question and add these details! most people may not look at the comments! – arturomp Sep 19 '13 at 17:59

1 Answers1

0

Late answer (probably too late for the OP), but maybe useful for somebody else: I don't know myself of any API method to close the socket connection or TCP connection (I use the terms here synonymously, but see here for a distinction). However, it seems there is a dirty way of doing it by provoking an IOException. From the documentation of the HttpExchange#getResponseBody():

If the call to sendResponseHeaders() specified a fixed response body length, then the exact number of bytes specified in that call must be written to this stream. If too many bytes are written, then write() will throw an IOException. If too few bytes are written then the stream close() will throw an IOException. In both cases, the exchange is aborted and the underlying TCP connection closed.

Furthermore, looking at the implementation details in sun.net.httpserver.ServerImpl.Exchange.run(), it seems that any Exception that is thrown by the HttpHandler.handle() method and which is not a NumberFormatException or URISyntaxException will lead to the closing of the TCP connection. So in particular all IOExceptions. Therefore, throwing an IOException from within a handler would be a way to terminate the TCP connection. But it's certainly not a nice way and depends to some extent on the implementation details. Intentionally writing too much to the response-stream and provoking the corresponding exception instead of throwing one oneself would be same ugly, but at least then it wouldn't depend on implementation details, as the termination of the TCP connection is well documented in that case.

Sebastian
  • 365
  • 3
  • 17