【问题标题】:Data Missing when using Netcat in C在 C 中使用 Netcat 时数据丢失
【发布时间】:2018-10-15 07:34:26
【问题描述】:

我有一个用于嵌入式协议之一的 C 软件。在此,我需要将回调函数中的一些事件重定向到 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

在代码中,如果我将最后一个系统函数调用替换为 be

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

【问题讨论】:

  • 你也有服务器的代码吗?
  • 为什么要使用netcat?您可以在 C 中轻松创建 TCP 套接字。使用 system 完成所有工作是自找麻烦(或命令注入)。
  • 除了丑陋的机制之外,我没有看到任何原因,为什么它会按照您描述的方式失败。可能错误位于服务器中。

标签: c string netcat


【解决方案1】:

systemcomm 未初始化,因此strcat(systemcomm,tcp_buf); 是未定义的行为。一切皆有可能。我们甚至可能无法推测出什么样的初始垃圾会导致这种症状。

如果你坚持system,就做一个

    sprintf(systemcomm, "echo '....' | netcat localhost 9091, ....);

【讨论】:

  • snprintf 会更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-19
  • 2011-12-19
  • 2018-08-19
  • 1970-01-01
相关资源
最近更新 更多