3 * Copyright (C) 2002 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /****************************************************************
22 * Fortune Cleint - Using Message Queues
24 * Author: Simone Piccardi
27 * Usage: fortune -h give all info
29 * $Id: MQFortuneClient.c,v 1.1 2002/10/20 22:42:48 piccardi Exp $
31 ****************************************************************/
33 * Include needed headers
35 #include <sys/types.h> /* predefined types */
36 #include <sys/stat.h> /* */
37 #include <unistd.h> /* include unix standard library */
38 #include <stdio.h> /* include standard I/O library */
39 #include <stdlib.h> /* standard library */
40 #include <string.h> /* ANSI C standard string */
41 #include <errno.h> /* errorstring */
42 #include <signal.h> /* signals */
49 /* Maximum message size */
52 /* Subroutines declaration */
55 int main(int argc, char *argv[])
57 /* Variables definition */
59 struct msgbuf_read { /* message struct to ask fortune to server */
60 long mtype; /* message type, must be 1 */
61 long pid; /* message data, must be the pid of the client */
63 struct msgbuf_write { /* message struct to get result from server */
64 long mtype; /* message type, will be the pid of the client*/
65 char mtext[MSGMAX]; /* message data, will be the fortune */
67 int msgid; /* Message queue identifier */
68 key_t key; /* Message queue key */
69 int size; /* message size */
71 * Input section: decode parameters passed in the calling
74 opterr = 0; /* don't want writing to stderr */
75 while ( (i = getopt(argc, argv, "h")) != -1) {
84 case '?': /* unrecognized options */
85 printf("Unrecognized options -%c\n", optopt);
87 default: /* should not reached */
91 /* ***********************************************************
93 * Options processing completed
97 * ***********************************************************/
99 * Comunication section
101 key = ftok("./MQFortuneServer.c", 1);
102 msgid = msgget(key, 0);
104 perror("Cannot find message queue");
108 /* Main body: do request and write result */
109 msg_read.mtype = 1; /* type for request is always 1 */
110 msg_read.pid = getpid(); /* use pid for communications */
111 size = sizeof(msg_read.pid);
112 msgsnd(msgid, &msg_read, size, 0); /* send request message */
113 debug("sent request from %d\n", msg_read.pid);
114 msgrcv(msgid, &msg_write, MSGMAX, msg_read.pid, MSG_NOERROR);
115 printf("%s", msg_write.mtext);
118 * routine to print usage info and exit
121 printf("Elementary fortune server\n");
123 printf(" fortuned [-h] [-f] -n XXX \n");
124 printf(" -h print this help\n");
125 printf(" -f filename set file for fortunes\n");
126 printf(" -n XXX set pool depth\n");