0

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). 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.
  • Tar the files (an amount say 4 files) in computer 2.
  • Recive the files in computer 1.

One way at first I wanted to do this, was with Netcat and tar, in bash. But the I read that it was not a good Ideas, because bash doesn't work well with file transfers. 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). 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++)

but I cant figure out the part of tar and sending Tar, and if this code would help for this.

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.

Thanks a lot in advance for your answers.

image

jww
  • 97,681
  • 90
  • 411
  • 885
Cactus
  • 19
  • 5
  • 6
    I would have thought `scp` or `rsync` would be the way to transfer files between machines - or use NFS to share a directory between the two and there's no need to transfer anything since both machines will be able to see any files either machine places in it. – Mark Setchell Feb 05 '18 at 21:07
  • I don't think you question is clear. Are you having problem make a tar and sending the .tar file? – Daniel Santos Feb 05 '18 at 21:13
  • 3
    Use a [Google Protocol Buffer](https://developers.google.com/protocol-buffers/) to send and receive messages. It will handle framing the message for you. You should also show the relevant code. Also see [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – jww Feb 05 '18 at 21:25
  • Here are examples of using a protocol buff to send an array with embedded `\0`. It should be similar to your tar example. [Google protocol buffers and use of std::string for arbitrary binary data](https://stackoverflow.com/q/9373325/608639) and [why protocol buffer bytes is string in c++?](https://stackoverflow.com/q/11467567/608639). – jww Feb 06 '18 at 03:45
  • Hello. Thanks for your answers. I had thought of something like scp, but the thing is: wouldnt it be too slow? and a question also. In computer 2, there are going to be 3 netwotk adapters (one internal Ethernet adapter and 2 usb/Ethernet connectors) each with different ip addres. Computer 2 then would work as a server (for 2 inputs), and as a client (sending info to computer 1). is there a problem if the client is using scp command (as sugested by Jeremy Friesner) from a bash script, and the other conections using other type of communication (Ethercat). Thanks a lot again for the answers. – Cactus Feb 06 '18 at 23:32
  • Also, im looking at google protocols, didnt knew about them. Thanks – Cactus Feb 06 '18 at 23:33

1 Answers1

1

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.

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. 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.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234