Add some more cases to the app-id unit tests
[glib.git] / gio / tests / echo-server.c
blob7634b5bb0201d86d8f69992e387ed3840fa4a885
1 #include <gio/gio.h>
2 #include <string.h>
4 #define MESSAGE "Welcome to the echo service!\n"
6 int port = 7777;
7 static GOptionEntry cmd_entries[] = {
8 {"port", 'p', 0, G_OPTION_ARG_INT, &port,
9 "Local port to bind to", NULL},
10 {NULL}
14 static gboolean
15 handler (GThreadedSocketService *service,
16 GSocketConnection *connection,
17 GSocketListener *listener,
18 gpointer user_data)
20 GOutputStream *out;
21 GInputStream *in;
22 char buffer[1024];
23 gssize size;
25 out = g_io_stream_get_output_stream (G_IO_STREAM (connection));
26 in = g_io_stream_get_input_stream (G_IO_STREAM (connection));
28 g_output_stream_write_all (out, MESSAGE, strlen (MESSAGE),
29 NULL, NULL, NULL);
31 while (0 < (size = g_input_stream_read (in, buffer,
32 sizeof buffer, NULL, NULL)))
33 g_output_stream_write (out, buffer, size, NULL, NULL);
35 return TRUE;
38 int
39 main (int argc, char *argv[])
41 GSocketService *service;
42 GOptionContext *context;
43 GError *error = NULL;
45 context = g_option_context_new (" - Test GSocket server stuff");
46 g_option_context_add_main_entries (context, cmd_entries, NULL);
47 if (!g_option_context_parse (context, &argc, &argv, &error))
49 g_printerr ("%s: %s\n", argv[0], error->message);
50 return 1;
53 service = g_threaded_socket_service_new (10);
55 if (!g_socket_listener_add_inet_port (G_SOCKET_LISTENER (service),
56 port,
57 NULL,
58 &error))
60 g_printerr ("%s: %s\n", argv[0], error->message);
61 return 1;
64 g_print ("Echo service listening on port %d\n", port);
66 g_signal_connect (service, "run", G_CALLBACK (handler), NULL);
68 g_main_loop_run (g_main_loop_new (NULL, FALSE));
69 g_assert_not_reached ();