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 ();
104 g_socket_client_connect_to_host_async (client, argv[1], 7777,
105 cancellable, async_cb, &res);
106 g_main_loop_run (loop);
107 connection = g_socket_client_connect_to_host_finish (client, res, &error);
108 g_object_unref (res);
112 connection = g_socket_client_connect_to_host (client,
115 cancellable, &error);
117 if (connection == NULL)
119 g_printerr ("%s can't connect: %s\n", argv[0], error->message);
122 g_object_unref (client);
124 address = g_socket_connection_get_remote_address (connection, &error);
127 g_printerr ("Error getting remote address: %s\n",
131 g_print ("Connected to address: %s\n",
132 socket_address_to_string (address));
133 g_object_unref (address);
136 g_tcp_connection_set_graceful_disconnect (G_TCP_CONNECTION (connection), TRUE);
138 out = g_io_stream_get_output_stream (G_IO_STREAM (connection));
140 while (fgets(buffer, sizeof (buffer), stdin) != NULL)
142 /* FIXME if (async) */
143 if (!g_output_stream_write_all (out, buffer, strlen (buffer),
144 NULL, cancellable, &error))
146 g_warning ("send error: %s\n", error->message);
147 g_error_free (error);
152 g_print ("closing stream\n");
156 g_io_stream_close_async (G_IO_STREAM (connection),
157 0, cancellable, async_cb, &res);
158 g_main_loop_run (loop);
159 if (!g_io_stream_close_finish (G_IO_STREAM (connection),
162 g_object_unref (res);
163 g_warning ("close error: %s\n", error->message);
166 g_object_unref (res);
170 if (!g_io_stream_close (G_IO_STREAM (connection), cancellable, &error))
172 g_warning ("close error: %s\n", error->message);
177 g_object_unref (connection);