У меня программное обеспечение на Си для одного из встроенных протоколов.В этом мне нужно перенаправить некоторые события, поступающие в функции обратного вызова, на сервер TCP.Итак, я подумал об использовании Netcat и системных команд.Всякий раз, когда срабатывает функция обратного вызова, если я сохраняю строку и пересылаю ее в netcat, то на TCP-сервере я получаю данные.Но если я формирую команду, используя параметры в функции обратного вызова, только в первый раз, когда я получаю данные после этого на TCP-сервере, я могу видеть успешное соединение и только отключать сообщения.
Мой код C -
static void zwp_avi_interfaces_alarm_report_handler(zwifd_p ifd, zwalrm_p alarm_info, time_t ts)
{
zwp_avi_interfaces_alarm_state_t *state;
zwifd_p desc_interface;
char systemcomm[1500];
char tcp_buf[100];
sprintf(tcp_buf,"echo 'node id: %d alarm type: %d alarm_event: %d", ifd->nodeid, alarm_info->ex_event, alarm_info->ex_has_sequence);
strcat(systemcomm,tcp_buf);
strcat(systemcomm," ' | netcat localhost 9091");
system(systemcomm);
}
И вывод на TCP-сервере -
A new connection has been established.
Data received from client: node id: 7 alarm type: 22 alarm_event: 0
Closing connection with the client
A new connection has been established.
Closing connection with the client
A new connection has been established.
Closing connection with the client
A new connection has been established.
Closing connection with the client
A new connection has been established.
Closing connection with the client
A new connection has been established.
Closing connection with the client
A new connection has been established.
Closing connection with the client
A new connection has been established.
Closing connection with the client
A new connection has been established.
Closing connection with the client
В коде, если я заменю последний системный вызов функции на
system("echo 'alarm_event' | netcat localhost 9091");
Тогда я получаю, сколько раз этот обратный вызов срабатывает столько раз.Вывод на TCP-сервер, как показано ниже
A new connection has been established.
Data received from client: alarm_event
Closing connection with the client
A new connection has been established.
Data received from client: alarm_event
Closing connection with the client
A new connection has been established.
Data received from client: alarm_event
Closing connection with the client
A new connection has been established.
Data received from client: alarm_event
Closing connection with the client
A new connection has been established.
Data received from client: alarm_event
Closing connection with the client
A new connection has been established.
Data received from client: alarm_event
Closing connection with the client
A new connection has been established.
Data received from client: alarm_event
Closing connection with the client
A new connection has been established.
Data received from client: alarm_event