7 int cancel_timeout = 0;
9 gboolean async = FALSE;
10 gboolean graceful = FALSE;
11 static GOptionEntry cmd_entries[] = {
12 {"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
13 "Cancel any op after the specified amount of seconds", NULL},
14 {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
15 "Use async ops", NULL},
16 {"graceful-disconnect", 'g', 0, G_OPTION_ARG_NONE, &graceful,
17 "Use graceful disconnect", NULL},
18 {"timeout", 't', 0, G_OPTION_ARG_INT, &io_timeout,
19 "Time out socket I/O after the specified number of seconds", NULL},
24 cancel_thread (gpointer data)
26 GCancellable *cancellable = data;
28 g_usleep (1000*1000*cancel_timeout);
29 g_print ("Cancelling\n");
30 g_cancellable_cancel (cancellable);
35 socket_address_to_string (GSocketAddress *address)
37 GInetAddress *inet_address;
41 inet_address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
42 str = g_inet_address_to_string (inet_address);
43 port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
44 res = g_strdup_printf ("%s:%d", str, port);
50 async_cb (GObject *source_object,
54 GAsyncResult **resp = user_data;
55 *resp = g_object_ref (res);
56 g_main_loop_quit (loop);
61 main (int argc, char *argv[])
63 GOptionContext *context;
64 GSocketClient *client;
65 GSocketConnection *connection;
66 GSocketAddress *address;
67 GCancellable *cancellable;
75 context = g_option_context_new (" <hostname>[:port] - send data to tcp host");
76 g_option_context_add_main_entries (context, cmd_entries, NULL);
77 if (!g_option_context_parse (context, &argc, &argv, &error))
79 g_printerr ("%s: %s\n", argv[0], error->message);
85 g_printerr ("%s: %s\n", argv[0], "Need to specify hostname");
90 loop = g_main_loop_new (NULL, FALSE);
94 cancellable = g_cancellable_new ();
95 g_thread_create (cancel_thread, cancellable, FALSE, NULL);
102 client = g_socket_client_new ();
104 g_socket_client_set_timeout (client, io_timeout);
109 g_socket_client_connect_to_host_async (client, argv[1], 7777,
110 cancellable, async_cb, &res);
111 g_main_loop_run (loop);
112 connection = g_socket_client_connect_to_host_finish (client, res, &error);
113 g_object_unref (res);
117 connection = g_socket_client_connect_to_host (client,
120 cancellable, &error);
122 if (connection == NULL)
124 g_printerr ("%s can't connect: %s\n", argv[0], error->message);
127 g_object_unref (client);
129 address = g_socket_connection_get_remote_address (connection, &error);
132 g_printerr ("Error getting remote address: %s\n",
136 g_print ("Connected to address: %s\n",
137 socket_address_to_string (address));
138 g_object_unref (address);
141 g_tcp_connection_set_graceful_disconnect (G_TCP_CONNECTION (connection), TRUE);
143 out = g_io_stream_get_output_stream (G_IO_STREAM (connection));
145 while (fgets(buffer, sizeof (buffer), stdin) != NULL)
147 /* FIXME if (async) */
148 if (!g_output_stream_write_all (out, buffer, strlen (buffer),
149 NULL, cancellable, &error))
151 g_warning ("send error: %s\n", error->message);
152 g_error_free (error);
157 g_print ("closing stream\n");
161 g_io_stream_close_async (G_IO_STREAM (connection),
162 0, cancellable, async_cb, &res);
163 g_main_loop_run (loop);
164 if (!g_io_stream_close_finish (G_IO_STREAM (connection),
167 g_object_unref (res);
168 g_warning ("close error: %s\n", error->message);
171 g_object_unref (res);
175 if (!g_io_stream_close (G_IO_STREAM (connection), cancellable, &error))
177 g_warning ("close error: %s\n", error->message);
182 g_object_unref (connection);