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.

The socket client test program, SocketRequestResponse.java, works fine to verify how the Web service server works.

But I also need socket server test program to verify how my SOAP client program 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
 * 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 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 Message Exchange Patterns

 SOAP HTTP Binding

 SOAP Perl Implementations

 SOAP PHP Implementations

 SOAP Java Implementations

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

 Perl Socket Test Program for HTTP and SOAP

 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

 PHP SOAP Extension Client Programs

 PHP SOAP Extension Server Programs

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 GetSpeech SOAP 1.1

 Capturing HTTP Request Generated by the HttpURLConnection Class

 Calling GetSpeech SOAP 1.1 Web Service

 Using HttpURLConnection to Call GetSpeech SOAP 1.2

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

 Web Services and SOAP Terminology

 References

 PDF Printing Version