iTP Active Transaction Pages (iTP ATP) Programmer's Guide
ATP Web Page and Script Structure
iTP Active Transaction Pages (iTP ATP) Programmer’s Guide—522292-002
3-16
Using TCP/IP Sockets from JavaScript
Example 3-9. Code for TCP/IP Sockets Script - Error Handling
// Report socket related errors .....
function handleErrors (sock, comment) {
 if (sock.error != 0) {
 atp.print ('TCP/IP socket error: ',comment,' - ',
 sock.error, ' ', sock.error_text, '\n'); 
 sock.close();
 atp.quit (1);
 }
 }
// Report SMTP related errors .....
function handleReply (sock, comment, alsoGood) {
 var str, buf;
 var eol;
 var chr;
 var rc=0;
 buf = '';
 eol = '';
 chr = '';
 sock.setSoTimeout(3000);
 while (true) {
 chr = sock.read(1);
 // atp.print ('char='+chr+';error='+sock.error+'\n');
 if (chr == null)
 break;
 handleErrors (sock, comment);
 sock.setSoTimeout(30);
 buf += chr;
 }
 if (buf.length == 0)
 return 0;
 // atp.print ("\n<br>"+buf+"\n"); 
 while (true) {
 rc = parseInt (buf);
 if ((rc >= 200 && rc < 300) ||
 (handleReply.arguments.length==3 && rc==alsoGood))
 ;
 else { 
 atp.print ('SMTP error: ',comment,' - ', buf, '\n');
 sock.close();
 atp.quit (1);
 } 
 eol = buf.indexOf ('\n');
 if (eol == -1)
 break;
 buf = buf.substring(eol+1,buf.length);
 if (buf.length == 0)
 break;
 }
 return rc;










