1

I am trying the following code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <mqueue.h>
#include "sctp.h"
#include "s1ap.h"
#include "messages.h"

void send_to_s1ap(void);

int main()
{
    pthread_t tid;

    pthread_create(&tid, NULL, s1ap, (void *)&tid);
    pthread_create(&tid, NULL, sctp, (void *)&tid);

    send_to_s1ap();

    pthread_exit(NULL);
    return 0;
}

void send_to_s1ap(void)
{
    int status = 0;
    mqd_t mqfd;
    struct mq_attr attr;
    int open_flags = 0;
    int priority_of_msg;
    message msg;

    attr.mq_maxmsg = 100;
    attr.mq_msgsize = 100;
    attr.mq_flags   = 0;
    open_flags = O_WRONLY|O_CREAT;
    mqfd = mq_open("/s1ap_queue",open_flags,0660,&attr);
    if (mqfd == -1)
    {
        printf("mq_open failure from main\n");
        return;
    }
    /* Fill in a test message buffer to send */

    msg.msg_id = 1;
    msg.data[0] = 'P';
    msg.data[1] = 'R';
    msg.data[2] = 'I';
    msg.data[3] = 'O';
    msg.data[4] = 'R';
    msg.data[5] = 'I';
    msg.data[6] = 'T';
    msg.data[7] = 'Y';
    msg.data[8] = '1';
    msg.data[9] = 'a';
    priority_of_msg = 1;

    status = mq_send(mqfd,(const char *) &msg,sizeof(msg),priority_of_msg);

    if (status == -1)
    {
        perror("mq_send failure on mqfd\n");
    }
    else
    {
        printf("successful call to mq_send\n");
    }
    if (mq_close(mqfd) == -1)
    {
        printf("mq_close failure on mqfd");
    }
}

where in messages.h I declare

#define max_msg_size 10

typedef struct message {
    int msg_id;
   char data[max_msg_size];
message;

I get the following error code: mq_send failure on mqfd : Message too long

I have commented out all the code in the client side (which is doing mq_receive) , so the error doesnt come from there. Any ideas what is causing the problem?

I tried changing

attr.mq_maxmsg = 100; attr.mq_msgsize = 100;

but it didnt help

Edit:

I did an example with

void send_to_s1ap(void)
{
    int status = 0;
    mqd_t mqfd;
    struct mq_attr attr;
    int open_flags = 0;
    int priority_of_msg;
    char buffer2[12] = "Hello world";
    
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = 100;
    attr.mq_flags   = 0;
    attr.mq_curmsgs = 0;
    open_flags = O_WRONLY|O_CREAT;
    mqfd = mq_open("/s1ap_queue",open_flags,0660,&attr);
    if (mqfd == -1)
    {
        printf("mq_open failure from main\n");
        return;
    }
    /* Fill in a test message buffer to send */


    
    priority_of_msg = 1;

    status = mq_send(mqfd,buffer2,sizeof(buffer2),priority_of_msg);

    if (status == -1)
    {
        perror("mq_send failure on mqfd\n");
    }
    else
    {
        printf("successful call to mq_send\n");
    }
    if (mq_close(mqfd) == -1)
    {
        printf("mq_close failure on mqfd");
    }
}

and its still failing with

mq_send failure on mqfd : Message too long

So I guess its not the struct that is causing the issue, but indeed for some reason its not sending big structs/strings. Any idea why?

Haris
  • 11
  • 2
  • Check if this helps. https://stackoverflow.com/questions/5625845/mq-receive-message-too-long – Karthick Mar 14 '23 at 10:29
  • Hi, no it doesnt help, the problem is at mq_send() side, not at mq_receive side. All the mq_receive code has been commented out. – Haris Mar 14 '23 at 11:04
  • Does a simple Hello World message work via this code? First try that. – kiner_shah Mar 14 '23 at 11:37
  • Hi, yes its possible to send a simple string via this code. But when switching to a struct then its not working. – Haris Mar 14 '23 at 13:17
  • After doing examples, I noticed mq_send works properly up to #define max_msg_size 4 . If I do #define max_msg_size 5 then it fails as explained above – Haris Mar 14 '23 at 14:00
  • @Haris Did you confirm this? `Don't forget to unlink the message queue before running your program again. If you dont unlink it, it will still use the old message queue settings. This happens when you end your program with Ctrl+C. ` – Karthick Mar 14 '23 at 15:35
  • You forgot a curly bracket in `typedef struct message`. – xihtyM Mar 16 '23 at 13:44
  • Thank you I have fixed this and still not working – Haris Mar 17 '23 at 10:03

0 Answers0