Async connecting *almost* works. For some reason the G_IO_OUT condition
[cascardo/gnio.git] / test / test-tcp-client.c
index d9c7679..8620ba4 100644 (file)
@@ -3,25 +3,28 @@
 
 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);
        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");
 }
-*/
 
 int main (int argc, char *argv[])
 {
        GTcpClient *client;
-       GError *error = NULL;
+/*     GInputStream *input;
+       GOutputStream *output;
+       gchar buffer[512] = {0};
+       gssize count;
+       GError *error = NULL;*/
 
        g_thread_init (NULL);
 
@@ -29,20 +32,44 @@ 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", 80);
 
        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;
+               return 1;
        }
 
        g_print ("connected!\n");
 
-       g_object_unref (G_OBJECT (client));
+       output = G_OUTPUT_STREAM (g_tcp_client_get_output_stream (client));
+
+       input = G_INPUT_STREAM (g_tcp_client_get_input_stream (client));
 
-//     g_main_loop_run (loop);
+       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 1;
+       }
+
+       g_print ("wrote %d bytes\n", count);
+
+       if ((count = g_input_stream_read (input, buffer, 512, NULL, &error)) < 0) {
+               g_warning (error->message);
+               return 1;
+       }
+
+       g_print ("read %d bytes: %s\n", count, buffer);
+
+       g_object_unref (G_OBJECT (client));
+*/
+       g_main_loop_run (loop);
 
        return 0;
 }