31 #include <sys\types.h>
36 #include <sys/types.h>
42 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <sys/socket.h>
67 char cModule[32] =
"rconnect";
70 int iMaxTime = *piMaxTime;
76 struct hostent *pHE = NULL;
78 struct sockaddr_in sSockAddr;
83 printf(
"\n-D- begin %s: try connection to %s:%d",
84 cModule, cNode, iPort);
86 printf(
" (1 trial)\n");
87 else if (iMaxTime == 0)
88 printf(
" (until success)\n");
90 printf(
" (for %d sec)\n", iMaxTime);
93 if ( ( pHE = gethostbyname(cNode) ) == NULL )
95 lAddr = inet_addr(cNode);
96 if ( ( pHE = gethostbyaddr(
97 (
char *)&lAddr,
sizeof(lAddr), AF_INET ) ) == NULL )
99 printf(
"-E- %s: unknown host %s\n", cModule, cNode );
104 printf(
" %s: gethostbyaddr succeeded\n", cModule);
107 printf(
" %s: gethostbyname succeeded\n", cModule);
112 if ( ( iSocket = socket(
113 AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) == -1 )
115 printf(
"-E- %s: socket failed\n", cModule);
118 printf(
" %s\n", strerror(errno));
126 sSockAddr.sin_family = AF_INET;
127 sSockAddr.sin_port = 0;
128 sSockAddr.sin_addr.s_addr = INADDR_ANY;
131 (
struct sockaddr *) &sSockAddr,
132 sizeof(sSockAddr) ) == -1 )
134 printf(
"-E- %s: bind failed\n", cModule);
137 printf(
" %s\n", strerror(errno));
145 sSockAddr.sin_family = AF_INET;
146 sSockAddr.sin_port = htons( (
short) iPort );
147 sSockAddr.sin_addr = * ( (
struct in_addr *) sHE.h_addr );
149 if ( connect( iSocket,
150 (
struct sockaddr *) &sSockAddr,
151 sizeof(sSockAddr) ) == -1 )
156 printf(
"-E- %s: connect to %s:%d failed\n", cModule, cNode, iPort);
159 printf(
" %s\n", strerror(errno));
165 if ( (iTime < iMaxTime) || (iMaxTime == -1) )
168 closesocket(iSocket);
180 if (iSleep < iMaxSleep)
185 " time %d of %d, sleep %d\n",
186 iTime, iMaxTime, iSleep);
190 sleep((
unsigned) iSleep);
201 printf(
"-D- end %s (success after %d sec)\n\n", cModule, *piMaxTime);
208 shutdown(iSocket, 2);
210 closesocket(iSocket);
224 printf(
"-D- end %s", cModule);
226 printf(
" (after %d sec)\n\n", iTime);
int rconnect(char *cNode, int iPort, int *piMaxTime, int *piSocket)