[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

socket connect timeout




How do I control the timeout period when connecting to an invalid url? I only want to wait for max 10 seconds.

these are the steps I am currently taking

c     *entry        plist                                            
c                   parm                    host                    
                                                                     
c                   eval      *inlr = *on                            
                                                                     
c                   eval      IP = inet_addr(%trim(host))            
                                                                     
c                   if        IP = INADDR_NONE                      
c                   eval      p_hostent = gethostbyname(%trim(host))
c                   if        p_hostent <> *NULL                    
c                   eval      IP = h_addr                            
c                   endif                                            
c                   endif                                            
                                                                     
c                   if        IP = INADDR_NONE                      
c                   eval      Msg = %trim(host) + ' host not found!'
C                   dsply                   msg                      
c                   return                                          
c                   else                                            
 *c                   eval      Msg = 'IP = ' + %str(inet_ntoa(IP))  
c                   endif                                            
                                                                     
C*************************************************                  
C* Create a socket                                                  
c     *entry        plist                                            
c                   parm                    host                    
                                                                     
c                   eval      *inlr = *on                            
                                                                     
c                   eval      IP = inet_addr(%trim(host))            
                                                                     
c                   if        IP = INADDR_NONE                      
c                   eval      p_hostent = gethostbyname(%trim(host))
c                   if        p_hostent <> *NULL                    
c                   eval      IP = h_addr                            
c                   endif                                            
c                   endif                                            
                                                                     
c                   if        IP = INADDR_NONE                      
c                   eval      Msg = %trim(host) + ' host not found!'
C                   dsply                   msg                      
c                   return                                          
c                   else                                            
 *c                   eval      Msg = 'IP = ' + %str(inet_ntoa(IP))  
c                   endif                                            
                                                                     
C*************************************************                  
C* Create a socket                                                  
c                   eval      sock = socket(AF_INET: SOCK_STREAM:
c                                           IPPROTO_IP)          
c                   if        sock < 0                          
c                   eval      msg = 'Error calling socket()!'    
c                   dsply                   msg                  
c                   return                                      
c                   endif                                        
                                                                 
C*** tell socket we don't want blocking...                      
c                   eval      flags = fcntl(sock: F_GETFL)      
c                   eval      flags = flags + O_NONBLOCK        
c                   if        fcntl(sock: F_SETFL: flags) < 0    
c                   eval      msg = 'Error calling fcntl()!'    
c                   dsply                   msg                  
c                   return                                      
c                   endif                                        
                                                                 
C*************************************************              
C* Create a socket address structure that                        
C*   describes the host & port we wanted to                      
C*   connect to                                                  
C*************************************************              
c                   eval      addrlen = %size(sockaddr)          
c                   alloc     addrlen       p_connto            
                                                                   
c                   eval      p_sockaddr = p_connto                
c                   eval      sin_family = AF_INET                
c                   eval      sin_addr = IP                        
c                   eval      sin_port = port                      
c                   eval      sin_zero = *ALLx'00'                
                                                                   
C*************************************************                
C* Connect to the requested host                                  
C*************************************************                
c                   Eval      $connect = false                    
c     1             Do        10                                  
c                   Eval      RC = connect(sock: p_connto: addrlen)
c                   If        RC < 0                              
 *c                   eval      msg = 'Not connected...'          
 *c                   dsply                   msg                  
C                   call      'DELAY'                              
C                   Else                                          
c                   Eval      $connect = true                      
c                   eval      msg = 'Connected!'                  
c                   dsply                   msg                    
C                   leave                                          
C                   endif                                          
c                   Enddo                                          

After the above loop I still have a RC that is -1. Any ideas?


Jim Murtha
Software Engineer
Voice: 801.501.7120
Fax: 801.501.0701