What I want to do is the following: I want to execute a ping command, but I don't want my program to be blocked while waiting for the response, so I want to execute it asynchronously and after a timeout check if the answer has arrived. This is for a program which should run on linux (debian).
I thought of using the g_spawn_async_with_pipes command for glib, but that doesn't seem to work. Here's a simplified version of part my code:
Code:
void ping(char *IpAddress)
{
char command[100];
gint Child_Process;
gint Child_Out, Child_Err;
GError *Child_Error = NULL;
GIOChannel* iochannel;
strcpy(command, "ping -c 1 "); // one packet at a time
strcat(command, IpAddress);
g_spawn_async_with_pipes(NULL, (gchar **)&command, NULL, (GSpawnFlags) (G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD),
NULL, NULL, &Child_Process, NULL, &Child_Out, &Child_Err, &Child_Error);
iochannel = g_io_channel_unix_new(Child_Out);
g_io_channel_set_close_on_unref (iochannel, TRUE);
g_io_add_watch (iochannel, (GIOCondition)(G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP|G_IO_NVAL), iofunc_stdout, NULL);
}
So I call the ping-function, but what I see is that the response-function (iofunc_stdout) is never called. Anybody an idea what I'm doing wrong. Or has an example on how to do this (I already searched the web for that)? Or anybody has a better way to do this?