× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.


  • Subject: Socket connect function problem
  • From: "A.Vishnu Vardhan" <vishvar@xxxxxxxxx>
  • Date: Fri, 8 Oct 1999 00:03:45 -0700 (PDT)

Hi all,

I am trying to establish connection with some other
server from AS/400 using sockets. Server socket is
running in the other server. I am using IP address to
connect to the server. I am able to connect
successfully, but sometimes I get the system message
saying 'connection failed'. I am able to ping from
both the servers. Can any one give solution to this
problem from the below code. 
Thanks in advance.
   
Vishnu


int WEBAPIConnect( char *system, int port )

{
   ulong                addr;       /* Address of
server           */
   ulong                addrdec;    /* Address of
server           */
   int                  sockfd = 0; /* socket
descriptor           */
   int                  rc = 0;     /* return value of
select()    */
   int                  iMax = 0;   /* Maximum value
of descriptor */
   int                  otimeout =0;/* Socket open
time value      */
   int                  iBlock =0;  /* IO block flag  
            */
   char                 *temp =" "; /* Temporary
variable          */
   char              *tmpsockfd = " ";/* To store
sockfd           */
   fd_set               ready;      /* Descriptor set 
            */
   struct hostent       *fxtent;    /* Address
structure           */
   struct sockaddr_in   fxaddr;     /* Address
structure           */
   struct timeval       timeout;    /* Timeout
structure           */



   /* Retrieve socket open timeout value from
configuration data area*/
   memset( temp, 0, sizeof( temp ));
   QXXRTVDA(cfg_dtaname, 36, 8, temp);
   otimeout = atoi( temp );

   /* Initialize the structure containing the address
*/
      memset(&(fxaddr.sin_zero),0,8);

      fxaddr.sin_port = htons(port);
      fxaddr.sin_family = AF_INET;


   /* Resolve whether address in dotted decimal format
or not */
   if( ( addr=inet_addr( system ) )!= INADDR_NONE )
      {

          /* Assign address to sockaddr_in structure
*/
          fxaddr.sin_addr.s_addr =  addr;

         }
   else
        {
        /* Assign address to hostent structure */
        fxtent = gethostbyname( system );

           if( fxtent == ( struct hostent* ) NULL)
            {
               Write_Error( errno);
               return( FAILURE );
            }
           else
            {
               memcpy( &fxaddr.sin_addr,
fxtent->h_addr,
                                     fxtent->h_length
);
            }
         }



   /* Create socket      */
   if( sockfd = socket( AF_INET, SOCK_STREAM, 0 ) < 0)
    {
        Write_Error( errno );
        Write_Log(" Socket not created");
        return( FAILURE );
    }

      /* Make descriptors zero and set them  */
      FD_ZERO(&ready);
      FD_SET(sockfd, &ready);

      /* Set to non blocking  mode*/
      iBlock = 1;
      ioctl(sockfd, FIONBIO, (char *) &iBlock);

      /* Try to establish a connection with the server
*/
      if(connect( sockfd, (struct sockaddr *) &fxaddr,
                         sizeof( fxaddr))< 0)
      {
            if (errno == EINPROGRESS)
             {
               timeout.tv_sec = 0;
               timeout.tv_usec = otimeout;
               iMax = sockfd + 1;
               rc = select(iMax, NULL, &ready, NULL,
&timeout);

               if (rc > 0)
                {
                  iBlock = 0;
                  ioctl(sockfd, FIONBIO, (char *)
&iBlock);
                 }
               if (rc == 0)
                {
                  Write_Error( errno);
                  close(sockfd);
                  return( FAILURE );
                }
               if (rc < 0)
                {

                  Write_Error( errno);
                  close(sockfd);
                  return( FAILURE );
                 }
         }

        else
         {
           Write_Error( errno );
           Write_Log(" Connection not established");
           close(sockfd);
           return( FAILURE );
        }
     }
     iBlock = 0;
     ioctl(sockfd, FIONBIO, (char *) &iBlock);

     /* If connection successful store sockfd in a
temporary data
          area for future retrieval  */
     sprintf( tmpsockfd,"%d",sockfd);
     QXXCHGDA( tmp_dtaname, 1, 4, tmpsockfd );
     return( SUCCESS );
   


=====

__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com
+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.