/* Echo client * Copyright (C) 2007 Andrea Del Signore * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using GLib; using GNet; namespace GNetExamples { int main(string[] args) { string hostname; int port; InetAddr addr; TcpSocket socket; IOChannel iochannel; IOError error = IOError.NONE; String buffer = new String.sized(1024); ulong n; GNet.init(); /* Parse args */ if (args.length != 3) { stdout.printf ("usage: %s \n", args[0]); return 1; } hostname = args[1]; port = args[2].to_int(); /* Create the address */ addr = new InetAddr (hostname, port); if (addr == null) { stderr.printf ("Error: Name lookup for %s failed\n", hostname); return 1; } /* Create the socket */ socket = new TcpSocket.from_inetaddr (addr); if (socket == null) { stderr.printf ("Error: Could not connect to %s:%d\n", hostname, port); return 1; } /* Get the IOChannel */ iochannel = socket.get_io_channel (); while (stdin.gets(buffer.str, (int) (1024)) != null) { n = buffer.str.len(); error = IO.channel_writen (iochannel, buffer.str, n, out n); if (error != IOError.NONE) break; error = IO.channel_readn (iochannel, buffer.str, n, out n); if (error != IOError.NONE) break; stdout.printf("%s",buffer.str); } if (error != IOError.NONE) stderr.printf ("Error: IO error (%d)\n", error); return 0; } }