7 int cancel_timeout = 0;
9 gboolean async = FALSE;
10 gboolean graceful = FALSE;
11 gboolean verbose = FALSE;
12 static GOptionEntry cmd_entries[] = {
13 {"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
14 "Cancel any op after the specified amount of seconds", NULL},
15 {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
16 "Use async ops", NULL},
17 {"graceful-disconnect", 'g', 0, G_OPTION_ARG_NONE, &graceful,
18 "Use graceful disconnect", NULL},
19 {"timeout", 't', 0, G_OPTION_ARG_INT, &io_timeout,
20 "Time out socket I/O after the specified number of seconds", NULL},
21 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
22 "Verbose debugging output", NULL},
27 cancel_thread (gpointer data)
29 GCancellable *cancellable = data;
31 g_usleep (1000*1000*cancel_timeout);
32 g_print ("Cancelling\n");
33 g_cancellable_cancel (cancellable);
38 socket_address_to_string (GSocketAddress *address)
40 GInetAddress *inet_address;
44 inet_address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
45 str = g_inet_address_to_string (inet_address);
46 port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
47 res = g_strdup_printf ("%s:%d", str, port);
53 async_cb (GObject *source_object,
57 GAsyncResult **resp = user_data;
58 *resp = g_object_ref (res);
59 g_main_loop_quit (loop);
63 socket_client_event (GSocketClient *client,
64 GSocketClientEvent event,
65 GSocketConnectable *connectable,
66 GSocketConnection *connection)
68 static GEnumClass *event_class;
72 event_class = g_type_class_ref (G_TYPE_SOCKET_CLIENT_EVENT);
74 g_get_current_time (&tv);
75 printf ("% 12ld.%06ld GSocketClient => %s [%s]\n",
76 tv.tv_sec, tv.tv_usec,
77 g_enum_get_value (event_class, event)->value_nick,
78 connection ? G_OBJECT_TYPE_NAME (connection) : "");
82 main (int argc, char *argv[])
84 GOptionContext *context;
85 GSocketClient *client;
86 GSocketConnection *connection;
87 GSocketAddress *address;
88 GCancellable *cancellable;
95 context = g_option_context_new (" <hostname>[:port] - send data to tcp host");
96 g_option_context_add_main_entries (context, cmd_entries, NULL);
97 if (!g_option_context_parse (context, &argc, &argv, &error))
99 g_printerr ("%s: %s\n", argv[0], error->message);
105 g_printerr ("%s: %s\n", argv[0], "Need to specify hostname");
110 loop = g_main_loop_new (NULL, FALSE);
115 cancellable = g_cancellable_new ();
116 thread = g_thread_new ("cancel", cancel_thread, cancellable);
117 g_thread_unref (thread);
124 client = g_socket_client_new ();
126 g_socket_client_set_timeout (client, io_timeout);
128 g_signal_connect (client, "event", G_CALLBACK (socket_client_event), NULL);
133 g_socket_client_connect_to_host_async (client, argv[1], 7777,
134 cancellable, async_cb, &res);
135 g_main_loop_run (loop);
136 connection = g_socket_client_connect_to_host_finish (client, res, &error);
137 g_object_unref (res);
141 connection = g_socket_client_connect_to_host (client,
144 cancellable, &error);
146 if (connection == NULL)
148 g_printerr ("%s can't connect: %s\n", argv[0], error->message);
151 g_object_unref (client);
153 address = g_socket_connection_get_remote_address (connection, &error);
156 g_printerr ("Error getting remote address: %s\n",
160 g_print ("Connected to address: %s\n",
161 socket_address_to_string (address));
162 g_object_unref (address);
165 g_tcp_connection_set_graceful_disconnect (G_TCP_CONNECTION (connection), TRUE);
167 out = g_io_stream_get_output_stream (G_IO_STREAM (connection));
169 while (fgets(buffer, sizeof (buffer), stdin) != NULL)
171 /* FIXME if (async) */
172 if (!g_output_stream_write_all (out, buffer, strlen (buffer),
173 NULL, cancellable, &error))
175 g_warning ("send error: %s\n", error->message);
176 g_error_free (error);
181 g_print ("closing stream\n");
185 g_io_stream_close_async (G_IO_STREAM (connection),
186 0, cancellable, async_cb, &res);
187 g_main_loop_run (loop);
188 if (!g_io_stream_close_finish (G_IO_STREAM (connection),
191 g_object_unref (res);
192 g_warning ("close error: %s\n", error->message);
195 g_object_unref (res);
199 if (!g_io_stream_close (G_IO_STREAM (connection), cancellable, &error))
201 g_warning ("close error: %s\n", error->message);
206 g_object_unref (connection);