SocketRequestResponse.pl - Socket Level Testing Program

This section provides a tutorial example on how to write a Perl program using socket interface to send a request and receive a response.

Since most Web services are based on the TCP socket communication, it is very important to have a socket level testing program ready to troubleshoot your Web service applications.

So I wrote this simple socket program, SocketRequestResponse.pl, that can be used to send a request message and receive the response message to a remote TCP based server.

#- SocketRequestResponse.pl
#- Copyright (c) 2009 HerongYang.com, All Rights Reserved.
#
   use Socket;
   ($host, $port, $in, $out) =  @ARGV;

#- Reading the request
   open(IN, "< $in");
   read(IN, $req, (-s $in));
   close(IN);

#- Connecting
   socket(SOCK,PF_INET,SOCK_STREAM,getprotobyname('tcp'));
   connect(SOCK, sockaddr_in($port, inet_aton($host)));
   select(SOCK); $| = 1; select(STDOUT);

#- Sending the request
   print SOCK $req;

#- Receiving the response
   open(OUT, "> $out");
   while ($line=<SOCK>) {
      print OUT $line;
   }
   close(OUT);

#- Closing connection
   close(SOCK);
   exit;

To try my socket testing program, I prepared this request message in a file, http_get.req, that can be used to send to any Web server:

GET / HTTP/1.0


Note that my request has 2 new line (\n) characters at the end. The first \n terminates the GET command. The second \n terminates the HTTP request header section.

Let's try it with www.yahoo.com first:

C:\herong>perl SocketRequestResponse.pl www.yahoo.com 80 \ 
   http_get.req http_get.res

C:\herong>more http_get.res
HTTP/1.0 400 Host Header Required
Via: http/1.1 media-router-fp1001.prod.media.bf1.yahoo.com 
  (ApacheTrafficServer[c s f ])
Server: ATS
Cache-Control: no-store
Content-Type: text/html
Content-Language: en
X-Frame-Options: SAMEORIGIN
Content-Length: 6533

<!DOCTYPE html>
<html lang="en-us">
  <head>
...

My socket testing program works! But yahoo.com seems to be not happy about my simple HTTP request. It returns an error: 400 Host Header Required.

Last update: 2018.

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

SocketRequestResponse.pl - Socket Level Testing Program

 Examples of HTTP 1.0 Requests and Responses

 Examples of HTTP 1.1 Requests and Responses

 SOAP 1.1 Request - Content-Length Too Small

 SOAP 1.1 Request - Content-Length Too Large

 SOAP 1.1 Request and Response of GetTemp

 SOAP 1.2 Request and Response of GetTemp

 Perl SOAP::Lite for NumberToWords SOAP 1.1 Web Service

 Perl SOAP::Lite for SOAP 1.2 Web Services

 Perl SOAP::Lite for WSDL

 SOAP Java Implementations

 Java Socket and HttpURLConnection for SOAP

 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

 Outdated Tutorials

 References

 Full Version in PDF/EPUB