/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- */ /* Lac - Library for asynchronous communication * Copyright (C) 2002 Søren Sandmann (sandmann@daimi.au.dk) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #include #include GMainLoop *main_loop; int n_connections = 0; static void conn_callback (LacConnection *connection, const LacConnectionEvent *event) { gchar *s; g_print ("connection to %s:80: ", (gchar *)lac_connection_get_data (connection)); switch (event->type) { case LAC_CONNECTION_EVENT_CONNECT: g_print ("CONNECT\n"); lac_connection_write_printf ( connection, "GET / HTTP/1.1\r\nHost: %s\r\n\r\n", lac_connection_get_data (connection)); break; case LAC_CONNECTION_EVENT_READ: g_print ("READ (%d bytes)\n", event->read.len); s = g_new (gchar, event->read.len + 1); strncpy (s, event->read.data, event->read.len); s[event->read.len] = '\0'; g_print ("%s\n", s); g_free (s); break; case LAC_CONNECTION_EVENT_CLOSE: g_print ("CLOSE (%s)\n", event->close.remote_closed? "remote" : "local"); lac_connection_unref (connection); if (--n_connections == 0) g_main_loop_quit (main_loop); break; case LAC_CONNECTION_EVENT_ERROR: g_print ("ERROR (%s)\n", event->error.err->message); lac_connection_unref (connection); if (--n_connections == 0) g_main_loop_quit (main_loop); break; default: g_print ("event->type == %d?\n", event->type); g_assert_not_reached (); break; } } static void callback (const LacAddress *addr, gpointer data, const GError *err) { if (err) { g_print (" (%p) ", err->message); g_print ("%s\n", err->message); } else { LacConnection *connection; connection = lac_connection_new_tcp (addr, 80, conn_callback, data); } } int main (int argc, char *argv[]) { int i; #if 0 lac_set_verbose (TRUE); #endif if (argc > 1) { for (i = 1; i < argc; ++i) { ++n_connections; lac_address_new_lookup_from_name (argv[i], callback, argv[i]); } } else { g_print ("usage %s \n", argv[0]); return 1; } main_loop = g_main_loop_new (NULL, TRUE); g_assert (main_loop); g_main_loop_run (main_loop); return 0; }