Client to Server Communication using a Message Queue

This is a discussion on Client to Server Communication using a Message Queue within the C Programming forums, part of the General Programming Boards category; I'm implementing a program of communication by sending and receiving data between a server and a client: I have a ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Client to Server Communication using a Message Queue

    I'm implementing a program of communication by sending and receiving data between a server and a client:

    I have a problem when writing data to a file and multiply it by 3.0. The program is only doing it for the first number stored in the file.

    This is because when reading numbers from a file input.asc the program is read as a single message, and should read number by number and send to the server.

    The file input.asc can have any number for example:
    1.0
    2.0
    3.0

    How to change this part of the code to read number by number to the message (msg.buffer)?
    The part of the code to change itś brown.

    Client:

    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());
    
    
    
        //Opening input file, throw an error if invalid file
    
        inFile = fopen("input.asc", "r");
    
        if(inFile == NULL){
    
            printf("Unable to open File = input.asc");
    
            return 1;
    
        }
    
    //Copy input 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);
    
    }

    Server:

    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 inbin;
    
    FILE *bin;
    
    FILE *outasc;
    
    int conta = 0;
    
    
    
    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);
    
        }
    
    
    
        //Aquiring Cliend PID to message return
    
        long client = msg.msg_fm;
    
    	
    
        //Server displays received message
    
        printf("SERVER receives:\n");
    
        printf("%s\n", msg.buffer); 
    
        float aux=atof(msg.buffer);
    
        printf("Conversion:%lf\n", aux);
    
    	
    
        //Creating the file input.bin
    
    	if((inbin = open("input.bin",O_WRONLY|O_TRUNC|O_CREAT,0600)) < 0)
    
    	{
    
    		perror("\nERROR NO input.bin!\n");
    
    		exit(1);
    
    	}
    
    	else
    
    	{
    
    		printf("\n File input.bin read\n");
    
    		printf("-------------------------\n\n");
    
    		int i=0;
    
       		while(msg.buffer[i] != '\0'){
    
    			write(inbin,&aux,sizeof(float));
    
    			i++;
    
    			conta++;
    			}								
    
    		}
    
    	
    
    	//Close the file input.bin
    
    	int closeinbin = close(inbin);
    
    	if(closeinbin < 0)
    
    	{
    
    		perror("\nERROR close the file input.bin\n");
    
    		exit(1);
    
    	}
    
    
    
    	float tab[conta];
    
    
    
    	//Open input.bin and saved the data
    
    	int readinbin = fread(&tab,sizeof(tab),1,(bin=fopen("input.bin","r")));
    
    	if(bin < 0 || readinbin < 0)
    
    	{
    
    		perror("\n ERROR open the file input.bin\n");
    
    		exit(1);
    
    	}
    
    	//Close the file input.bin
    
    	if(fclose(bin) < 0)
    
    	{
    
    		perror("\nERROR close the file input.bin!\n");
    
    		exit(1);
    
    	}
    
    
    
    			
    
    	//Criar o output.asc//
    
    	if((outasc=fopen("output.asc","a")) < 0)
    
    	{
    
    		perror("ERROR open the file output.asc");
    
    		exit(1);
    
    	}
    
    	//Write in file output.asc//
    
    	int a;
    
    	for(a = 0; a < conta; a++)
    
    	{
    
     		char BUFFER[20];
    
    		tab[a] = 3.0*tab[a];		
    
    		sprintf(BUFFER,"%f",tab[a]);
    
    		fputs(BUFFER,outasc);
    
    		fputs("\n",outasc);
    
    		printf("Wrote: %f \n",tab[a]);
    
    	}
    
    if(fclose(outasc) < 0)
    
    	{
    
    		printf("ERROR close the file");
    
    		exit(1);
    
    	}	
    
        //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 Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So instead of reading until end-of-file, why not read until end-of-line (or any whitespace, really)?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Also... a minor grumble... could you please not double linespace your code... you're wearing out the wheel on my mouse!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-19-2011, 11:14 PM
  2. Client to Server Communication using a Message Queue
    By cr80expert5 in forum C Programming
    Replies: 2
    Last Post: 05-10-2011, 12:35 AM
  3. Raw Socket Client and Server Communication Problem
    By rplumii in forum C Programming
    Replies: 6
    Last Post: 02-10-2011, 07:02 AM
  4. 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
  5. sockets: Why does server[buffer] > client[message]? + more questions
    By heras in forum Networking/Device Communication
    Replies: 4
    Last Post: 03-10-2008, 05:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21