Thread: Client to Server Communication using a Message Queue

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

    Client to Server Communication using a Message Queue

    So here's my issue:
    I need to implement a program(client and server) using message queues instead of named pipes (FIFO's).

    I already do the client and server programs using named pipes (FIFO's), but I need change these programs in order to use message queues.

    Any idea for the new code?
    Thanks

    The client program:
    Code:
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <float.h>
    #include <fcntl.h>
    #include <limits.h>
    
    #define  FIFO_Name "FIFO"
    
    int main()
    {	
    	FILE *inasc;
    	float fltmax=FLT_MAX;
    	float aux;
    	int ffopen;
    
    	
    	if((ffopen = open(FIFO_Name,O_WRONLY)) < 0)
    	{
    		perror("\nFIFO not open\n");
    		exit(1);
    	}
    	
    	if((inasc=fopen("input.asc","rb")) < 0)
    	{
    		perror("\nERROR OPEN THE FILE input.asc\n");
    		exit(1);
    	}
    
    	
    	else 
    	{
    		printf("\nOPEN THE FILE input.asc\n");
    		while(!feof(inasc))
    		{
    			int ret=fscanf(inasc, "%f", &aux); 	
    			if(ret==1){			
    				if(aux<FLT_MAX){
    					write(ffopen,&aux,sizeof(aux)); 
    					printf("DATA SEND TO FIFO %f\n",aux);
    				}
    			}				
    			
    		}
    	/**/
    	write(ffopen,&fltmax,sizeof(fltmax));
    	close(ffopen);
    	}
    return 0;
    }
    The server program:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <fcntl.h>
    #include <float.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <sys/un.h>
    #include <unistd.h>
    #include <sys/stat.h>
    
    #define FIFO_Name "FIFO"
    
    int main()
    {
    	remove(FIFO_Name);
    	remove("input.bin");
    	
    	int inbin;
    	FILE *bin;
    	FILE *outasc;
    
    	int conta = 0;
    	
    	
    	int ff = mkfifo(FIFO_Name, O_CREAT|O_EXCL | 0777);
    	if (ff < 0)
    	{
    		perror("\nFIFO NOT CREATED\n");
    		exit(1);
    	}
    	else
    	{
    		printf("\nFIFO CREATED! CONECTING...\n");
    	}
    
    	
    	int ffread = open(FIFO_Name,O_RDONLY);
    	if(ffread < 0)
    	{
    		perror("\nISN'T POSSIBLE READ THE FIFO!\n");
    		exit(1);
    	}
    	else
    	{
    		printf("\nEND OF READING.\n");
    	}
    
    	float TEMP;
    	
    	
    	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");
    		while(TEMP != FLT_MAX)
    		{
    			read(ffread, &TEMP, sizeof(TEMP));
    			if(TEMP<FLT_MAX){				
    				write(inbin,&TEMP,sizeof(TEMP));
    				printf("DATA RECEIVED IN CLIENT: %f\n",TEMP);
    				conta++;
    			}								
    			
    		}
    	}
    	
    	
    	int closeinbin = close(inbin);
    	if(closeinbin < 0)
    	{
    		perror("\nERROR CLOSE THE FILE input.bin\n");
    		exit(1);
    	}
    	float tab[conta];
    
    	
    	int readinbin = fread(&tab,sizeof(tab),1,(bin=fopen("input.bin","r")));
    	if(bin < 0 || readinbin < 0)
    	{
    		perror("\nERROR TRY TO OPEN FILE input.bin\n");
    		exit(1);
    	}
    	
    	if(fclose(bin) < 0)
    	{
    		perror("\nERROR CLOSE THE FILE input.bin!\n");
    		exit(1);
    	}
    	remove(FIFO_Name);
    	
    
    
    	if((outasc=fopen("output.asc","a")) < 0)	/
    	{
    		perror("ERROR OPEN output.asc");
    		exit(1);
    	}
    
    	
    	int i;
    	
    	printf("\n\n IN FILE ASCII WAS WRITE:\n");
    	printf("-------------------------------------------\n");
    	for(i = 0; i < conta; i++)
    	{
    		char BUFFER[20]; 
    		tab[i] = 3.0*tab[i];	
    		sprintf(BUFFER,"%lf",tab[i]);
    		fputs(BUFFER,outasc);
    		fputs("\n",outasc);
    		printf("WRITE %f \n",tab[i]);
    	}
    
    	if(fclose(outasc) < 0)
    	{
    		printf("ERROR CLOSE THE FILE");
    		exit(1);
    	}
    	
    	return 0;
    }
    The file input.asc can be:

    1.1
    2.2
    3.3
    4.4
    5.5

  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
    Yes, you read the manual pages on message queues, and try to write a simple test program which sends
    Code:
    char message[] = "hello world";
    through the queue.

    Then you post your attempt here if you get stuck somewhere.

    But if you figure it out from your simple example, you can then replace the FIFO bits in your code with the MQ bits, and you're done.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client to Server Communication using a Message Queue
    By cr80expert5 in forum C Programming
    Replies: 2
    Last Post: 05-09-2011, 11:35 PM
  2. Raw Socket Client and Server Communication Problem
    By rplumii in forum C Programming
    Replies: 6
    Last Post: 02-10-2011, 07:02 AM
  3. 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
  4. 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
  5. clent-client communication inc
    By cnu_sree in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-04-2006, 11:58 PM