1 /* Echo client
2 * Copyright (C) 2000-2001 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 int
main(int argc,char ** argv)29 main(int argc, char** argv)
30 {
31 gchar* hostname;
32 gint port;
33 GInetAddr* addr;
34 GTcpSocket* socket;
35 GIOChannel* iochannel;
36 GIOError error = G_IO_ERROR_NONE;
37 gchar buffer[1024];
38 gsize n;
39
40 gnet_init ();
41
42 /* Parse args */
43 if (argc != 3)
44 {
45 g_print ("usage: %s <server> <port>\n", argv[0]);
46 exit(EXIT_FAILURE);
47 }
48 hostname = argv[1];
49 port = atoi(argv[2]);
50
51 /* Create the address */
52 addr = gnet_inetaddr_new (hostname, port);
53 if (!addr)
54 {
55 fprintf (stderr, "Error: Name lookup for %s failed\n", hostname);
56 exit (EXIT_FAILURE);
57 }
58
59 /* Create the socket */
60 socket = gnet_tcp_socket_new (addr);
61 gnet_inetaddr_delete (addr);
62 if (!socket)
63 {
64 fprintf (stderr, "Error: Could not connect to %s:%d\n", hostname, port);
65 exit (EXIT_FAILURE);
66 }
67
68 #if 0
69 {
70 gchar* cname;
71
72 /* Print local address */
73 addr = gnet_tcp_socket_get_local_inetaddr (socket);
74 g_assert (addr);
75 cname = gnet_inetaddr_get_canonical_name (addr);
76 g_assert (cname);
77 g_print ("Local address: %s:%d\n", cname, gnet_inetaddr_get_port(addr));
78 g_free (cname);
79 gnet_inetaddr_delete (addr);
80
81 /* Print remote address */
82 addr = gnet_tcp_socket_get_remote_inetaddr (socket);
83 g_assert (addr);
84 cname = gnet_inetaddr_get_canonical_name (addr);
85 g_assert (cname);
86 g_print ("Remote address: %s:%d\n", cname, gnet_inetaddr_get_port(addr));
87 g_free (cname);
88 gnet_inetaddr_delete (addr);
89 }
90 #endif
91
92 /* Get the IOChannel */
93 iochannel = gnet_tcp_socket_get_io_channel (socket);
94 g_assert (iochannel != NULL);
95
96 while (fgets(buffer, sizeof(buffer), stdin) != 0)
97 {
98 n = strlen(buffer);
99 error = gnet_io_channel_writen (iochannel, buffer, n, &n);
100 if (error != G_IO_ERROR_NONE) break;
101
102 error = gnet_io_channel_readn (iochannel, buffer, n, &n);
103 if (error != G_IO_ERROR_NONE) break;
104
105 fwrite(buffer, n, 1, stdout);
106 }
107
108 if (error != G_IO_ERROR_NONE)
109 fprintf (stderr, "Error: IO error (%d)\n", error);
110
111 gnet_tcp_socket_delete (socket);
112
113 return 0;
114 }
115
116