如何将二进制文件或任何文件传输到远程服务器? - 红宝石

发布于 2024-08-01 21:18:20 字数 161 浏览 1 评论 0原文

情况是我正在尝试编写一个接受来自客户端的文件传输的服务器脚本。 我弄清楚了如何使用 TCP 协议与服务器进行连接。 但是我想知道如何在 ruby​​ 中传输二进制文件?

我的意思是您可以打开二进制文件,但是需要执行哪些步骤才能传输它? TCP是流的想法吗? 那么UDP呢?

The situation is that I'm trying to write a server script that accepts file transfers from a client. I figured out how to make and connect to and from the server using the TCP protocol. However I was wondering how do you transfer a binary file in ruby?

I mean you can open a binary file, but what steps are necessary to be able to transfer it? Is TCP stream idea? What about UDP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花落人离去 2024-08-08 21:18:20

好吧,如果您编写自己的客户端和服务器,那么总结起来就很简单了,

socket.write(data)

因为套接字只是一个 IO 对象。 然后终止你的输入(当然,你需要发明你自己的转义,或者预先发送长度 - 从头开始​​发明一个协议)。

Well, if you wrote your own client and your own server it prety much sums up to

socket.write(data)

since a socket is just an IO object. And then terminating your input (of course you will need to invent your own escaping, or send length beforehand - invent a protocol from scratch).

你是我的挚爱i 2024-08-08 21:18:20

就套接字而言,“二进制”数据和非二进制数据之间没有什么真正区别,因为它们都是一样的。 它们只是数据流,两端的应用程序有责任正确格式化和解释通信。

如果数据没有某种框架,就很难确定传输的数据是否完整且有效。 HTTP 规范就是实现这一点的一个示例,尽管存在许多其他规范,其中一些非常简单,例如 FTP。

理想情况下,您可以使用现有的规范,而不必自行推出规范,并且有一些规范(例如 BEEP)可以作为有用的、健壮的、通用的示例。

As far as sockets go, there's nothing to really distinguish between "binary" data and non-binary data, as it's all the same. They are just streams of data and it is the responsibility of the applications on either end to correctly format and interpret the communications.

Without some kind of framing on your data it will be hard to determine if the transmitted data is complete and valid. An example of how this is done is the HTTP specification, although many others exist, some of which are quite simple such as FTP.

Ideally you can make use of an existing specification without having to resort to rolling your own, and there are specifications such as BEEP which can serve as useful, robust, generic examples.

泪是无色的血 2024-08-08 21:18:20

我想我找到了解决方案。

使用 SFTP,我可以通过 SSH 连接将文件上传到服务器:

require 'net/sftp'

Net::SFTP.start('host', 'username', :password => 'password') do |sftp|
  # upload a file or directory to the remote host
  sftp.upload!("/path/to/local", "/path/to/remote")

  # download a file or directory from the remote host
  sftp.download!("/path/to/remote", "/path/to/local")
end

但这并不是我真正想要的,因为上面的内容依赖于使用 SSH。 我本来希望它能够独立。

I think I found a solution.

Using SFTP, I can upload files over an SSH connection to the server:

require 'net/sftp'

Net::SFTP.start('host', 'username', :password => 'password') do |sftp|
  # upload a file or directory to the remote host
  sftp.upload!("/path/to/local", "/path/to/remote")

  # download a file or directory from the remote host
  sftp.download!("/path/to/remote", "/path/to/local")
end

However this isn't really what I was looking for as the above relies on using SSH. I was hoping it to be independent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文