Message queues

Hi all,

I've been trying for hours to figure out how to turn my 2-program (one to send and one to receive) "chat system" using message queues, into a single program where each concurrent component (entity) will both send and receive messages. PLEASE give me a hand with this, I'm starting to think I'm stupid (Maybe I am, but only I can say it :wink: )

THANX!!

Here I give you the code to both programs. Both of them are working fine:

/*Sending Program*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct my_msg_st
{
        long int my_msg_type;
        char some_text[BUFSIZ];
}


main()
{
        int running=1;
        int msgid;
        char buffer[BUFSIZ];
        struct my_msg_st some_data;

        msgid=msgget((key_t)1234, 0666 | IPC_CREAT);

        if(msgid == -1)
        {
                fprintf(stderr, "msgget failed\n");
                exit(EXIT_FAILURE);
        }

        while(running)
        {
                printf("Enter some text: ");
                fgets(buffer, BUFSIZ, stdin);
                some_data.my_msg_type= 1;
                strcpy(some_data.some_text, buffer);

                if(msgsnd(msgid, (void*)&some_data, BUFSIZ, 0) == -1)
                {
                        fprintf(stderr, "msgsnd failed\n");
                        exit(EXIT_FAILURE);
                }

                if (strncmp(buffer, "end", 3) == 0)
                {
                        running=0;
                }
        }

        exit(EXIT_SUCCESS);
}
/*Receiving program*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct my_msg_st
{
        long int my_msg_type;
        char some_text[BUFSIZ];
}


main()
{
        int running=1;
        int msgid;
        struct my_msg_st some_data;
        long int msg_to_receive=0;

        msgid=msgget((key_t)1234, 0666 | IPC_CREAT);

        if(msgid == -1)
        {
                fprintf(stderr, "msgget failed\n");
                exit(EXIT_FAILURE);
        }

        while(running)
        {
                if(msgrcv(msgid, (void *)&some_data, BUFSIZ, msg_to_receive, 0) == -1)
                {
                        fprintf(stderr, "msgrcv failed\n");
                        exit(EXIT_FAILURE);
                }

                printf("You wrote: %s", some_data.some_text);

                if(strncmp(some_data.some_text, "end", 3) == 0)
                {
                        running=0;
                }
        }

        if(msgctl(msgid, IPC_RMID, 0) == -1)
        {
                fprintf(stderr, "msgctl(IPC_RMID) failed\n");
                exit(EXIT_FAILURE);
        }

        exit(EXIT_SUCCESS);
}

You mean that you want the same program to send and receive messages, but how do you plan to run this exactly?
Will the program recv messages that it has itself sent? Or will there be two or more instances of the program running, each putting up messages of its own, while 'recv'ing all other messages?

That's the one I'm looking for.

You would be best off having two programs, but not in the same way that you have created. Have one program that is the "server". This will create the message queue when it starts up, and destroy the queue when it shuts down. The second program will be the client program that will just connect to the message queue (no IPC_CREAT).

The client program will put messages with a type flag set to some unique value ( using the program's pid would be a good idea ); while msgrcv will be done with type set to the the unique value, but with the flag set to MSG_EXCEPT - this will pick up messages with any type that is not the value set as msgtyp in the msgrcv call.

A single program will probably complicate things unnecessarily.

Yeah, I DID think think about a server/client approach to this problem, but since I'm fairly new to UNIX I didn't know how to apply my limited knowledge to this type of implementation, so I just tried the above solution taking bits and pieces from textbooks and from what I learned at uni. Would u happen to have a sample code for this?? I'm sorry I'm such a pain in the ass, but I can admit when I need help, and this is one of those ocassions.

Thanx a lot for your help blowtorch, you're a legend.

I know you already answered this question, but will there be exactly two or more than two processes communicating between each other? This would affect how the program will work.

I'm supposed to write a set of programs that allow users logged on to a host to send messages to each other.

At first, I should start with this messenger program I'm telling you about and add to it a coordination program. Since the coordinator should accept messages from the clients and forward them to all attached clients, I think that the answer to your question is that there will be more than 2 processes communicating between each other.

THANX!!!

You can go for a client-server sort of model: The server can have two message queues that are known to all client systems.
One will be a control queue - when a client comes up it will create a new message queue and send the message queue id that is obtained to the server over the control queue. The server will store the msqids-pid(of the client) pairs - where these will be used will be seen later.
The second message queue will be the data queue - this queue will be the one where all clients send their data messages. The message type here must be something unique, the pid of the client process will be perfect.
The server can read messages from the data queue in the order that they are available - msgtyp=0 can be used. The server will then send messages to all the queues that are known to it (the msqid - pid pairs stored before will be used here), except to the pid that sent the message to it. Here you can use msgtyp=1 - you will never get the pid of any client equal to 1.

In cleanup, the server will remove the control and data message queues, the clients will delete the message queues that they created.

I know that the programming part may be a bit complicated, but with an unknown number of clients connecting, I think that this would be the best way to go.

Thanx a lot blowtorch, I'll try to figure out the coding part :wink:

one difference b/w msgqueues & shared memory.
wat is exact meaing of subnetting