Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/RawAPI/rconnect.c

Go to the documentation of this file.
00001 //---------------------------------------------------------------
00002 //        Go4 Release Package v2.10-5 (build 21005) 
00003 //                      03-Nov-2005
00004 //---------------------------------------------------------------
00005 //       The GSI Online Offline Object Oriented (Go4) Project
00006 //       Experiment Data Processing at DVEE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 /*********************************************************************
00017  * Copyright:
00018  *   GSI, Gesellschaft fuer Schwerionenforschung mbH
00019  *   Planckstr. 1
00020  *   D-64291 Darmstadt
00021  *   Germany
00022  * created 26. 1.1996 by Horst Goeringer
00023  *********************************************************************
00024  * rconnect.c
00025  * open connection to specified server
00026  *********************************************************************
00027  *  7. 4.1999, H.G.: return error number in case of failure
00028  *                   pass max time for retries of connect()
00029  *  3. 3.2000, H.G.: rename iExit to imySigS
00030  *                   *piMaxTime: meaning of values -1 and 0 exchanged
00031  *  7. 3.2000, H.G.: renamed form rawConnect to rconnect
00032  * 31.10.2001, H.G.: ported to W2000
00033  * 14.10.2002, H.G.: ported to Lynx
00034  *********************************************************************
00035  */
00036 
00037 #include <stdio.h>
00038 #include <string.h>
00039 #include <stdlib.h>
00040 
00041 #ifdef WIN32          /* Windows */
00042 #include <sys\types.h>
00043 #include <winsock.h>
00044 #include <windows.h>
00045 #include <process.h>
00046 #else                 /* Unix */
00047 
00048 #ifdef Lynx
00049 #include <sys/types.h>
00050 #else
00051 #include <sys/types.h>
00052 #endif
00053 
00054 #include <sys/socket.h>
00055 #include <errno.h>
00056 #include <netdb.h>
00057 
00058 #ifdef Linux
00059 //#include <linux/in.h>
00060 //#include <linux/inet.h>
00061 #elif Lynx
00062 #include <netinet/in.h>
00063 #else               /* _AIX */
00064 #include <in.h>
00065 #endif
00066 
00067 #endif              /* end Unix */
00068 
00069 #if defined(VMS)
00070 #include <inet.h>
00071 #include <signal.h>
00072 #include <unixio.h>
00073 #endif
00074 
00075 extern int imySigS;                      /* if = 1: CTL C specified */
00076 
00077 /********************************************************************/
00078 
00079 int rconnect( char *cNode,        /* input:  name of remote host */
00080               int iPort,          /* input:  port number */
00081               int *piMaxTime,     /* input:  max time for connect */
00082                                   /* = -1: try only once */
00083                                   /* =  0: try until success */
00084                                   /* >  0: try at most *piMaxTime s */
00085                                   /* output: time needed for connect */
00086               int *piSocket)      /* output: socket number */
00087 {
00088    char cModule[32] = "rconnect";
00089    int iDebug = 0;
00090    int iSocket = 0;
00091    int iMaxTime = *piMaxTime;
00092    int iTime = 0;
00093    int iError;
00094    int iSleep = 0;
00095    int iMaxSleep = 10;             /* max sleep time */
00096 
00097    struct hostent *pHE = NULL;
00098    struct hostent  sHE;
00099    struct sockaddr_in sSockAddr;
00100    unsigned long lAddr;
00101 
00102    if ( ( pHE = gethostbyname(cNode) ) == NULL )
00103    {
00104       lAddr = inet_addr(cNode);
00105       if ( ( pHE = gethostbyaddr(
00106                (char *)&lAddr, sizeof(lAddr), AF_INET ) ) == NULL )
00107       {
00108          fprintf(stderr,"-E- %s: unknown host %s\n", cModule, cNode );
00109          iError = -1;
00110          goto gError;
00111       }
00112       if (iDebug)
00113          printf("-D- %s: gethostbyaddr succeeded\n", cModule);
00114    }
00115    else if (iDebug)
00116       printf("-D- %s: gethostbyname succeeded\n", cModule);
00117    sHE = *pHE;                                        /* safe copy */
00118 
00119 gRetryConnect:;
00120 
00121    /* create the socket */
00122    if ( ( iSocket = socket(
00123             AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) == -1 )
00124    {
00125       perror("-E- rconnect(socket)");
00126       iError = errno;
00127       goto gError;
00128    }
00129 
00130    sSockAddr.sin_family = AF_INET;
00131    sSockAddr.sin_port   = 0;
00132    sSockAddr.sin_addr.s_addr = INADDR_ANY;
00133                                  /* bind a socket */
00134    if ( bind ( iSocket,
00135                (struct sockaddr *) &sSockAddr,
00136                sizeof(sSockAddr) ) == -1 )
00137    {
00138       perror("-E- rconnect(bind)");
00139       iError = errno;
00140       goto gError;
00141    }
00142 
00143    sSockAddr.sin_family = AF_INET;
00144    sSockAddr.sin_port   = htons( (short) iPort );
00145    sSockAddr.sin_addr = * ( (struct in_addr *) sHE.h_addr );
00146                                  /* contact remote server */
00147    if ( connect( iSocket,
00148                  (struct sockaddr *) &sSockAddr,
00149                  sizeof(sSockAddr) ) == -1 )
00150    {
00151       /* if not successful, retry.  Possibly server not yet up. */
00152 #ifdef WIN32
00153       if ( (iTime < iMaxTime) || (iMaxTime == -1) )
00154       {
00155          closesocket(iSocket);
00156          iSocket = -1;
00157 #else
00158       if ( (errno == ECONNREFUSED) &&
00159            ( (iTime < iMaxTime) || (iMaxTime == -1) ) )
00160       {
00161          close(iSocket);
00162          iSocket = -1;
00163          if (imySigS) goto gError;            /* CTL C specified */
00164 #endif
00165 
00166          if (iSleep < iMaxSleep) iSleep++;
00167          iTime += iSleep;
00168          if (iDebug)
00169             printf("    still trying (after %d sec) ...\n",iSleep);
00170 #ifdef WIN32
00171          Sleep(iSleep*1000);
00172 #else
00173          sleep(iSleep);
00174 #endif
00175          goto gRetryConnect;
00176       }
00177 #ifdef WIN32
00178       iError = -1;           /* errno, perror indicate NO error! */
00179 #else
00180       perror("-E- rconnect(connect)");
00181       iError = errno;
00182 #endif
00183       goto gError;
00184    }
00185 
00186    *piMaxTime = iTime;         /* return time needed for connect */
00187    *piSocket = iSocket;        /* return socket number */
00188    return(0);                  /* no error */
00189 
00190 gError:
00191    if (iSocket)
00192    {
00193       shutdown(iSocket, 2);
00194 #ifdef WIN32
00195       closesocket(iSocket);
00196 #else
00197       close(iSocket);
00198 #endif
00199    }
00200 
00201    *piSocket = -1;
00202    *piMaxTime = -1;
00203    return iError;
00204 }
00205 
00206 
00207 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Tue Nov 8 10:56:10 2005 for Go4-v2.10-5 by doxygen1.2.15