Thread: Client to Server Communication using a Message Queue

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Client to Server Communication using a Message Queue

    So here's my issue. I enable the server first and then enable the client to send the message over to server, where the server does a simple lowercase to uppercase transformation on the message sent from the client, and then the server sends it back to the client with all uppercase letters. However, during the process of communication on the server side after receiving the client's message, I'm receiving a Segmentation Fault and I can't track down why. Any idea of why this is happening?


    Here is the client side's code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <string.h>
    #include <ctype.h>
    #include <fcntl.h>
    
    #define SERVER 1L
    typedef struct {
        long    msg_to;
        long    msg_fm;
        char    buffer[BUFSIZ];
    } MESSAGE;
    
    int mid;
    key_t key;
    struct msqid_ds buf;
    MESSAGE msg;
    FILE *inFile;
    
    int main(int argc, char** argv) {
    
        //Aquiring Message Queue ID
        key = ftok(".", 'z');
        mid = msgget(key, 0);
    
        //Display Message Queue and Client ID
        printf("Message Queue ID: %d\n", mid);
        printf("Client ID: %ld\n", (long)getpid());
    
        //Opeining input file, throw an error if invalid file
        inFile = fopen(argv[1], "r");
        if(inFile == NULL){
            printf("Unable to open File = %s\n", argv[1]);
            return 1;
        }
    
        //Copy input characters into msg.buffer, loops breaks when EOF is reached
        int i = 0;
        while(1){
            msg.buffer[i] = fgetc(inFile);
            if(msg.buffer[i]==EOF){
                msg.buffer[i] = '\0';
                break;
            }
            i++;
        }
    
        //Displaying message before conversion of server
        printf("Message before conversion:\n");
        printf("%s\n", msg.buffer);
    
        //Getting Client PID and preparing message to message queue
        long iD = (long)getpid();
        msg.msg_to = SERVER;
        msg.msg_fm = (long)getpid();
    
        //Send message to Message Queue for Server, throws and error for invalid input
        if(msgsnd(mid, &msg, sizeof(msg.buffer), 0)==-1){
            perror("msgsnd");
            exit(-1);
        }
    
        //Client waits for response from Server, throws an error if invalid input
        if(msgrcv(mid, &msg, sizeof(msg), iD, 0)<0){
            perror("msgrcv");
            exit(-1);
        }
    
        //Display new converting message.
        printf("Message after conversion\n");
        printf("%s\n", msg.buffer);
    
        //Removing message queue
        msgctl(mid, IPC_RMID, (struct msqid_ds *) 0);
    
        //Client exits
        return (EXIT_SUCCESS);
    }
    And here is the server side's code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <string.h>
    #include <ctype.h>
    #include <fcntl.h>
    
    #define SERVER 1L
    typedef struct {
        long    msg_to;
        long    msg_fm;
        char    buffer[BUFSIZ];
    } MESSAGE;
    
    int mid;
    key_t key;
    struct msqid_ds buf;
    MESSAGE msg;
    
    int main(int argc, char** argv) {
    
        //Creating a message queue
        key = ftok(".", 'z');
        if((mid = msgget(key, IPC_CREAT | 0660))<0){
            printf("Error Creating Message Queue\n");
            exit(-1);
        }
    
        //Display Message Queue and Server ID
        printf("Message Queue ID: %d\n", mid);
        printf("Server ID: %ld\n", (long)getpid());    
    
        //Receiving message from client, throws and error if input is invalid
        if(msgrcv(mid, &msg, sizeof(msg.buffer), SERVER, 0)<0){
            perror("msgrcv");
            exit(-1);
        }
    
        //Server displays received message
        printf("SERVER receives:\n");
        printf("%s\n", msg.buffer);
    
        //Aquiring Cliend PID to message return
        long client = msg.msg_fm;
    
        //convert all lowercase characters to uppercase
        int i;
        while(msg.buffer[i] != '\0'){
            msg.buffer[i] = toupper(msg.buffer[i]);
            i++;
        }
    
        //prep return message
        msg.msg_fm = SERVER;
        msg.msg_to = client;
    
        //send converting message back to client, throws and error if input is invalid
        if(msgsnd(mid, (struct MESSAGE*)&msg, sizeof(msg.buffer), 0)==-1){
            perror("msgsnd");
            exit(-1);
        }
    
        //server exits
        return (EXIT_SUCCESS);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
        //convert all lowercase characters to uppercase
        int i;
        while(msg.buffer[i] != '\0'){
            msg.buffer[i] = toupper(msg.buffer[i]);
            i++;
        }
    What's your initial value of i here?

    Use a for loop, where you can explicitly initialise i to zero.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Much love Salem, thanks for the quick fix. Now my expected output is my actual output .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Raw Socket Client and Server Communication Problem
    By rplumii in forum C Programming
    Replies: 6
    Last Post: 02-10-2011, 07:02 AM
  2. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  3. sockets: Why does server[buffer] > client[message]? + more questions
    By heras in forum Networking/Device Communication
    Replies: 4
    Last Post: 03-10-2008, 04:19 AM
  4. clent-client communication inc
    By cnu_sree in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-04-2006, 11:58 PM
  5. New Thread with same Message Queue?
    By genghis in forum Windows Programming
    Replies: 2
    Last Post: 01-31-2002, 06:17 PM