Discussion questions: remote procedure calls

Please work out these problems before your next discussion section. The GSIs and IAs will go over these problems during the discussion section.

1. Remote procedure calls (RPC)

A client wants to send a linked list to a server over the network so that the server can access the elements of the list locally. A linked list structure contains the following:

struct Node {
    char *nodedata;	// null-terminated string
    struct Node *next;	// pointer to the next node in the data
};

struct Node *head;	// pointer to the first node in the list
int listLength;         // This variable always contains the length 
                        // of the above linked list
int strlen(char *s);    // returns the length of a null-terminated
                        // string, EXCLUDING the null terminator.

a. Marshalling data

Show how the client can marshal the linked list into a message and send it to the server. Indicate all sends to the communication channel, including spaces, null characters, etc., so that the receiver has sufficient information to parse the message and recreate the linked list. You should only send data that is absolutely essential for the receiver to parse messages correctly and recreate the linked list. Do not send data that can be safely omitted. You can assume that the client and the server use identical processors.

Use the following call for sending data to the server:

sendData(int sock, const void *msg, int msgLength);

The above call sends msgLength bytes of the buffer pointed to by msg to the server, using the socket sock.

b. Unmarshalling data

Show how the server can unmarshal the message and recreate a linked list. Use the following call for sending data to the server:

int receiveData(int sock, void *msg, int msgLength);

This function receives msgLength bytes of data from socket sock and stores it in memory pointed to by msg (memory must be allocated by the calling function).

c. TCP code

Think about how you would write the code for receiving data sent by the sendData() function described above. Write pseudo-code for the following function:

int receiveData(int sock, void *msg, int msgLength);

This function returns either on failure or after it reads msgLength of data from socket sock. The function stores the received data in the buffer pointed to by msg. You can assume that memory has been allocated by the calling function. Use the system call recv() to receive data from the network. receiveData() should return -1 on failure (i.e., when recv() fails) and 0 on success.