Bogdan M.
Bogdan M.

Reputation: 2181

Basic unix server-client IPC(msg queue) issue

I'm working on a problem wich implies a basic running server client(done one before). the problem is that I run server than I run client. My msg Que is created i nboth client takes my char as input sends it, i get the confirmation print but my server msgrcv isn't responding.

s.c

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

#include "struc.h"


int main(){
    int qt;
    struct msg m;

    qt = msgget(1271,IPC_CREAT|IPC_EXCL|0600);
    if(qt < 0){ perror("Error MSGGET()\n");}
    printf("msg queue created!\n");

    if(msgrcv(qt,&m,sizeof(struct msg),0,0)<0){
        perror("Msg recive error");
    }   
    printf("msg recived!\n");


    msgctl(qt,IPC_RMID,NULL);
    return 0;
}

c.c

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

#include "struc.h"

int main(){
    int qt;
    struct msg m;
    qt = msgget(1271,0);
    if(qt < 0){ perror("~~~ Error MSGGET()\n");}
    printf("msg created!\n");

    printf("Enter one char: !\n");
    scanf("%c",&m.c);

    msgsnd(qt, &m,sizeof(struct msg),0);
    printf("msg sent!\n");
return 0;
}

struc.h

struct msg{
    long mtype;
//  matrix M;
    char c;
};

(By creating the 3 files you cna test it yourself. Any idea is welcome maybe i missed something)

Upvotes: 0

Views: 2214

Answers (1)

nos
nos

Reputation: 229108

You should do this to verify that sending doesn't fail.

 if(msgsnd(qt, &m,sizeof(struct msg),0)) < 0) {
    perror("Msg send error");
  }

You should also heed the docs for msgsnd:

The msgp argument is a pointer to caller-defined structure of the 
following general form:

       struct msgbuf {
           long mtype;       /* message type, must be > 0 */
           char mtext[1];    /* message data */
        };

That is, you have to set the mtype in the message you send to be > 0. Currently it is uninitialized, so you should do:

m.mtype = 1;
if(msgsnd(qt, &m,sizeof(struct msg),0)) < 0) {
   perror("Msg send error");
}

Upvotes: 1

Related Questions