LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   ipc problem (https://www.linuxquestions.org/questions/programming-9/ipc-problem-13417/)

raven 02-03-2002 05:45 AM

ipc problem
 
hello

i have a problem using ipc-message-queues.

if i put this into my code:

if ((msgrcv(msgid,message,60,0,MSG_NOERROR))<0)

then reading of the message causes the program to exit (error message: bad address)

does the message has to have a special type? the client sends a message with type:

char *message;

ist this OK?

then what is the problem?

i can initialise the queue, no problems there, everything seems to work, the client sends the message coorectly (or at least it seems so), but the server cannot read the message from the queue.

any help is welcome

raven

crabboy 02-03-2002 02:12 PM

I'm not sure exactly what your code looks like, so I'm unable to give you a better clue of what is wrong. I can provide you with a simple example that works:

Hope this helps.

Code:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct message
{
  long message_type;
  char message_data[256];
};

main()
{
  int msgid;
  struct message msg;
  struct message msg_in;

  // create message queue
  msgid = msgget( 123, IPC_CREAT | 0644 );
  if ( msgid == -1 )
  {
      printf("Unable to get message queue\n");
      exit(-1);
  }  // end if

  msg.message_type = 1;
  strcpy( msg.message_data, "Hello there" );

  msgsnd( msgid, &msg, sizeof( msg ) - sizeof( long ), 0 );

  msgrcv( msgid, &msg_in, sizeof( msg_in ) - sizeof( long ), 1, 0 );

  printf( "Message is: [%s]\n", msg_in.message_data );

  // remove message queue
  msgctl( msgid, IPC_RMID, 0 );
}


crabboy 02-03-2002 02:14 PM

I forgot to mention some commands that can be helpful when dealing with shared mem, semaphores and queues.
Code:

ipcs
ipcrm


raven 02-04-2002 10:53 AM

thank you it works now fine.

the problem was i sent a plaintext message to the server and no struct...

thanks anyway

raven


All times are GMT -5. The time now is 10:46 AM.