【问题标题】:Write string to remote file using libssh2使用 libssh2 将字符串写入远程文件
【发布时间】:2017-07-06 22:24:54
【问题描述】:

我正在尝试使用 libssh2 将字符串写入文件。我可以做到。

问题是我只能向文件写入一次,我不能追加到文件中,并将参数提供给libssh2_scp_send 我给出大小,如果大小大于字符串的实际大小(例如,我给 50,但字符串有 5 个字符)创建但有 0 个字节。

我使用的功能:

int s7c_hardware::writeKey(char * filename, char * str)
{
    channel = libssh2_scp_send(session, filename, 0777, strlen(str));
    char buf[99999];
    memcpy(buf, str, strlen(str));
    buf[strlen(str)] = '\0';
    libssh2_channel_write(channel, buf, strlen(str));
    libssh2_channel_free(channel);
    channel = NULL;
    return 0;
}

【问题讨论】:

  • 为什么是memcpy()?效果不正是strcpy() 所做的吗?什么时候关闭频道?因为channel 似乎是一个全局变量......而且我没有看到对libssh2_channel_close 的调用。通道写入是否返回错误(您忽略它)。
  • @dhke 对不起,我忘记输入关闭频道的代码了。为什么是内存?因为在我的应用程序中,该字符串的中间可以有“\0”零终止符。
  • 如果可以,您应该使用 SFTP 协议而不是 SCP。 SCP 不是很复杂。
  • @Kenster 但是 SCP 和 SFTP 的区别在于:SCP 速度更快,不使用 ACK 标志,但使用 SFTP 您可以开始和停止下载我们的上传
  • @Kenster 但是好的,我会尝试使用 SFTP

标签: c++ string ssh scp libssh2


【解决方案1】:

现在我明白为什么了! SCP 不能附加到文件,因为这实际上是开始和停止下载,SCP 不能这样做,但 SFTP 可以!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多