1 /* Echo client (UDP)
2 * Copyright (C) 2000 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
28 static void usage (int status);
29
30
31 int
main(int argc,char ** argv)32 main(int argc, char** argv)
33 {
34 gchar* hostname;
35 gint port;
36 GInetAddr* addr = NULL;
37 GUdpSocket* socket = NULL;
38 gint ttl;
39 gint rv;
40 gchar buffer[1024];
41 guint n;
42
43 gnet_init ();
44
45 if (argc != 3)
46 usage (EXIT_FAILURE);
47 hostname = argv[argc-2];
48 port = atoi(argv[argc-1]);
49
50 /* Create the address */
51 addr = gnet_inetaddr_new (hostname, port);
52 g_assert (addr != NULL);
53
54 /* Create the socket */
55 socket = gnet_udp_socket_new ();
56 g_assert (socket != NULL);
57
58 /* Get the TTL */
59 ttl = gnet_udp_socket_get_ttl (socket);
60 g_assert (ttl >= -1);
61
62 /* Set the TTL to 64 (the default on many systems) */
63 rv = gnet_udp_socket_set_ttl (socket, 64);
64 g_assert (rv == 0);
65
66 /* Make sure that worked */
67 ttl = gnet_udp_socket_get_ttl (socket);
68 g_assert (ttl == 64);
69
70 while (fgets(buffer, sizeof(buffer), stdin) != 0)
71 {
72 gint rv;
73
74 /* Send packet */
75 n = strlen(buffer);
76 rv = gnet_udp_socket_send (socket, buffer, n, addr);
77 g_assert (rv == 0);
78
79 /* Receive packet */
80 n = gnet_udp_socket_receive (socket, buffer, sizeof(buffer), NULL);
81 if (n == -1) break;
82
83 /* Write out */
84 fwrite (buffer, n, 1, stdout);
85 }
86
87 gnet_inetaddr_delete (addr);
88 gnet_udp_socket_delete (socket);
89
90 return 0;
91 }
92
93
94 static void
usage(int status)95 usage (int status)
96 {
97 g_print ("usage: echoclient-udp <server> <port>\n");
98 exit(status);
99 }
100