/* Echo server (TCP blocking) * Copyright (C) 2000-2003 David Helder * * 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, GNet; namespace GNetExamples { int main(string[] args) { int port; TcpSocket server; InetAddr addr; string name; TcpSocket client = null; String buffer = String.sized(1024); uint n; IOChannel ioclient = null; IOError error; GNet.init (); if (args.length != 2) { stderr.printf ("usage: echoserver \n"); return 1; } port = args[1].to_int(); /* Create the server */ server = TcpSocket.server_new_with_port (port); if (server == null) { stderr.printf("Could not create server on port %d\n", port); return 1; } /* Print the address */ addr = server.get_local_inetaddr(); name = addr.get_canonical_name (); port = addr.get_port (); stdout.printf ("Normal echoserver running on %s:%d\n", name, port); client = server.server_accept (); while (client != null) { /* Get IOChannel */ ioclient = client.get_io_channel(); /* Print the address */ addr = client.get_remote_inetaddr(); name = addr.get_canonical_name (); port = addr.get_port (); stdout.printf ("Accepted connection from %s:%d\n", name, port); while ((error = IO.channel_readline(ioclient, buffer.str, 1024, out n)) == IOError.NONE && (n > 0)) { error = IO.channel_writen(ioclient, buffer.str, buffer.str.len(), out n); if (error != IOError.NONE) break; stdout.printf("%s", buffer.str); } if (error != IOError.NONE) { stderr.printf("exit with errorcode\n"); } stdout.printf ("Connection from %s:%d closed\n", name, port); client = server.server_accept (); } return 0; } /* static void normal_sig_int (int signum) { gnet_tcp_socket_delete (normal_server); exit (EXIT_FAILURE); } */ }