Thread: simultaneously waiting for data on FIFO and UDP using select call

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    4

    simultaneously waiting for data on FIFO and UDP using select call

    Hi,

    I am facing one typical problem,

    I am writing a server program in linux which is waiting for data either on UDP port or on FIFO. I use "Select" call for waiting on data.

    But, select only works for UDP or FIFO. that means, if initially (first time after server is started) data comes on UDP port, then select call only listens to UDP port only. It never responds to data on FIFO.


    Similarly, if initially data is received by fifo, select call next time responds only to fifo. It never responds for data on UDP port.

    Code is give below. Can anybody help me on this? I want to receive data on both FIFO and UDP.

    Regards,
    Yogesh

    Code:
    #define SERVER_FIFO_NAME "/tmp/server_fifo"
    
    int main()
    {
    	unsigned char incomingData[MAXBUFSIZE];
    	int server_fifo_fd;  // this is my fifo file descriptor
            int server_socket; // this is my socket descriptor
            struct sockaddr_in server_address;
    	int server_len;
    
    	int numberOfBytesRead=0;
    	int dummy_fifo_fd;
    	fd_set master;
    	int fdmax;
            
          server_socket=socket(AF_INET,SOCK_DGRAM,0);
            if(-1 == server_socket)
            {
                    printf("Error: Creating socket");
                    exit(1);
            }
    
            server_address.sin_family=AF_INET;
            server_address.sin_addr.s_addr=INADDR_ANY;
            server_address.sin_port=htons(9734);
            memset(&(server_address.sin_zero),0x00,8);
            server_len=sizeof(struct sockaddr);
    
            if(-1==bind(server_socket,(struct sockaddr *)&server_address,server_len))
            {
                    printf("Binding Failed\n");
                    exit(1);
            }
    	
            FD_ZERO(&master);
    
    
            mkfifo(SERVER_FIFO_NAME,0777); //fifo created
            
    	server_fifo_fd=open(SERVER_FIFO_NAME,O_RDONLY|O_NONBLOCK);
            dummy_fifo_fd=open(SERVER_FIFO_NAME,O_WRONLY);
            
    	if(-1==server_fifo_fd)
            {
                    perror("Server fifo opening failure");
                    exit(1);
            }
    
    	FD_SET(server_fifo_fd,&master);  //fifo added to fdmaster list
            fdmax=server_fifo_fd;
    	FD_SET(server_socket,&master); //socket added to fdmaster list
    	if(server_socket>fdmax)
    	{
    		fdmax=server_socket;
    	}	
    	
    	
       	for(;;)
            {
                    memset(incomingData,'\0',sizeof(incomingData));
                    if(select(fdmax+1,&master,NULL,NULL,0)==-1)    //select call which waits for data
                    {
                            perror("Select Failed");
                            exit(1);
                    }
    
    		if(FD_ISSET(server_fifo_fd,&master)) //if data is on fifo
                    {
    		 	numberOfBytesRead=read(server_fifo_fd,incomingData,sizeof(incomingData));
                             
    			if(numberOfBytesRead!=0)
                            {
                            	printf("Printing buffer now %d\n",numberOfBytesRead);
                            	printf("Received from pipe %s\n ", incomingData);
                            }
    	
    		}
    		else if(FD_ISSET(server_socket,&master)) //if data is on udp socket
    		{
                                //code  for receiveing data from udp
    		}
                 //code for processing received data
            }//end of for loop
       // code for closing fifo and sockets
      }//end of main

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. you don't call FD_CLR to empty the set before doing the add

    2. you probably need to take a copy of the set, since select will modify it.
    Eg.
    Code:
    for ( ;; ) {
      fd_set copy = master;
      if(select(fdmax+1,&copy,NULL,NULL,0)==-1)
      // more tests on copy
    }
    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
    Jan 2007
    Posts
    4

    Problem solved.

    Thanks a lot Salem,

    I maintained copy of set and the problem is solved now.

    Your help is higly appreciated.
    Yogesh

Popular pages Recent additions subscribe to a feed