32 #include <sys\types.h>
37 #include <sys/types.h>
43 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <sys/socket.h>
69 char cModule[32] =
"rconnect";
72 int iMaxTime = *piMaxTime;
78 struct hostent *pHE = NULL;
80 struct sockaddr_in sSockAddr;
85 fprintf(
fLogFile,
"\n-D- begin %s: try connection to %s:%d",
86 cModule, cNode, iPort);
89 else if (iMaxTime == 0)
90 fprintf(
fLogFile,
" (until success)\n");
92 fprintf(
fLogFile,
" (for %d sec)\n", iMaxTime);
95 if ( ( pHE = gethostbyname(cNode) ) == NULL )
97 lAddr = inet_addr(cNode);
98 if ( ( pHE = gethostbyaddr(
99 (
char *)&lAddr,
sizeof(lAddr), AF_INET ) ) == NULL )
101 fprintf(
fLogFile,
"-E- %s: unknown host %s\n", cModule, cNode );
104 fprintf(
fLogFile,
" %s\n", strerror(errno));
112 fprintf(
fLogFile,
" %s: gethostbyaddr succeeded\n", cModule);
115 fprintf(
fLogFile,
" %s: gethostbyname succeeded\n", cModule);
120 if ( ( iSocket = socket(
121 AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) == -1 )
123 fprintf(
fLogFile,
"-E- %s: socket failed\n", cModule);
126 fprintf(
fLogFile,
" %s\n", strerror(errno));
134 sSockAddr.sin_family = AF_INET;
135 sSockAddr.sin_port = 0;
136 sSockAddr.sin_addr.s_addr = INADDR_ANY;
139 (
struct sockaddr *) &sSockAddr,
140 sizeof(sSockAddr) ) == -1 )
142 fprintf(
fLogFile,
"-E- %s: bind failed\n", cModule);
145 fprintf(
fLogFile,
" %s\n", strerror(errno));
153 sSockAddr.sin_family = AF_INET;
154 sSockAddr.sin_port = htons( (
short) iPort );
155 sSockAddr.sin_addr = * ( (
struct in_addr *) sHE.h_addr );
157 if ( connect( iSocket,
158 (
struct sockaddr *) &sSockAddr,
159 sizeof(sSockAddr) ) == -1 )
164 fprintf(
fLogFile,
"-E- %s: connect to %s:%d failed\n",
165 cModule, cNode, iPort);
168 fprintf(
fLogFile,
" %s\n", strerror(errno));
174 if ( (iTime < iMaxTime) || (iMaxTime == -1) )
177 closesocket(iSocket);
189 if (iSleep < iMaxSleep)
194 " time %d of %d, sleep %d\n",
195 iTime, iMaxTime, iSleep);
199 sleep((
unsigned) iSleep);
210 fprintf(
fLogFile,
"-D- end %s (success after %d sec)\n\n",
211 cModule, *piMaxTime);
218 shutdown(iSocket, 2);
220 closesocket(iSocket);
234 fprintf(
fLogFile,
"-D- end %s", cModule);
236 fprintf(
fLogFile,
" (after %d sec)\n\n", iTime);
int rconnect(char *cNode, int iPort, int *piMaxTime, int *piSocket)