* Message queue
@ 2006-12-20 11:10 Prasanta Sadhukhan
2006-12-20 14:45 ` Glynn Clements
0 siblings, 1 reply; 2+ messages in thread
From: Prasanta Sadhukhan @ 2006-12-20 11:10 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 458 bytes --]
Hi,
I was trying to use the message queue implementation of linux through a
simple client/server program.
The client will get text from stdin and send to server through message
queue and server will convert to upper case and resend to back to client
Attached is the client and server program I used
However, while sending in client and receiving in server, I am getting
errno as 13(EACCESS). Can anyone tell me what I need to do extra?
Regards
Prasanta
[-- Attachment #2: client.c --]
[-- Type: text/x-csrc, Size: 1468 bytes --]
#include <stdio.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
#define key_in 1234
#define key_out 4321
#define size 1024
static int input_id;
static int output_id;
static int send_id;
static int receive_id;
void term()
{
printf("Strg+c...sending quit to msq\n");
msgsnd(output_id,"quit",strlen("quit"),0);
msgctl(output_id,IPC_RMID,0);
exit(0);
}
int main(int argc, char *argv[])
{
int n;
char outtext[size];
char intext[size];
signal(SIGINT,term);
input_id=msgget(key_in,IPC_CREAT);
output_id=msgget(key_out,IPC_CREAT);
if (input_id == -1)
{
printf("input msgget failed ....%d\n",errno);
return -1;
}
if (output_id == -1)
{
printf("output msgget failed ....\n");
return -1;
}
while(1)
{
fgets(outtext, 10, stdin);
printf("output text %s\n", outtext);
send_id=msgsnd(output_id,outtext,strlen(outtext),0);
if (send_id < 0)
{
printf("msg snd failed ...%d\n",errno);
return -1;
}
if (strcmp(outtext,"quit") == 0)
{
printf("<<exiting>>\n");
msgctl(output_id,IPC_RMID,0);
return 0;
}
receive_id=msgrcv(input_id,intext,strlen(intext),0,0);
if(receive_id < 0)
{
printf("msgrcv failed ...\n");
return -1;
}
fprintf("input text %s\n", intext);
}
}
[-- Attachment #3: server.c --]
[-- Type: text/x-csrc, Size: 1481 bytes --]
#include <stdio.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
#define key_in 1234
#define key_out 4321
#define size 1024
static int send_id;
static int receive_id;
int main()
{
int n,tooutput_id,toinput_id,receive_id,send_id;
char text[size];
//key_out as input as this is the msgq where client will send its output
toinput_id=msgget(key_out, IPC_CREAT);
if (toinput_id == -1)
{
printf("input msgget failed ...\n");
return -1;
}
tooutput_id=msgget(key_in, IPC_CREAT);
if (tooutput_id == -1)
{
printf("output msgq failed ...\n");
return -1;
}
printf("Server ...\n");
while(1)
{
receive_id = msgrcv(toinput_id,text,strlen(text),0,0);
if(receive_id < 0)
{
printf("msgrcv failed ...%d\n", errno);
return -1;
}
if(strcmp(text,"quit")==0)
{
msgsnd(tooutput_id,text,strlen(text),0);
printf("<<exiting>>\n");
msgctl(toinput_id,IPC_RMID,0);
msgctl(tooutput_id,IPC_RMID,0);
return 0;
}
puts(text);
for (n=0;n<strlen(text);n++)
{
text[n]=toupper(text[n]);
}
printf("Changed to: ");
puts(text);
printf("Server output text %s\n", text);
send_id = msgsnd(tooutput_id,text,strlen(text),0);
if(send_id < 0)
{
printf("msgsend failed ...\n");
return -1;
}
}
return 0;
}
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: Message queue
2006-12-20 11:10 Message queue Prasanta Sadhukhan
@ 2006-12-20 14:45 ` Glynn Clements
0 siblings, 0 replies; 2+ messages in thread
From: Glynn Clements @ 2006-12-20 14:45 UTC (permalink / raw)
To: Prasanta Sadhukhan; +Cc: linux-c-programming
Prasanta Sadhukhan wrote:
> I was trying to use the message queue implementation of linux through a
> simple client/server program.
> The client will get text from stdin and send to server through message
> queue and server will convert to upper case and resend to back to client
> Attached is the client and server program I used
> However, while sending in client and receiving in server, I am getting
> errno as 13(EACCESS). Can anyone tell me what I need to do extra?
For a start, the side which creates the message queue (probably the
server) needs to set the permissions, e.g.:
#include <sys/stat.h>
...
toinput_id=msgget(key_out, IPC_CREAT|S_IRUSR|S_IWUSR);
...
tooutput_id=msgget(key_in, IPC_CREAT|S_IRUSR|S_IWUSR);
Otherwise, access will be refused, hence EACCESS.
Some other issues:
The third argument to msgrcv() needs to be the size of the buffer,
e.g. sizeof(text) not strlen(text).
The return value from main should be non-negative, zero for success
and greater than zero for failure. In practice, a value of -1 will
typically result in an exit code of 255 due to being truncated to 8
bits.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2006-12-20 14:45 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-12-20 11:10 Message queue Prasanta Sadhukhan
2006-12-20 14:45 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).