7 connect_callback (GObject *source, GAsyncResult *result, gpointer data)
9 GTcpClient *client = G_TCP_CLIENT (source);
11 GOutputStream *output;
12 gchar buffer[512] = {0};
16 if (!g_tcp_client_connect_finish (client, result, &error)) {
17 g_warning (error->message);
21 g_print ("successfully connected\n");
23 output = G_OUTPUT_STREAM (g_tcp_client_get_output_stream (client));
25 input = G_INPUT_STREAM (g_tcp_client_get_input_stream (client));
27 g_print ("writing...\n");
29 if ((count = g_output_stream_write (output, "GET / HTTP/1.0\r\n\r\n", 19, NULL, &error)) < 0) {
30 g_warning (error->message);
34 g_print ("wrote %" G_GSSIZE_FORMAT " bytes\n", count);
36 if ((count = g_input_stream_read (input, buffer, 512, NULL, &error)) < 0) {
37 g_warning (error->message);
41 g_print ("read %" G_GSSIZE_FORMAT " bytes: %s\n", count, buffer);
44 int main (int argc, char *argv[])
52 loop = g_main_loop_new (NULL, FALSE);
54 client = g_tcp_client_new ("localhost", 31882);
56 g_print ("connecting to www.google.com:80\n");
58 g_tcp_client_connect_async (client, NULL, connect_callback, NULL);
60 g_print ("connecting seems to have begun\n");
62 if (!g_tcp_client_connect (client, NULL, &error)) {
63 g_warning (error->message);
67 g_print ("connected!\n");
69 g_object_unref (G_OBJECT (client));
72 g_main_loop_run (loop);