int main(int argc, char **argv) { addr_t addr; double timeout = 5.0; int sock; if (argc < 2) { printf("Usage: connect host:port [timeout_in_seconds]\n"); return 1; } if (argc > 2) { timeout = atof(argv[2]); if (timeout <= 0) { timeout = 5.0; printf("Warning: using timeout %gs\n", timeout); } } printf("Resolving...\n"); if (init_addr(&addr, argv[1]) != 0) perror_fatal("init_addr(%s)", argv[1]); sock = socket(addr.name.sa_family, SOCK_STREAM, 0); if (sock == -1) perror_fatal("socket"); printf("Connecting...\n"); if (nonblock_connect(sock, &addr.name, addr.namelen, timeout * 1000) == -1) perror_fatal("connect(%s)", addr.a_addr); else printf("Success!\n"); return 0; }
int connect_to(const char *host, int port) { addr_t a; char buf[64]; int s; sprintf(buf, "%s:%d", host, port); if (init_addr(&a, buf) != 0) perror_fatal("connect %s:%u", host, port); free(a.a_addr); s = socket(a.name.sa_family, SOCK_STREAM, 0); if (s == -1) perror_fatal("socket"); if (nonblock_connect(s, &a.name, a.namelen, 1000) != 0) perror_fatal("connect(%s:%d)", host, port); return s; }
static int command_start (workerState * ws, char *ebuf, int ebuf_sz) { int ret; assert (ws->fd == -1); //TODO need this? ws->reconn_after = currtime_millis () + RECONNECT_INTERVAL; ws->fd = nonblock_connect (ws, ebuf, ebuf_sz); if (ws->fd < 0) { return -1; } ret = aeCreateFileEvent (ws->el, ws->fd, AE_READABLE, response_handler, ws); assert (ret != AE_ERR); ret = emit_command (ws); assert (ret >= 0); return 0; }