"read(byteBuf) = -1" Not Working

This section provides a tutorial example of trying to use 'read(byteBuf) = -1' as the condition to stop reading the request on the server side. It will not work if the client keeps the connection open and waiting for the response.

I was not so happy about my socket server testing program, SocketRequestResponseServer.java, because it can only receive a request in a single read() statement with byte array of 10240 bytes. So I tried to read the request in a loop:

/* SocketServerTest.java
 * Copyright (c) 2002 HerongYang.com. All Rights Reserved.
 */
import java.io.*;
import java.net.*;
public class SocketServerTest {
   public static void main(String[] args) {
      PrintStream out = System.out;

// Checking command line arguments
      if (args.length < 2) {
         out.println("Usage:");
         out.println(
            "java SocketServerTest inFile outFile");
         return;
      }
      String inFile = args[0];
      String outFile = args[1];

      try {
// Reading the response from a file
         File objFile = new File(inFile);
         int resLen = (int) objFile.length();
         byte[] resBytes = new byte[resLen];
         FileInputStream inStream = new FileInputStream(objFile);
         inStream.read(resBytes);
         inStream.close();

// Creating socket socket
         ServerSocket server = new ServerSocket(8888);
         out.println("Listening at 8888");
         Socket con = server.accept();
         out.println("Connection received from "
            +con.getInetAddress().toString());

// Receiving the request
         InputStream reqStream = con.getInputStream();
         FileOutputStream outStream = new FileOutputStream(outFile);
         byte[] byteBuf = new byte[1024];
         int reqLen = 0;
         int len = 0;
         while ((len=reqStream.read(byteBuf))>-1) {;
            reqLen += len;
            outStream.write(byteBuf,0,len);
            out.println("Bytes received: "+len);
         }
         outStream.close();

// Sending the response
         OutputStream resStream = con.getOutputStream();
         resStream.write(resBytes);
         resStream.close();

         reqStream.close();

// Output counts
         out.println("Request length: "+reqLen);
         out.println("Response length: "+resLen);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

The program code logic looks good. Let's run it in a command line window:

herong> java SocketServerTest http_1_0_server.res server.req

Listening at 8888

Then run the socket client test program from another command line window:

herong> java SocketRequestResponse localhost 8888 http_get.req client.res

Connecting to localhost at 8888

We have a problem here. The client program is hanging, and not able to receive the response from the server. If you look at the server window, you will see:

Connection received from /127.0.0.1
Bytes received: 18

The server is hanging too. It did receive the first 18 byte, which is the entire request. But the next len=reqStream.read(byteBuf) statement did not return -1 to signal end of the input stream. Why, because the client kept the connection open after sending the request and was waiting to receive response. There will be no end of the input stream as long as the connection is still open.

For now, I can press Ctrl-C to stop the server, and release the connect to client.

Takeaways:

Table of Contents

 About This Book

 Introduction to Web Service

 Introduction to SOAP (Simple Object Access Protocol)

 SOAP Message Structure

 SOAP Message Transmission and Processing

 SOAP Data Model

 SOAP Encoding

 SOAP RPC Presentation

 SOAP Properties Model

 SOAP MEP (Message Exchange Patterns)

 SOAP HTTP Binding

 SOAP PHP Implementations

 PHP SOAP Extension Client Programs

 PHP SOAP Extension Server Programs

 PHP SOAP Web Service Example - getTemp

 SOAP Perl Implementations

 Perl SOAP::Lite - SOAP Server-Client Communication Module

 Perl Socket Test Program for HTTP and SOAP

 Perl SOAP::Lite for NumberToWords SOAP 1.1 Web Service

 Perl SOAP::Lite for SOAP 1.2 Web Services

 Perl SOAP::Lite for WSDL

 Python SOAP Client: Zeep

 SOAP Java Implementations

Java Socket and HttpURLConnection for SOAP

 SocketRequestResponse.java - Socket Client Testing Program

 SocketRequestResponseServer.java - Socket Server Testing Program

 Capturing the HTTP Request from a Browser

"read(byteBuf) = -1" Not Working

 Using java.net.HttpURLConnection to Send SOAP Messages

 Using HttpURLConnection to Call SOAP 1.1

 Capturing HTTP Request Generated by the HttpURLConnection Class

 Calling NumberToWords SOAP 1.1 Web Service

 Using HttpURLConnection to Call SOAP 1.2

 Calling NumberToWords SOAP 1.2 Web Service

 SAAJ - SOAP with Attachments API for Java

 SoapUI - SOAP Web Service Testing Tool

 WS-Security - SOAP Message Security Extension

 WS-Security X.509 Certificate Token

 Perl SOAP::Lite for GetSpeech SOAP 1.1 Web Service

 Perl SOAP::Lite 0.710 for SOAP 1.2 Web Services

 Perl SOAP::Lite 0.710 for WSDL

 Web Services and SOAP Terminology

 Archived Tutorials

 References

 Full Version in PDF/EPUB