create new tag
view all tags

Exercises 3: Files, Pipes and Sockets


When talking to the sensors during the next exercises we will use a Raspberry Pi to do the readout. Two different libraries are available to easily interface to the sensors:

It is a matter of taste, which of the two you prefer to use. Personally I prefer pigpio, which comes in 2 versions: The first one makes direct access to the Linux kernel drivers while the second one provides the pigpio daemon to which your application connects through either a pipe (if the application runs on the Raspberry Pi) or via sockets, if the application runs e.g. on your PC. Like this you get remote access to the sensors, which are connected and controlled by the Pi. In this exercise we will demonstrate how communication works either via pipes between two processes on the same machine or via sockets where you communicate through the network and the two processes may run on two different machines. (if your “remote host” is localhost then both processes will run on the same machine!).

Exercise 1: Reading and writing files

In analogy to the way we read and write pipes we will first try to write some text to a file and read it back with another program.
Write a C program called fileWrite.c which writes the text:

hello #1

hello #2

hello #3

to a text file. This file should be readable to anybody but writable only to its owner and members of the group owning the file. Name the file “text.txt”. Use the system calls

  • open

  • write/read

  • close

to write and read the file. Have a look at the corresponding man pages first (e..g. man 2 open).

Check that the file has been created and use cat/more/less or an editor to look at its content.

Then write a new program fileRead.c to read back its content into a buffer and print out the result with printf.

As an additional exercise you may try to do the same thing with fopen, fwrire, fread, fclose. What is the difference?

Exercise 2: Pipes

This exercises demonstrates the communication between two processes via pipes:

Write a program that creates a pipe using the pipe system call. Have a look at the man page for pipe to get more information about this call. Then your program will spawn a second process (use the system call fork), close the unused pipe ends and send some message from the child process to the parent process who prints the message. Compare writing and reading of pipes to writing and reading files.

Exercise 3: Sockets

Now we will communicate between 2 machines: You will have to write 2 programs:

  • a tcp server

  • a tcp client

for this communication to work. In case of our pigpio library the server is the pigpio daemon, which must be started for the library calls to work correctly.

The server opens a socket with the socket system call. Have a look at the sockaddr_in structure which must be filled before you can bind to the socket. Use port no 5001 for your server.

Once bind was successful (test it!) listen for connect requests on this socket and accept them. Then read messages coming from the client and answer them (e.g. saying:”I saw your message ...”);

These are is the sequence of actions to make the server work:

  • create a socket (socket system call)

  • clear and then fill the sockaddr_in structure

    • serv_addr.sin_family = AF_INET;

    • serv_addr.sin_addr.s_addr = INADDR_ANY;

    • serv_addr.sin_port = htons(portno);

  • bind to the socket

  • listen to the socket

  • if there is a connection request accept it

  • read/write from the socket

  • close socket

The client must

  • create a socket with the socket system call

  • fill the sockaddr_in data structure

  • connect to the server socket

  • read/write from/to the socket

  • close it

-- Uli Raich - 2017-08-12


Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2017-08-13 - uli
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback