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 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 by Dr. Herong Yang, 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:

Last update: 2009.

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

 WSDL 20 Programming APIs and Testing Tools

 Introduction to WSDL 1.1

 WSDL 1.1 Document Structure and Syntax

 WSDL 1.1 Binding Extension for SOAP 1.1

 soapUI 3.0.1 - Web Service Testing Tool

 WSDL 1.1 and SOAP 1.1 Examples - Document and RPC Styles

 PHP SOAP Extension in PHP 5.3.1

 Using WSDL in Perl with SOAP::Lite 0.710

Using WSDL Document in Java with Axis2 1.4.1

 What Is Axis2?

 Downloading and Installing Axis2/Java 1.4.1

 org.apache.axis2.client.ServiceClient Class

 Creating Service Client with WSDL 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

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

 WSDL Related Terminologies

 References

 PDF Printing Version