SocketRequestResponseServer.java - Socket Server Testing Program

This section provides a tutorial example on how to write a socket level server side testing program to receive and record request received from a client program.

In order to learn how input parameters are encoded in the SOAP Body element when executing the test program described in the previous tutorial, I need to capture the HTTP request generated by Axis2.

So I need a socket server test program to verify how my client program is doing by recording whatever received from the client. This is very important because there is no 'trace" function on the client side to see what client is sending.

Here is my socket server test program, SocketRequestResponseServer.java:

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

// Checking command line arguments
      if (args.length < 2) {
         out.println("Usage:");
         out.println(
            "java SocketRequestResponseServer 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();
         byte[] reqBytes = new byte[10240];
         int reqLen = reqStream.read(reqBytes);

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

         reqStream.close();
         resStream.close();

// Writing the request to a file
         FileOutputStream outStream = new FileOutputStream(outFile);
         outStream.write(reqBytes,0,reqLen);
         outStream.close();

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

Note that:

Table of Contents

 About This Book

 Introduction to WSDL 2.0

 WSDL 2.0 Document Structure and Syntax

 WSDL Version 2.0 Part 2: Adjuncts

 WSDL 2.0 Document Examples with SOAP Binding

 Using WSDL Document in Java Apache Axis2/Java for WSDL

 Apache Woden for WSDL Documents in Java

 SoapUI - Web Service Testing Tool

 PHP SOAP Extension for WSDL

 Perl SOAP::Lite for WSDL

 Introduction to WSDL 1.1

 WSDL 1.1 Document Structure and Syntax

 WSDL 1.1 Binding Extension for SOAP 1.1

 SoapUI as WSDL 1.1 Testing Tool

 WSDL 1.1 and SOAP 1.1 Examples - Document and RPC Styles

 PHP SOAP Extension for WSDL 1.1

 Perl SOAP::Lite for WSDL 1.1

Apache Axis2/Java for WSDL 1.1

 Creating Service Client with WSDL 1.1 Document

 org.apache.axis2.client.Options - Operation Client Options

 sendReceive() Method - Invoking a Named Operation

 Turning Off the Chunked HTTP Flag

 AXIOM (AXIs Object Model)

 Axis2RegistrationClient.java - document/literal Style

 org.apache.axis2.rpc.client.RPCServiceClient Class

 Axis2GetExchangeRateClient.java - rpc/encoded Style

SocketRequestResponseServer.java - Socket Server Testing Program

 Capturing the HTTP Request from an Axis2 Client Program

 Using WSDL2Java to Generate Web Service Stub Classes

 WSDL 1.1 Binding Extension for SOAP 1.2

 WSDL 1.1 and SOAP 1.2 Examples - Document and RPC Styles

 SOAP 1.2 Binding - PHP, Java and Perl Clients

 Python SOAP Client: Zeep

 WSDL Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB