linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Prasanta Sadhukhan <Prasanta.Sadhukhan@Sun.COM>
To: linux-c-programming@vger.kernel.org
Subject: Message queue
Date: Wed, 20 Dec 2006 16:40:01 +0530	[thread overview]
Message-ID: <45891A09.1060204@sun.com> (raw)

[-- 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;
}

             reply	other threads:[~2006-12-20 11:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-20 11:10 Prasanta Sadhukhan [this message]
2006-12-20 14:45 ` Message queue Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=45891A09.1060204@sun.com \
    --to=prasanta.sadhukhan@sun.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).