iSockets: The Cool, New, Free Way to Do Web Services PDF Print E-mail
Written by Robert Cozzi   
Tuesday, 17 January 2006

Bob has written a new tool just for you.

It's been awhile since I've built a free tool for the iSeries and AS/400 market.

First, I wrote something called DSPDBF (circa 1984) that allowed you to display and update any database file (physical or logical) in character mode or hex mode. Then, I did Q38TEXT (which I hear some people continue to use to this day); it was the first non-IBM word processor for this architecture. In fact, Q38TEXT may have been available before IBM's System/38 text processor. I don't remember. Then, there was Q38 UTILITIES, which became COZZI UTILITIES, a set of programs that eventually contributed to encouraging IBM to produce QUSRTOOLS.

On the iSeries, I created an early CGI library called CGI TOOLS that allowed you to more easily use the IBM-provided CGI APIs. Then, I built RPG xTools, a set of nearly 200 subprocedures that make doing complex API calls or day-to-day RPG IV tasks easier.

Enter iSockets

iSockets is a service program that contains about a dozen subprocedures that allow RPG IV programmers to easily communicate with Web services. For example, suppose you need to send a request to UPS to track a package. With iSockets, you simply open the UPS URL, send it your request, and receive the response back into your RPG IV program.

I would show you an example, but since the UPS license agreement seems to be over 10,000 words in length, I have no idea if I'm legally allowed to show it to you. (Do they really believe anyone has ever read that agreement?)

iSockets wraps traditional sockets programming into easy-to-use functions. While sockets programming may be commonplace to non-iSeries developers, it is not very common in our RPG world. iSockets makes using sockets simple. If you can call a subprocedure, you can open a socket to a Web services Web site.

A simple way to illustrate how iSockets helps is the GETURI example program. GETURI is an industry-wide routine that is used to open a Web page and read its contents (HTML tags and data) into a variable in your program.

Illustrated in Figure 1 is the GETURL program source code. I chose GETURL instead of GETURI because I discovered in the CGI Web programming classes I teach that most people think GETURI is simply a misspelling of GETURL.

     H OPTION(*NODEBUGIO:*SRCSTMT)  BNDDIR('ISOCKETS')
      /IF DEFINED(*CRTBNDRPG)
     DFTACTGRP(*NO)
      /ELSE
     NOMAIN
      /ENDIF

      /COPY ISOCKETS/QCPYSRC,iSockets

      /IF NOT DEFINED(*CRTBNDRPG)
     D GetURL          PR            10I 0
     D  URL                         128A   Const                                
     D  szRtnHTML                 65535A   OPTIONS(*VARSIZE)                    
     D  nRtnLen                      10I 0 Const                                


     P GetURL          B                   Export 
     D GetURL          PI            10I 0
     D  URL                         128A   Const                                
     D  szRtnHTML                 65535A   OPTIONS(*VARSIZE)                    
     D  nRtnLen                      10I 0 Const                                
      /ELSE
     D URL             S            128A
      /ENDIF

     D szURL           S            128A   Varying
     D hURL            S             10I 0
     D nBytes          S             10I 0
     D szHtml          S          65535A
     D pHtml           S               *   Inz(%addr(szHtml))
     D nPage           S             10I 0
     D nPageB          S             10I 0
     D nPageF          S             10I 0
     D szDomain        S                   Like(szURL)
     D szPage          S                   Like(szURL)
     D szParms         S                   Like(szURL)

      /IF DEFINED(*CRTBNDRPG)
     C     *ENTRY        PLIST
     C                   PARM                    URL

     C                   eval      *INLR = *ON    
      /ENDIF

     C                   if        %Parms >= 1
     C                   eval      szUrl = %trimR(URL)
     C                   endif
     C                   if        szURL = ''
     C                   return
     C                   endif

     C                   eval      nPageF = %scan('':szURL)
     C                   eval      nPageB = %scan('/':szURL)
     C                   if        nPageB = 0 and nPageF = 0
     C                   eval      nPage = 0
     C                   else
     C                   if        nPageB > nPageF
     C                   eval      nPage = nPageB
     C                   else
     C                   eval      nPage = nPageF
     C                   endif
     C                   endif
      ** Split the page being requested apart from the domain name.
     C                   if        nPage > 0
     C                   eval      szDomain = %subst(szURL:1:nPage-1)
     C                   eval      szPage   = %Subst(szURL:nPage)
     C                   endif

     C                   eval      hUrl = OpenURL(szDomain)
     C                   eval      nBytes=SendURLText(hUrl:'GET http://'+
     C                                    szDomain+ szPage + ' HTTP/1.0 ')
     C                   eval      nBytes =
     C                                RecvUrlData(hUrl:pHtml:%size(szHtml))
     C
     C                   callp     CloseURL(hUrl)

      /IF NOT DEFINED(*CRTBNDRPG)
     C                   if        nBytes <= nRtnLen
     C                   eval      szRtnHtml = %subst(szHtml:nBytes)
     C                   else
     C                   eval      szRtnHtml = %subst(szHtml:nRtnLen)
     C                   endif
     C                   return    nBytes
      /else
     C                   return
      /endif

Figure 1: Here's the source for the GetURL program.

The GETURL program is sort of polymorphic. I use preprocessor directives to condition the source code so that you can use it as either a standalone program or as a procedure within a module. It is controlled based on the compiler command being used to compile it. If you use CRTBNDRPG (PDM option 14), a standalone program is produced; if you use CRTRPGMOD (PDM option 15), a module is created, and the pieces unique to *PGM objects are discarded. I tested the program version of GETURL only in debug mode, but you could certainly add the ability to write the returned HTML (stored in szHTML) to a source or IFS file. That should be simple enough.

iSocket Interfaces

Today, we'll cover five of the iSocket subprocedures. The GETURL example uses four of them:

  • OpenURL opens a URL.
  • SendURLText writes character text strings to the open URL.
  • RecvURLData receives the response back from the Web services.
  • CloseURL closes the open URL.

OpenURL (Open a URL)

hURL = OpenURL( domain or IP address [ :nPort ]  )

The OpenURL subprocedure opens a socket to a domain name or IP address. The optional second parameter indicates the port number to use for communications. Normally, port 80 is used, so it's the default. If unspecified, the OpenURL subprocedure asks your system for the port number through which HTTP is running.

SendURLData (Send URL Data)

bytes-sent = SendURLData( hURL  : address-of-variable  : length-of-data-to-send )

The SendURLData procedure sends data to the open URL. The data to be sent is specified on the second parameter, and the length of the data to be sent is specified on the third parameter. The second parameter is a pointer to the data to be sent, not a variable. This means %add() is used to specify the second parameter. Here's an example.

This is correct:

SendUrlData(hUrl : %addr(myData ): %len(myData))

This is not correct:

SendUrlData(hUrl : myData : %len(myData))  

SendURLData returns the number of bytes written to the open URL. Check this value for zero or less than zero to ensure that your data is sent correctly.

SendURLText (Send URL Text)

bytes-sent = SendURLTexta( hURL  : text-variable or 'quoted-literal' )

This simplified version of the SendURLData iSockets procedure allows you to easily pass a text string to the open URL. Whatever you specify is passed.

RecvURLData (Receive Reply URL Data)

bytes-returned = RecvURLData( hURL  : address-of-variable  : max-length-to-return )

The RecvURLData procedure waits for the Web service to reply to your request. The response is copied into the second parameter (again the address of a variable), and the number of bytes returned is returned by RecvURLData. Here's an example:

nBytesRecv = RecvUrlData(hUrl : %addr(myData ): %len(myData))

CloseURL (Close an Open URL)

CloseURL(  hURL )

The CloseURL subprocedure closes an open socket to a domain name or IP address. Pass the handle to the URL (hURL) that you received when issuing the OpenURL procedure.

But Wait; There's More!

That's all I have time for this week. Next time, I'll cover the rest of the iSockets interfaces, including these:

  • GetURLData—Retrieve (e.g., steal) a Web page's content from within RPG IV
  • EncodeURI—Escape (encode) a URL component before sending it to a Web services routine
  • UnEncodeURI—Unescape a received URL component
  • UnEscapeURI—Unescape a received URL component
  • OpenCvtCCSID—Open conversion using From/To CCSID
  • CvtCCSID—Convert from one CCSID to another (e.g., ASCII to EBCDIC)
  • CloseCvtCCSID —Close and open conversion
  • Joblog—Write a text message to the joblog

Remember, iSockets is completely free and is available only at www.rpgiv.com/isockets or www.iSockets.net.

Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.


Last Updated ( Tuesday, 17 January 2006 )
 
Discuss (12 posts)
T.Malik
iSockets: The Cool, New, Free Way to Do Web Services
Feb 07 2006 19:04:00
You will understand the code when you grow up. <BR>
In the meantime seek help.
#119116
tslate1
iSockets: The Cool, New, Free Way to Do Web Services
Feb 07 2006 15:54:00
Absolutely useless code. At least you got your name in print!
#119115
T.Malik
iSockets: The Cool, New, Free Way to Do Web Services
Feb 01 2006 16:35:00
Here is a simple Java version. <p>Pass something like <a href="http://www.mypage.com">http://www.mypage.com</a> and you are done. <BR>
You can put in any url including the ones that invokes Webservices or any url string. <BR>
once you have the server response, you can do whatever with it. <p>By the way with a little bit of imagination you can take the response <BR>
and put it anywhere on the AS/400. <p>You can even call it from RPG. <BR>
If you are using this from the As/400 make sure you substitute the url with correct IP address as AS/400 does not know where the webserver is for example <BR>
http;//localhost/mypage.html would be <BR>
<a href="http://10.10.10.10/mypage.html">http://10.10.10.10/mypage.html</a>. <p>As long as you know the address of your webserver fire away this program and you will get the response back from the webserver. <p>I would not use this with Webservices, there are better ways to deal with webservives. This is good to invoke webpages that invoke further logic. You get too many HTML tags and stuff back from the Web Server. <p>enjoy.. <p>Malik <BR> t_malik@bellsouth.net <p><!--mccodelink_begin--> <BR>
<!-- do not remove --> <BR>
<hr width=50 align=left><small><a href='http://www.mcpressonline.com/mc/showcode@@.6b3369a0/9' target='_blank'>Code</a></small> <BR>
<!--mccodelink_end-->
#119114
brianmay
iSockets: The Cool, New, Free Way to Do Web Services
Jan 30 2006 14:19:00
Bob, could this product be used for socket communications between multiple iSeries systems? I have no need for communicating with web services at this point, but a simple interface to handle socket communications between boxes or between the iseries and some of my Windows apps would be great <p>Thanks, <p>Brian
#119113
foxj@mscdirect.com
iSockets: The Cool, New, Free Way to Do Web Services
Jan 25 2006 10:56:00
Most of the companies I do services with require SSL. Is there any plans to incorporate the ability to use secure communications?
#119112
Guest.Visitor
iSockets: The Cool, New, Free Way to Do Web Services
Jan 20 2006 15:51:00
Hello, <p>Thank you by interesting article, but I have a problem for get library isockets because both Web Sites it's down, Any can send me the library for try. Thank you Julio pinellij@argentina.com
#119111
Guest.Visitor
iSockets: The Cool, New, Free Way to Do Web Services
Jan 18 2006 13:37:00
Bob, Thanks for the article and the code. This will make it easy to get started with this.
#119110
R.Cozzi
iSockets: The Cool, New, Free Way to Do Web Services
Jan 18 2006 12:57:00
robberendt, <p>If that were the case, how many of my prior works contributed to the products, articles, tools and so forth used throughout the market. In fact, my stuff has been used for so long and that people have forgotten where it came from. Many even think it is part of the base OS. Why create iSeriesNetwork when "Q38 Technical Journal" was around? Why create yet another newsletter when there is already one out there? <p>As Hans has pointed out Scott won an award for his website and contribution to the iSeries community. Scott's HTTP tutorial did not win an award. Scott's HTTP API is a good place to go to see all the bits and pieces involved in Sockets programming, but it is not a concise tool that does one specific thing, easily. <p>Using your logic, why write FireFox or Opera when Internet Explorer is already there? Because different people have different perspectives on things, there are different needs and different ways of providing things to the community.
#119109

H.Boldt
iSockets: The Cool, New, Free Way to Do Web Services
Jan 18 2006 12:08:00
Scott's web site is a tutorial describing how to do socket programming using RPG. I believe what Bob is providing is a library of procedures to simplify the process for RPG programmers without having to know all the nitty gritty details. Others have indeed written such libraries, but as far as I know, Bob's is the first <em>free</em> library. It may well be a case of NIH, but IMO, Bob is to be commended on his efforts. <p>Cheers! <a href="http://www.boldts.net/">Hans</a>
#119108
robberendt
iSockets: The Cool, New, Free Way to Do Web Services
Jan 18 2006 11:43:00
Isn't there already free award winning socket stuff available at: <BR>
<a href="http://www.scottklement.com/rpg/socktut/">http://www.scottklement.com/rpg/socktut/</a> <p>Isn't your code an example of the "Not Invented Here" syndrome?
#119107

H.Boldt
iSockets: The Cool, New, Free Way to Do Web Services
Jan 18 2006 10:08:00
It's good to see new procs like this for RPG! As you pointed out, this kind of stuff is rather foreign to most OS/400 programmers. Easy to use procs are clearly important. Programmers in the Unix/Linux world have had the functionality for years, and it's time RPG programmers had the same advantages. <p>Just to show the kinds of things Linux programmers are used to, below is a command line session demonstrating the use of Python's urllib. It's a simple example of retrieving a web resource. <p>The function call urllib.urlopen() opens the web resource, and returns a file-like object, which can be read like a file. The method readlines() returns the data of the web page as a list of records. <p>Bob, if you need inspiration for your iSockets, perhaps you should look at the functionality provided by <a href="http://www.python.org/">Python</a>'s <a href="http://python.org/doc/2.4.2/lib/internet.html">Internet Protocols and Support</a>. <p>Cheers! <a href="http://www.boldts.net/">Hans</a> <p><!--mccodelink_begin--> <BR>
<!-- do not remove --> <BR>
<hr width=50 align=left><small><a href='http://www.mcpressonline.com/mc/showcode@@.6b3369a0/0' target='_blank'>Code</a></small> <BR>
<!--mccodelink_end-->
#119106
MC Press Web Site Staff
iSockets: The Cool, New, Free Way to Do Web Services
Feb 07 2006 19:04:00
This is a discussion about <B>iSockets: The Cool, New, Free Way to Do Web Services</b>.<p align='center'><a href=http://www.mcpressonline.com/mc?1@232.1KNKfHX1eQT.17@.6b32df75>Click here for the article</a>.</p>
#119105


Discuss...
User Rating: / 0
PoorBest 
Related Articles
< Prev   Next >

   MC-STORE.COM