OPS102
John Sellens
john.sellens@senecapolytechnic.ca
Home UNX511 Help Notes Contact

Lab 8 - Week 10 - Client/Server with Messaging

Due: Sunday July 27

FIRST: See labs.html for information on labs.

You may wish to consult our class materials and sample code. For the code to go with this lab, see https://github.com/jsellens/unx511_samples/tree/main/lab_8

In this lab you will create a server (server.cpp) that communicates with three clients (client1.cpp, client2.cpp and client3.cpp) using one message queue.

The client code for all three clients has been given to you along with the Makefile, a start-up script, and a stop script in case you have to manually stop the server and all the clients. All these files are available in https://github.com/jsellens/unx511_samples/tree/main/lab_8 (You may wish to clone the repository.)

Your server and clients will be passing messages as defined in the structure Message as seen in client.h:


    const int BUF_LEN=64;
    
    // structure for message queue
    typedef struct mesg_buffer {
        long source;
        long dest;
        char buf[BUF_LEN];
    } MesgBuffer;
    
    typedef struct mymsg {
        long mtype; /* Message type */
        MesgBuffer msgBuf;
    } Message;
    

Your server will receive on message type (mtype) 4 but will transmit on one of three message types depending on the destination of the message. If the message is for client 1, then the server sets the message type to 1. If the message is for client 2, then the message type is set to 2. If the message is for client 3, then the message type is set to 3. This means client 1 filters all messages of message type 1, client 2 of message type 2, and client 3 of message type 3.

Your message buffer (msgBuf) will consist of a client source (source), client destination (dest), and a message (buf[]). The client source is the client that is sending the message. The client destination is the client which is receiving the message.

The server will act as a dispatcher of the messages. That is, all clients will send their messages to the server for distribution. When a server receives a message, it must eventually extract the destination of the message and send it to the destination client.

The key for communication between server and clients is as follows:
Server/Client1, Client2, Client3: key=ftok("serverclient", 65);

Study the client code to see how message reads and writes are performed. Reads are performed in a read thread (recv_func) and writes are performed from main(). Both reads and writes are performed within an infinite while-loop with is_running as the condition flag in both cases. Note that the messages are queued and mutex protected. Note also that your clients perform a controlled shutdown on ctrl-C.

For your server, the requirements are as follows:

In testing your work, it might help to isolate each client. For instance, if you want to see messages received by client3, use the following from the command line to start the clients:
$ ./startClient.sh | grep 'client 3:'

Questions:
Compare and contrast the mechanisms of inter-process communication that you have studied so far. Answer these questions in a comment block in your server.cpp file. For each question, you answer should be 3 or so sentences long.

  1. Between sockets, pipes, fifos, and messages, which is your favorite and why?
  2. Which is your least favorite and why?
  3. For the scenario presented in this lab, is there a need for a server? Why?

Lab Submission:



UNX511 NSB Class Links

References

Tools

Links





|Home|UNX511|Help Notes|Contact|


Last modified: 2025-07-16 21:58:02 -- Last formatted: 2025-08-05 00:44:22
john.sellens@senecapolytechnic.ca