Pi (C) write to file on remote machine

I wish to continually log data in a programme running on a Pi, this could be written to a file on the Pi. I want to be able to send this data to some file server in real time. I have in mind some script or preferable some C programme running on the Pi which periodically writes the data to a file on a remote ethernet network connected machine. So there should be no programme that I have written running on the remote machine. I am able to access the file system on the remote machine from the Pi. So the Pi should be able to create a file if it does not exist, and if it exists should be able to append data to the file in either text mode or binary mode. And of course should be able to read the contents of the remote file. I do not know how to do this, can I operate on a file with the remote machine IP address being part of the file path with a C language library call? I am confident that this should be easy to do - I just don’t know how to approach this. Could someone point me in the right direction please?

1 Like

Hi @Clem73188
What your looking for here is sockets. Gnu has the best explanation. :slight_smile:
I’m sure there are other ways, but this is tried and true.

1 Like

Hey @Clem73188,

Sockets, as suggested by @Pixmusix, would be a fantastic way to handle this. On the Pi you do have a few other options that would work slightly differently.

If you’re able to access the filesystem of the remote machine from the Pi, you can use standard C file operations, treating the remote filename as a pathname. This would require you to mount the remote file system on the Raspberry Pi.

NFS or Samba come to mind as good options for this task.

Sockets can be used to implement various higher-level network protocols, such as HTTP (web) or FTP (file transfer). However, they can be used directly for communicating raw data in situations where you have full control over both the sending and receiving application.

In your case, if mounting the file system is not an option or you want a more fine-grained control over your network interactions, creating a socket server/client in C language could be an option. You would create a socket that would listen for incoming connections on the Pi, and when data is ready to send, you would push that data over the network connection.

Hope this helps! :slight_smile:

1 Like