by rexxer » Tue Jul 20, 2010 12:57 am
Robert:
The netstat command is disabled for me on our z/OS.
Well, I read couple of articles, made some changes to the code and now have a working version of the client application. Now I have to figure out how to communicate with a server application residing on different server (different IP address) and running on a secure server (I am using a http server while the server application is on a https server). Any help would be greatly appreciated.
Here is the revised rexx code:
/*rexx*/
/*----------------------------------------------------------------+
| Rexx program to simulate a client application which will talk to|
| the java server application AlienServer. This program showcases |
| the usage of sockets in Rexx. |
| |
+----------------------------------------------------------------*/
rc = 0;
fc = SOCKET('Terminate');
Call !HTTP_Write '<html><head><title>CMS socket try.</title></head>';
Call !HTTP_Write '<body>';
fc = SOCKET('Initialize','CLIENT',2);
parse var fc socket_rc .
if socket_rc <> 0 then
Do;
Call !HTTP_Write 'INITIALIZE failed with return info ' fc '<br />';
Call http_end;
Exit 200;
End;
Call !HTTP_Write 'Socket initialized<br />';
fc = SOCKET('SOCKET',2,'SOCK_STREAM','TCP');
parse var fc socket_rc newsocketid .
if socket_rc <> 0 then
Do;
Call !HTTP_Write 'SOCKET failed with return info ' fc '<br />';
fc = SOCKET('TERMINATE');
Call http_end;
Exit 200;
End;
fc = SOCKET('Socketsetstatus','CLIENT');
Call !HTTP_Write 'Socket status is: ' fc '<br />';
serv = SOCKET('GetHostId');
parse var serv servrc ipaddress;
if servrc <> 0 then
Do;
Call !HTTP_Write 'Cannot get local IP Address. rc=' servrc '<br />';
Call http_end;
Exit 200;
End;
Call !HTTP_Write 'The local IP address is ' ipaddress '<br />';
fc = SOCKET('CONNECT',newsocketid,'AF_INET' 4444 ipaddress);
parse var fc connect_rc rest
if connect_rc <> 0 then
Do;
Call !HTTP_Write 'CONNECT failed with return info ' fc '<br />';
rc = 99;
Call SHUTDOWN_LABEL;
Call http_end;
Exit 200;
End;
CR = D2C(13);
Call !HTTP_Write 'CONNECTION ESTABLISED <br />';
fc = SOCKET('SEND',newsocketid,'hi there friend'||CR);
parse var fc send_rc num_sent_bytes
if send_rc <> 0 then
Do;
Call !HTTP_Write 'SEND failed with return info ' fc '<br />';
rc = 99;
Call SHUTDOWN_LABEL;
Call http_end;
Exit 200;
End;
if length('hi there friend'||CR) <> num_sent_bytes then
Do;
Call !HTTP_Write 'number of sbytes does not match number of rbytes';
Call !HTTP_Write '<br />';
rc = 99;
Call SHUTDOWN_LABEL;
Call http_end;
Exit 200;
End;
received_data = Read_from_socket();
parse var received_data rdata_rc . received_string
if rdata_rc <> 0 then
Do;
Call !HTTP_Write 'string received failed. rdata_rc: ' rdata_rc;
Call !HTTP_Write '<br />';
Call http_end;
Exit 200;
End;
Call !HTTP_Write 'Received string: ' received_string '<br />';
Call SHUTDOWN_LABEL;
Call http_end;
Return 200;
/*- SHUTDOWN_LABEL ---------------------------------+
| Call this function to close the socket. |
+--------------------------------------------------*/
SHUTDOWN_LABEL:
fc = SOCKET('CLOSE',newsocketid);
parse var fc close_rc rest
if close_rc <> 0 then
Do;
Call !HTTP_Write 'CLOSE failed with return info ' fc '<br />';
fc = SOCKET('TERMINATE');
End;
Call !HTTP_Write 'SOCKET TERMINATED <br />';
return;
/*- Peek_at_socket ---------------------------------+
| Call this function to see if more data is to be |
| received from sender |
+--------------------------------------------------*/
Peek_at_socket:
Returned_data = Socket('Recv',newsocketid,1024,'PEEK');
parse var Returned_data s_rc s_type s_port s_ip s_results
parse var Returned_data s_rc s_data_len s_data_text
if s_rc <> 0 then
Do;
Call !HTTP_Write 'Peek at socket failed. fc = ' Returned_data '<br />';
Call http_end;
Exit 200;
End;
Return Returned_data;
/*- Read_from_socket ----------------------------------+
| Call this function to read from the socket (sender) |
+-----------------------------------------------------*/
Read_from_socket:
Response = '';
do forever
Returned_data = peek_at_socket();
If s_rc <> 0 then
Do;
Call !HTTP_Write 'I tried to peek but had a problem: ' s_rc '<br />';
Call !HTTP_Write 'return_data: ' Returned_data '<br />';
Call http_end;
Exit 200;
End;
Response = Response||Returned_data;
If s_data_len = 0 then
Do;
Return Response;
end;
return Response;
/*- http_end ---------------------------------------------------------+
| Function to end the html tags. |
---------------------------------------------------------------------*/
http_end:
Call !HTTP_Write '</body></html>';
Return;
/*- !HTTP_Write ------------------------------------------------------+
| Call the HTTP Write service |
+--------------------------------------------------------------------*/
!HTTP_Write:
Parse arg pout
ADDRESS LINKMVS 'IMWXWRT pout'
Return;