简体   繁体   English

通过套接字传输压缩文件?

[英]Transfer compressed files over a socket?

I have 2 computers. 我有两台电脑。 One (computer 2) is getting files (.json) from 2 different processes, and then it passes this files (through ethernet cable) to the other computer (computer 1) (like in the attached image). 一台(计算机2)正在从2个不同的进程中获取文件(.json),然后(通过以太网电缆)将此文件传递到另一台计算机(计算机1)(如随附的图像)。 This happens constantly not just one's. 这种情况不断发生,而不仅仅是一个人。

To do this file transfers, my idea is the following: 为此,我的想法是:

  • Create a server/client sockets in c to comunicate both computers. 在c中创建服务器/客户端套接字,以通信两台计算机。
  • Tar the files (an amount say 4 files) in computer 2. 将计算机2中的文件(总共说4个文件)压缩。
  • Recive the files in computer 1. 接收计算机1中的文件。

One way at first I wanted to do this, was with Netcat and tar, in bash. 一开始我想做的一种方式是使用bash中的Netcat和tar。 But the I read that it was not a good Ideas, because bash doesn't work well with file transfers. 但是我读到它不是一个好主意,因为bash在文件传输中不能很好地工作。 So I decided to do it in C (it has to be C, or C++, not python, but I am better at C so C is the option). 因此,我决定使用C语言(它必须是C或C ++,而不是python,但我的C语言更好,因此可以选择C)。 So now I am doing it with this sample code: 所以现在我用下面的示例代码来做到这一点:

Send and Receive a file in socket programming in Linux with C/C++ (GCC/G++) 使用C / C ++(GCC / G ++)在Linux中的套接字编程中发送和接收文件

but I cant figure out the part of tar and sending Tar, and if this code would help for this. 但我无法弄清楚tar的一部分并发送Tar,如果该代码对此有所帮助。

Other way I wa thinking to do it was with zeromq, but I havent use this before, so I dont know if it is worth the extra studding. 我想做的另一种方式是用zeromq,但是我以前没有用过,所以我不知道是否值得进行额外的学习。

Thanks a lot in advance for your answers. 预先非常感谢您的回答。

image 图片

The .tar file format is pretty straightforward (and documentation on how to read/write a .tar file yourself can be found via a Google search on ".tar file format" or similar), or you could just use the existing libtar reader/writer library if you'd prefer not to reinvent the wheel. .tar文件格式是非常简单的(和文件如何读/写.tar文件可以自己通过的“.tar文件格式”或类似谷歌搜索可以找到),或者你可以只使用现有的libtar读/作家库,如果您不想重新发明轮子。

Another alternative (assuming you just need a quick program to handle a personal use-case and don't need a production-grade solution) would just be to call system () (or similar) to have it execute the appropriate shell commands on behalf of your C program -- but if that's good enough, one has to wonder why a simple bash script isn't also acceptable. 另一个替代方法(假设您只需要一个快速程序来处理个人用例,而无需生产级解决方案)就可以调用system ()(或类似方法)以使其代表相应的shell命令执行。的C程序-但是,如果这足够好,人们不得不怀疑为什么简单的bash脚本也不能被接受。 Bash may not be good at file transfers natively, but the various command-line utilities you can call from bash (scp, rsync, tar, untar, etc) certainly work fine. Bash可能并不擅长本机文件传输,但是您可以从bash调用的各种命令行实用程序(scp,rsync,tar,untar等)肯定能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM