Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6401598

New HttpServer cannot serve binary stream data

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 6
    • 6
    • core-libs
    • b85
    • x86
    • windows_xp

      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 :
      I created a simple demo to reproduce this bug.
      A client sends and receives continuously http POST messages to the new HttpServer. The client sends short
      numbers via DataOutputStream and the server simply replies this number. When one of the two bytes of the short number
      becomes to negative there are exceptions on client side (on the server side there are no errors, at all):

      Send: 120
      Received: 120
      Send: 121
      Received: 121
      Send: 122
      Received: 122
      Send: 123
      Received: 123
      Send: 124
      Received: 124
      Send: 125
      Received: 125
      Send: 126
      Received: 126
      Send: 127
      Received: 127
      Send: 128
      java.net.SocketException: Unexpected end of file from server
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:688)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)Send: 129

      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:575)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:974)
      at httpserverTest.Client.main(Client.java:38)
      java.net.SocketException: Unexpected end of file from server
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:688)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:686)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:974)
      at httpserverTest.Client.main(Client.java:38)
      Send: 130
      java.net.SocketException: Unexpected end of file from server
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:688)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:686)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:974)
      at httpserverTest.Client.main(Client.java:38)
      Send: 131
      java.net.SocketException: Unexpected end of file from server
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:688)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:686)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:974)
      at httpserverTest.Client.main(Client.java:38)

      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      Send: 254
      java.net.SocketException: Unexpected end of file from server
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:688)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:686)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:974)
      at httpserverTest.Client.main(Client.java:38)
      Send: 255
      java.net.SocketException: Unexpected end of file from server
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:688)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:686)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:974)
      at httpserverTest.Client.main(Client.java:38)
      Send: 256
      Received: 256
      Send: 257
      Received: 257
      Send: 258
      Received: 258
      Send: 259
      Received: 259
      Send: 260
      Received: 260
      Send: 261
      Received: 261
      Send: 262
      Received: 262
      Send: 263
      Received: 263
      Send: 264
      Received: 264
      Send: 265
      Received: 265
      Send: 266
      Received: 266
      Send: 267
      Received: 267
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      Send: 382
      Received: 382
      Send: 383
      Received: 383
      Send: 384
      java.net.SocketException: Unexpected end of file from server
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:688)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:575)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:974)
      at httpserverTest.Client.main(Client.java:38)
      Send: 385
      java.net.SocketException: Unexpected end of file from server
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:688)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)
      at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:686)
      at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:558)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:974)
      at httpserverTest.Client.main(Client.java:38)



      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) {

      short counter = 120;

      while(true) {
      try {
      if (counter > 30000) {
      counter = 0;
      }

      HttpURLConnection connection = getHttpURLConnection(new URL("http://fintat/server/"), 10000);

      OutputStream os = connection.getOutputStream();

      DataOutputStream dos = new DataOutputStream(os);

      System.out.println("Send: " + counter);

      dos.writeShort(counter);

      dos.close();

      counter++;

      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 ----------

            michaelm Michael McMahon
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: