Task: Unix Socket
Navigate to labs/lab-10/tasks/unix-socket and run make to generate the support directory. In this exercise, you’ll implement client-server communication between two processes using a UNIX socket. Both the sender and receiver are created from the same binary: run without arguments for a receiver, or with -s for a sender.
-
Complete the TODOs in the
sender_loop(). You need to verify whether the socket exists i.e. check if the receiver has created it. Next, create your own socket and connect to the receiver’s socket using its address (Hint: useget_sockaddr(<path>to obtain it). Once the connection is established, you can send messages usingsend(). -
Complete the TODOs in the
receiver_loop(). Similarly, you will need to create a socket and bind it to the receiver’s address (Hint: useget_sockaddr(<path>for this). Instead of connecting, you will listen for and accept incoming connections. Whenaccept()receives a connection request, it will return a new socket file descriptor that you can use to receive messages viarecv(). -
Inside the
tests/directory, you will need to runchecker.sh. The output for a successful implementation should look like this:
./checker.sh
Test for socket creation: PASSED
Test for send and receive: PASSED
If you’re having difficulties solving this exercise, go through this reading material.