1 /* Echo server (TCP blocking)
2 * Copyright (C) 2000-2003 David Helder
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
18 */
19
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <gnet.h>
26
27 #include <signal.h>
28
29 static void ob_server_func (GServer* server, GConn* conn, gpointer user_data);
30 static void ob_client_func (GConn* conn, GConnEvent* event,
31 gpointer user_data);
32 static void ob_sig_int (int signum);
33
34 static GServer* ob_server = NULL;
35
36
37 int
main(int argc,char ** argv)38 main(int argc, char** argv)
39 {
40 int port;
41 GServer* server;
42 GMainLoop* main_loop;
43
44 gnet_init ();
45
46 if (argc != 2)
47 {
48 fprintf (stderr, "usage: echoserver <port> \n");
49 exit(EXIT_FAILURE);
50 }
51
52 port = atoi(argv[argc - 1]);
53
54 /* Create the main loop */
55 main_loop = g_main_new (FALSE);
56
57 /* Create the server */
58 server = gnet_server_new (NULL, port, ob_server_func, NULL);
59 if (!server)
60 {
61 fprintf (stderr, "Error: Could not start server\n");
62 exit (EXIT_FAILURE);
63 }
64
65 ob_server = server;
66 signal (SIGINT, ob_sig_int);
67
68 /* Start the main loop */
69 g_main_run(main_loop);
70
71 exit (EXIT_SUCCESS);
72 return 0;
73 }
74
75
76 static void
ob_server_func(GServer * server,GConn * conn,gpointer user_data)77 ob_server_func (GServer* server, GConn* conn, gpointer user_data)
78 {
79 if (conn)
80 {
81 gnet_conn_set_callback (conn, ob_client_func, NULL);
82 gnet_conn_set_watch_error (conn, TRUE);
83 gnet_conn_readline (conn);
84 }
85 else /* Error */
86 {
87 gnet_server_delete (server);
88 exit (EXIT_FAILURE);
89 }
90 }
91
92
93 static void
ob_client_func(GConn * conn,GConnEvent * event,gpointer user_data)94 ob_client_func (GConn* conn, GConnEvent* event, gpointer user_data)
95 {
96 switch (event->type)
97 {
98 case GNET_CONN_READ:
99 {
100 event->buffer[event->length-1] = '\n';
101 gnet_conn_write (conn, event->buffer, event->length);
102
103 /* fwrite (event->buffer, event->length, 1, stdout); */
104
105 gnet_conn_readline (conn);
106 break;
107 }
108
109 case GNET_CONN_WRITE:
110 {
111 ; /* Do nothing */
112 break;
113 }
114
115 case GNET_CONN_CLOSE:
116 case GNET_CONN_TIMEOUT:
117 case GNET_CONN_ERROR:
118 {
119 gnet_conn_delete (conn);
120 break;
121 }
122
123 default:
124 g_assert_not_reached ();
125 }
126 }
127
128
129 static void
ob_sig_int(int signum)130 ob_sig_int (int signum)
131 {
132 gnet_server_delete (ob_server);
133 exit (EXIT_FAILURE);
134 }
135