4.8. improvement #7: Displaying the data using DSPF

I am writing a tutorial on how to use stream files from RPG, but as of the time that I'm writing this page, I haven't yet finished it. Even so, though, I don't want to take away from the socket tutorial to try to teach stream files, so... I'll just assume that you either already know how stream files work, or that you've been able to find a tutorial somewhere.

Since data is being returned from the socket in a stream format, and we want to write it to our stream file in a stream format, it's very easy to write the web page data to a stream file! In fact, we won't even bother breaking the data that we receive into 'lines of text', but rather just dump it all (as-is) into the stream file.

When the DSPF command is run, it'll do the job of breaking things up into lines of text.

So, first we will open a stream file to contain the data we receive:

         c                   eval      fd = open('/http_tempfile.txt':           
         c                                  O_WRONLY+O_TRUNC+O_CREAT+O_CODEPAGE: 
         c                                  511: 437)                            
         c                   if        fd < 0                                    
         c                   eval      err = errno                               
         c                   callp     close(sock)                               
         c                   callp     Die('open(): '+%str(strerror(err)))       
         c                   return                                              
         c                   endif                                               

Next, we'll write whatever we receive from the socket into the stream file, without changing a thing:

         c                   dou       rc < 1                              
         c                   eval      rc = recv(sock: %addr(recbuf):      
         c                                         %size(recbuf): 0)       
         c                   if        rc > 0                              
         c                   callp     write(fd: %addr(recbuf): rc)        
         c                   endif                                         
         c                   enddo                                         

After we've closed the file and the socket, we'll display the file using IBM's DSPF command:

         c                   callp     close(fd)                                
         c                   callp     close(sock)                              
         c                   callp     Cmd('DSPF STMF(''/http_tempfile.txt'')': 
         c                                200)                                  

Finally, we'll delete our temporary stream file using the 'unlink' API call:

         c                   callp     unlink('/http_tempfile.txt')             

WooHoo! We've gotten rid of all of the nasty DSPLY opcodes, now!