X-Git-Url: http://git.cascardo.info/?p=cascardo%2Fgnio.git;a=blobdiff_plain;f=test%2Ftest-tcp-client.c;fp=test%2Ftest-tcp-client.c;h=771da641fb8c1444aa89b2d6fe2bbb7dd3be3531;hp=f13aae82d258180c959ebb53866c8275fdd7f7c2;hb=932359e12259081300067d273febe261eee0c870;hpb=5183a9b80c8a22531948af76f4433f0a1a510cbc diff --git a/test/test-tcp-client.c b/test/test-tcp-client.c index f13aae8..771da64 100644 --- a/test/test-tcp-client.c +++ b/test/test-tcp-client.c @@ -3,25 +3,47 @@ GMainLoop *loop; -/* -void -accept_callback (GSocket *socket, GAsyncResult *result, gpointer data) +static void +connect_callback (GObject *source, GAsyncResult *result, gpointer data) { + GTcpClient *client = G_TCP_CLIENT (source); + GInputStream *input; + GOutputStream *output; + gchar buffer[512] = {0}; + gssize count; GError *error = NULL; - if (!g_socket_connect_finish (socket, result, &error)) { + if (!g_tcp_client_connect_finish (client, result, &error)) { g_warning (error->message); return; } g_print ("successfully connected\n"); + + output = G_OUTPUT_STREAM (g_tcp_client_get_output_stream (client)); + + input = G_INPUT_STREAM (g_tcp_client_get_input_stream (client)); + + g_print ("writing...\n"); + + if ((count = g_output_stream_write (output, "GET / HTTP/1.0\r\n\r\n", 19, NULL, &error)) < 0) { + g_warning (error->message); + return; + } + + g_print ("wrote %" G_GSSIZE_FORMAT " bytes\n", count); + + if ((count = g_input_stream_read (input, buffer, 512, NULL, &error)) < 0) { + g_warning (error->message); + return; + } + + g_print ("read %" G_GSSIZE_FORMAT " bytes: %s\n", count, buffer); } -*/ int main (int argc, char *argv[]) { GTcpClient *client; - GError *error = NULL; g_thread_init (NULL); @@ -29,10 +51,14 @@ int main (int argc, char *argv[]) loop = g_main_loop_new (NULL, FALSE); - client = g_tcp_client_new ("localhost", 90); + client = g_tcp_client_new ("localhost", 31882); g_print ("connecting to www.google.com:80\n"); + g_tcp_client_connect_async (client, NULL, connect_callback, NULL); + + g_print ("connecting seems to have begun\n"); +/* if (!g_tcp_client_connect (client, NULL, &error)) { g_warning (error->message); return 1; @@ -41,8 +67,10 @@ int main (int argc, char *argv[]) g_print ("connected!\n"); g_object_unref (G_OBJECT (client)); +*/ -// g_main_loop_run (loop); + g_main_loop_run (loop); return 0; } +