bool server_socket_add_host(struct server_socket *ss, const char *hostname, unsigned port, GError **error_r) { #ifdef HAVE_TCP struct addrinfo *ai = resolve_host_port(hostname, port, AI_PASSIVE, SOCK_STREAM, error_r); if (ai == NULL) return false; for (const struct addrinfo *i = ai; i != NULL; i = i->ai_next) server_socket_add_address(ss, i->ai_addr, i->ai_addrlen); freeaddrinfo(ai); ++ss->next_serial; return true; #else /* HAVE_TCP */ (void)ss; (void)hostname; (void)port; g_set_error(error_r, server_socket_quark(), 0, "TCP support is disabled"); return false; #endif /* HAVE_TCP */ }
int main(int argc, char **argv) { if (argc != 2) { g_printerr("Usage: run_tcp_connect IP:PORT\n"); return 1; } GError *error = NULL; struct addrinfo *ai = resolve_host_port(argv[1], 80, 0, SOCK_STREAM, &error); if (ai == NULL) { g_printerr("%s\n", error->message); g_error_free(error); return EXIT_FAILURE; } /* initialize GLib */ g_thread_init(NULL); /* initialize MPD */ io_thread_init(); if (!io_thread_start(&error)) { freeaddrinfo(ai); g_printerr("%s", error->message); g_error_free(error); return EXIT_FAILURE; } /* open the connection */ mutex = g_mutex_new(); cond = g_cond_new(); tcp_connect_address(ai->ai_addr, ai->ai_addrlen, 5000, &my_tcp_connect_handler, NULL, &handle); freeaddrinfo(ai); if (handle != NULL) { g_mutex_lock(mutex); while (!done) g_cond_wait(cond, mutex); g_mutex_unlock(mutex); tcp_connect_free(handle); } g_cond_free(cond); g_mutex_free(mutex); /* deinitialize everything */ io_thread_deinit(); return EXIT_SUCCESS; }