960 Chapter 46
Listing 46-9: Client for file server using System V message queues
––––––––––––––––––––––––––––––––––––––––––––––––– svmsg/svmsg_file_client.c
#include "svmsg_file.h"
static int clientId;
static void
removeQueue(void)
{
if (msgctl(clientId, IPC_RMID, NULL) == -1)
q errExit("msgctl");
}
int
main(int argc, char *argv[])
{
struct requestMsg req;
struct responseMsg resp;
int serverId, numMsgs;
ssize_t msgLen, totBytes;
if (argc != 2 || strcmp(argv[1], "--help") == 0)
usageErr("%s pathname\n", argv[0]);
if (strlen(argv[1]) > sizeof(req.pathname) - 1)
cmdLineErr("pathname too long (max: %ld bytes)\n",
(long) sizeof(req.pathname) - 1);
/* Get server's queue identifier; create queue for response */
serverId = msgget(SERVER_KEY, S_IWUSR);
if (serverId == -1)
errExit("msgget - server message queue");
w clientId = msgget(IPC_PRIVATE, S_IRUSR | S_IWUSR | S_IWGRP);
if (clientId == -1)
errExit("msgget - client message queue");
e if (atexit(removeQueue) != 0)
errExit("atexit");
/* Send message asking for file named in argv[1] */
req.mtype = 1; /* Any type will do */
req.clientId = clientId;
strncpy(req.pathname, argv[1], sizeof(req.pathname) - 1);
req.pathname[sizeof(req.pathname) - 1] = '\0';
/* Ensure string is terminated */
r if (msgsnd(serverId, &req, REQ_MSG_SIZE, 0) == -1)
errExit("msgsnd");