* Also CLOSE that socket if the connection attempt fails. This also fixes a socket descriptor leak that's been in the previous implementation pretty much forever.
return(-1);
}
- sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
- if (sock < 0) {
- CtdlLogPrintf(CTDL_ERR, "socket() failed: %s\n", strerror(errno));
- return(-1);
- }
-
/*
* Try all available addresses until we connect to one or until we run out.
*/
struct addrinfo *ai;
for (ai = res; ai != NULL; ai = ai->ai_next) {
/* FIXME display the address to which we are trying to connect */
+
+ sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+ if (sock < 0) {
+ CtdlLogPrintf(CTDL_ERR, "socket() failed: %s\n", strerror(errno));
+ return(-1);
+ }
+
rc = connect(sock, res->ai_addr, res->ai_addrlen);
if (rc >= 0) {
return(sock);
}
else {
CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
+ close(sock);
}
}
newprompt("Connect to (return for local server): ", hostbuf, 64);
#endif
- sln_printf("Attaching to server... \r");
- sln_flush();
+ sln_printf("Attaching to server...\n");
ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
if (!ipc) {
screen_delete();
return(-1);
}
- sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
- if (sock < 0) {
- // CtdlLogPrintf(CTDL_ERR, "socket() failed: %s\n", strerror(errno));
- return(-1);
- }
-
/*
* Try all available addresses until we connect to one or until we run out.
*/
struct addrinfo *ai;
for (ai = res; ai != NULL; ai = ai->ai_next) {
/* FIXME display the address to which we are trying to connect */
- rc = connect(sock, res->ai_addr, res->ai_addrlen);
+fprintf(stderr, "TRYING...\n");
+
+ sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+ if (sock < 0) return(-1);
+
+ rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
if (rc >= 0) {
+fprintf(stderr, "CONNECTED\n");
return(sock);
}
else {
+fprintf(stderr, "FAILED: %s\n", strerror(errno));
// CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
+ close(sock);
}
}