Thread: Socket Select problem

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    8

    Socket Select problem

    Hi

    I'm having a little trouble with the select and fork for the server side program that I'm building. The problem is I got the socket and everything working and the basic problem can receive and display the data out. But whenever I put in the select with the fork, the program does not receive and display the data anymore. Can someone point me into the right direction of the problem that I have with the code?

    sd is the listening socket descriptor and sd2 is the accepted socket descriptor.

    Code:
    while (1) {
    
       		
    		result = select(sd, &fds, NULL, NULL, NULL);
    		printf("%d", result);
    		if (result > 0) 
    		{
    			printf("entered");
    	 		if (FD_ISSET(sd, &fds)) 
    			{
    
    				alen = sizeof(cad);
    				if ( (sd2=accept(sd, (struct sockaddr *)&cad, &alen)) < 0) {
    					fprintf(stderr, "accept failed\n");
    					exit(1);
    				}
    
    				//make child process
    				printf("forking now");
    				pid = fork();
    
    				if (pid < 0) {
    				        perror("fork");
    				        exit(1);
      				}
    
         				if (pid == 0)   //child process
    				{					
    
    					
    					/* The sd2 has data available to be read */
    					close(sd);
    					n = recv(sd2, buf, sizeof(buf), 0);
    					
    					printf("received size: %d", n);
    					printf("%d", buf);
    					close(sd2);
    				}
        				else		//parent process
    				{
    					printf("This is the parents");
    					close(sd2);
    				}
             				
       			}
    
    		}
    		else if (result < 0) {
    			/* An error ocurred, just print it to stdout */
    			perror("select");
    		}
    		else if (result == 0){
    
    			printf("select is resulting 0");
    		}		
    
    	}
    thank you for all the help

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    select() destroys the fds bitset and you never reinitialize it. You have to do it every time through the outermost loop.

    There might be other problems -- I haven't looked carefully.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    8
    thx for the fast reply.

    I think I got the thing working with one little problem. Right now the code looks like the following with your modification suggestion.

    Code:
    	while (1) {
    
       		
    		result = select(sd+1, &fds, NULL, NULL, NULL);
    		FD_SET(sd, &fds);
    
    		if (result > 0) 
    		{
    
    	 		if (FD_ISSET(sd, &fds)) 
    			{
    
    				alen = sizeof(cad);
    				if ( (sd2=accept(sd, (struct sockaddr *)&cad, &alen)) < 0) {
    					fprintf(stderr, "accept failed\n");
    					exit(1);
    				}
    
    				//make child process
    				printf("forking now\n");
    				pid = fork();
    
    				if (pid < 0) {
    				        perror("fork");
    				        exit(1);
      				}
    
         				if (pid == 0)   //child process
    				{					
    
    					printf("this is the child\n");
    					/* The sdhas data available to be read */
    					close(sd);
    					n = recv(sd2, buf, sizeof(buf), 0);
    					
    					printf("received size: %d\n", n);
    					printf("%s\n", buf);
    					close(sd2);
    				}
        				else		//parent process
    				{
    					printf("This is the parents\n");
    					close(sd2);
    				}
             				
       			}
    			else
    			{
    				printf("WTF");
    				
    			}
    
    		}
    		else if (result < 0) {
    			/* An error ocurred, just print it to stdout */
    			perror("select");
    		}
    		else if (result == 0){
    
    			printf("select is resulting 0\n");
    		}		
    
    
    
    
    
    	}

    I got the server receive something from the client successfully, but I can open receive the msg one time and then it keep printing me bad file descriptor. I tried removed the while loop and it works perfectly fine( print one time). Is there something wrong I need to watch out for as well?

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    8
    problem solved.

    it was a silly mistake...forgot to check the EINTR.

    thank you for all the help

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by saipkjai View Post
    problem solved.

    it was a silly mistake...forgot to check the EINTR.

    thank you for all the help
    Eh? Why were you receiving a signal?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-05-2009, 11:05 PM
  2. problem in select() in a Multiprotocol client
    By zealot in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-09-2009, 12:45 PM
  3. A fifo file and select() problem
    By itsdafoetime in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 02:23 AM
  4. Problem with the socket
    By shiju in forum C Programming
    Replies: 3
    Last Post: 10-22-2004, 04:29 AM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM