midrange.com code scratchpad
Name:
Russell
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
07/10/2019 07:45:31 pm
IP:
Logged
Description:
Socket Client Connection
Code:
  1. SEU==>                                                                  MODBUS
  2. FMT H  HKEYWORDS++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  3.         *************** BEGINNING OF DATA *************************************
  4. 0001.01 H OPTION(*NODEBUGIO:*SRCSTMT:*NOUNREF)                                 
  5. 0002.00 H DFTACTGRP(*NO) ACTGRP(*NEW)                                          
  6. 0003.00 H BNDDIR('QC2LE') BNDDIR('SOCKTUT/SOCKUTIL')                           
  7. 0004.00 H DATEDIT(*YMD/)                                                       
  8. 0005.00  /copy socktut/qrpglesrc,socket_h                                      
  9. 0006.00  /copy socktut/qrpglesrc,errno_h                                       
  10. 0006.01  /copy socktut/qrpglesrc,sockutil_h                        
  11. 0257.00  *==============================================================*
  12. 0258.00  *  MAIN                                                         
  13. 0259.00  *==============================================================*
  14. 0260.00  /free                                                           
  15. 0261.00                                                                  
  16. 0262.00   // Initialize                                                  
  17. 0263.00      Exsr @INIT;                                                 
  18. 0263.01                                                                  
  19. 0263.02   // Create a Socket                                             
  20. 0285.01      Sock = socket(AF_INET:SOCK_STREAM:IPPROTO_IP);              
  21. 0285.02      If (Sock < 0);                                              
  22. 0285.03        return;                                                   
  23. 0285.05      Endif;                                                      
  24. 0285.06                              
  25. 0285.07   // Ask the operating system for some memory to store socket address 
  26. 0285.08      addrlen = %size(sockaddr);                                       
  27. 0285.09      p_connto = %alloc(addrlen);                                      
  28. 0285.10                                                                       
  29. 0285.11   // Point the socket address structure at the newly alloc mem area   
  30. 0285.12      p_sockaddr = p_connto;                                           
  31. 0285.13                                                                       
  32. 0285.14   // Populate the sockaddr_in structure                               
  33. 0285.15      sin_family = AF_INET;                                            
  34. 0285.16      sin_addr = IP2;                                                  
  35. 0285.17      sin_port = Port;                                                 
  36. 0285.18      sin_zero = *ALLx'00';                                            
  37. 0285.19                                                                       
  38. 0285.20   // Connect to Socket                                                
  39. 0285.21      rc = connect(Sock: p_connto: addrlen);                           
  40. 0285.22      If (Sock < 0);                                                   
  41. 0285.23        callp close(sock);                                             
  42. 0285.24        return;                                                        
  43. 0285.25      Endif;                                                           
  44. 0285.26                                                                       
  45. 0285.27   // Format something to write to register                                                        
  46. 0285.29      request = '0001 0000 0006 01 06 189E 0001';                      
  47. 0285.30      reqlen = %len(%trim(request));                                   
  48. 0285.31      callp  Translate(reqlen: request: 'QTCPASC');                    
  49. 0285.32                                                                       
  50. 0285.33                                                                       
  51. 0285.34   // Send Request                                                     
  52. 0285.35      Exsr @Send;                                                      
  53. 0285.36                                                                       
  54. 0285.40   // Close Socket                                                      
  55. 0285.41      callp close(sock);                                                
  56. 0285.42                                                                        
  57. 0285.45      Return;                                                           
  58. 0286.00      *inlr = *on;                                                      
  59. 0286.01   //==================================================================*
  60. 0286.02   // @Initialize                                                      *
  61. 0286.03   //==================================================================*
  62. 0286.04      Begsr @INIT;                                                      
  63. 0286.14                                                                        
  64. 0286.15   // Today's Date                                                      
  65. 0286.16      RESET DateSys;                                                    
  66. 0286.17      Date8 = YRSYS + MNSYS + DYSYS;                                    
  67. 0286.23                                                                        
  68. 0286.24      host = '199.119.10.157';                                          
  69. 0286.25      IP2 = inet_addr(%trim(host));                                     
  70. 0286.26      Port = 1015;                             
  71. 0286.59      Endsr;                                                            
  72. 0286.60   //==================================================================*
  73. 0286.61   // @SEND                                                             
  74. 0286.62   //==================================================================*
  75. 0286.63      Begsr @SEND;                                                      
  76. 0286.64                                                                        
  77. 0286.65      rc = send(sock: %addr(request): reqlen:0);                        
  78. 0286.66      If rc < reqlen;                                                   
  79. 0286.67        callp close(sock);                                              
  80. 0286.68        Return;                                                         
  81. 0286.69      Endif;                                                            
  82. 0286.70                                                                        
  83. 0286.78                                                                        
  84. 0286.79      Endsr;           
  85.  
© 2004-2019 by midrange.com generated in 0.008s valid xhtml & css