I have the following code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
time_t result;
char hostname[10] = "ws45new";
char graphite[10] = "127.0.0.1";
char buf[1024];
int n = 0;
int num = 0;
int graphiteport = 2003;
while (1 == 1)
{
result = time(NULL);
n = n + 1;
if (n >= 100)
n = 0;
printf("\n %d", n);
// snprintf(buf, sizeof(buf), "echo \"stats.ws45new.test %d %d \" | nc 127.0.0.1 2003", n, result);
num = sprintf(buf, "echo \"stats.ws45new1.test %d %d \" | nc 127.0.0.1 2003", n, result);
system(buf);
printf("\n %s", buf);
sleep(2);
}
return 0;
}
This is supposed to generate something like a sawtooth signal, which is only for testing. My problem is that when i run the program it doesn't do anything.
If i hit Ctrl-C to stop it, the program iterates once through the while loop. It is supposed to send some statistics to a graphite server and I'm using it to understand how graphite is working. From what i've seen it has something to do with the concatenation function sprintf
(snprintf
also acts the same way).
If I comment out that line it works correctly and generates the numbers I want (and also the epoch), but i need the concatenation to be able to send the required dynamic info to graphite.
If anyone has any idea why these concatenation functions don't respect the while function please let me know as I'm really curious. Also I'm open to other suggestions but I'd rather not go in to deep, like create sockets and not use nc and system, because i'm not that good with the C language.