[gnet] Re: Error, glib2.0, gnet_tcp_socket_connect_async



David Helder pise:
> 
> Marek,
> 
> Can you show us a simple example?  I'm not quite sure what you're trying
> to do.
:-)
Ok, see example.
--mara
/*
gcc -o tst  -L/usr/local/lib -lgthread-2.0 -lpthread -lglib-2.0 -lgnet   -lstdc++ -g -Wall -DGNET_EXPERIMENTAL -D_REENTRANT  -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/local/include/gnet -I/usr/local/lib/gnet/include   tst.cc -lgnet
 */




#ifndef XX__GNET_H__XX
#define XX__GNET_H__XX
#include <gnet.h>
#endif
#ifndef XX__STDIO_H__XX
#define XX__STDIO_H__XX
#include <stdio.h>
#endif
char *squid_host = "ns.agrie.cz";
int   squid_port = 3128;
int   s_port     = 9090;


void as_call_connect (GTcpSocket* socket, GInetAddr* ia,
		      GTcpSocketConnectAsyncStatus status,
		      gpointer data);
void as_call_connect (GTcpSocket* socket, GInetAddr* ia,
		      GTcpSocketConnectAsyncStatus status,
		      gpointer data)
{
	if (status != GTCP_SOCKET_CONNECT_ASYNC_STATUS_OK)
	{
		fprintf(stderr, "Could not connect (status = %d)",
			status);
		return;
	}
	fprintf(stderr, "Ok, connected to SQuid");
}

static void
async_accept (GTcpSocket* server, GTcpSocket* client, gpointer data)
{
	if (!client)
	{
		fprintf (stderr, "Server error\n");
		exit (-1);
	}
	gchar*    name;
	gint	  port;
	
	/* Print the address */
	GInetAddr*  addr = gnet_tcp_socket_get_inetaddr(client);
	g_assert (addr);
	name = gnet_inetaddr_get_canonical_name (addr);
	g_assert (name);
	port = gnet_inetaddr_get_port (addr);
	gnet_inetaddr_delete (addr);
	
	gnet_tcp_socket_connect_async (squid_host, squid_port,
				       as_call_connect, NULL);
}
static GTcpSocket* async_server = NULL;
static void 
async_sig_int (int signum)
{
  gnet_tcp_socket_delete (async_server);
  exit (-1);
}
int main(int argc, char *argv)
{
	GTcpSocket* server;
	GInetAddr*  addr;
	gchar*      name;
	gint	      port;
	GMainLoop*  main_loop = NULL;

	//signal_set_handlers();
	
	/* Create the server */
	server = gnet_tcp_socket_server_new(s_port);
	if (server == NULL)
	{
		fprintf (stderr, "Could not create server on port %d\n",
			 s_port);
		exit (-1);
	}

	async_server = server;

	
	/* Print the address */
	addr = gnet_tcp_socket_get_inetaddr(server);
	g_assert (addr);
	name = gnet_inetaddr_get_canonical_name (addr);
	g_assert (name);
	port = gnet_inetaddr_get_port (addr);
	g_print ("Server running on %s:%d\n", name, port);
	gnet_inetaddr_delete (addr);
	g_free (name);

	/* Create the main loop */
 	main_loop = g_main_new(FALSE);
	
	/* Wait asyncy for incoming clients */
	gnet_tcp_socket_server_accept_async (server, async_accept, NULL);
	
	/* Start the main loop */
	g_main_run(main_loop);
	return 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]