7 int cancel_timeout = 0;
8 gboolean async = FALSE;
9 gboolean graceful = FALSE;
10 static GOptionEntry cmd_entries[] = {
11 {"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
12 "Cancel any op after the specified amount of seconds", NULL},
13 {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
14 "Use async ops", NULL},
15 {"graceful-disconnect", 'g', 0, G_OPTION_ARG_NONE, &graceful,
16 "Use graceful disconnect", NULL},
21 cancel_thread (gpointer data)
23 GCancellable *cancellable = data;
25 g_usleep (1000*1000*cancel_timeout);
26 g_print ("Cancelling\n");
27 g_cancellable_cancel (cancellable);
32 socket_address_to_string (GSocketAddress *address)
34 GInetAddress *inet_address;
38 inet_address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
39 str = g_inet_address_to_string (inet_address);
40 port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
41 res = g_strdup_printf ("%s:%d", str, port);
47 async_cb (GObject *source_object,
51 GAsyncResult **resp = user_data;
52 *resp = g_object_ref (res);
53 g_main_loop_quit (loop);
58 main (int argc, char *argv[])
60 GOptionContext *context;
61 GSocketClient *client;
62 GSocketConnection *connection;
63 GSocketAddress *address;
64 GCancellable *cancellable;
72 context = g_option_context_new (" <hostname>[:port] - send data to tcp host");
73 g_option_context_add_main_entries (context, cmd_entries, NULL);
74 if (!g_option_context_parse (context, &argc, &argv, &error))
76 g_printerr ("%s: %s\n", argv[0], error->message);
82 g_printerr ("%s: %s\n", argv[0], "Need to specify hostname");
87 loop = g_main_loop_new (NULL, FALSE);
91 cancellable = g_cancellable_new ();
92 g_thread_create (cancel_thread, cancellable, FALSE, NULL);
99 client = g_socket_client_new ();
100 connection = g_socket_client_connect_to_host (client,
103 cancellable, &error);
104 if (connection == NULL)
106 g_printerr ("%s can't connect: %s\n", argv[0], error->message);
110 address = g_socket_connection_get_remote_address (connection, &error);
113 g_printerr ("Error getting remote address: %s\n",
117 g_print ("Connected to address: %s\n",
118 socket_address_to_string (address));
119 g_object_unref (address);
122 g_tcp_connection_set_graceful_disconnect (G_TCP_CONNECTION (connection), TRUE);
124 out = g_io_stream_get_output_stream (G_IO_STREAM (connection));
126 while (fgets(buffer, sizeof (buffer), stdin) != NULL)
128 if (!g_output_stream_write_all (out, buffer, strlen (buffer),
129 NULL, cancellable, &error))
131 g_warning ("send error: %s\n", error->message);
132 g_error_free (error);
137 g_print ("closing stream\n");
141 g_io_stream_close_async (G_IO_STREAM (connection),
142 0, cancellable, async_cb, &res);
143 g_main_loop_run (loop);
144 if (!g_io_stream_close_finish (G_IO_STREAM (connection),
147 g_object_unref (res);
148 g_warning ("close error: %s\n", error->message);
151 g_object_unref (res);
155 if (!g_io_stream_close (G_IO_STREAM (connection), cancellable, &error))
157 g_warning ("close error: %s\n", error->message);