Internet Socket - Read/Write sync with Select()

This is a discussion on Internet Socket - Read/Write sync with Select() within the Networking/Device Communication forums, part of the General Programming Boards category; If I have a function that's main purpose is to write(); to a client that expects a read(); in a ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Exclamation Internet Socket - Read/Write sync with Select()

    If I have a function that's main purpose is to write(); to a client that expects a read(); in a function, do I want my select(); to use the read or write argument of the function call?

    Example:

    Code:
    int displayMenu(int clientSock){
    
                      fd_set writefds;
              fd_zero(&writefds)
              fd_set(clientsock,&writefds);
    
            if(select(clientsock+1,NULL,&writeds,NULL,NULL)){
               for(int i =0;i<course_amount;i++)
             {
                 write(clientsock,courses[i].coursecode);
    fflush(stdout);
            }
        return 0;
    }
    What this will do is write to the client once and then hang (which is obviously something wrong in the setup I have with select).

    What step/idea am I missing out on when I need to use select();

    The hang is fixed if I cheat and use
    Code:
    sleep(1);
    after the write();
    Last edited by INFERNO2K; 07-18-2007 at 07:44 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    You need to call select before each write.
    And pay attention to the return result of write, in case it didn't send all the data in a single call.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    Code:
    int send_Courses_Menu(int clientSocket){
    
    	fd_set writefds;
    	int i, retval, bytes_written;
    	
    	FD_CLR(clientSocket,&writefds);
    	FD_ZERO(&writefds);
    	FD_SET(clientSocket,&writefds);
      	
    	  	//if ((retval >= 1) && (FD_ISSET(clientSocket, &writefds))) {
    	
    	 while(bytes_written>0){
    	  for(i=1;i<=__COURSE__AMOUNT__;i++){	
    	  		select(clientSocket+1, NULL,&writefds, NULL, NULL);		
    	  		bytes_written = write(clientSocket,courses[i].courseCode,strlen(courses[i].courseCode)+1);
    	  }	
    	}
    	
    	return 0;	
    }
    Function looks something like this and it still is'nt working properly

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    why your array starts at 1?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    I was thinking you should be calling select() with a timeout. As written, it does nothing useful at all.

    If select() times out, you go away and analyse the input say, or redraw some part of the screen - whatever.
    If select() returns an available fd, then you perform ONE I/O operation on that fd.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    I'm still getting the read "hang" on the client. I am not sure what I am doing wrong.

    Server function

    Code:
    int send_Courses_Menu(int clientSocket){
    
    	int i, retval;
    	fd_set writefds;
    	
    	FD_ZERO(&writefds);
    	FD_SET(clientSocket,&writefds);
    
    	for(i=1;i<=__COURSE__AMOUNT__;i++){		
    		retval = select(clientSocket+1,NULL,&writefds,NULL,NULL);
    		if(retval){
    			write(clientSocket,courses[i].courseCode,strlen(courses[i].courseCode));
    		}
    	}
    	
    	return 0;	
    }
    client function

    Code:
    int fetch_Info(int sockfd){
    
    	int i, bytesRead;
    	char message[BUFSIZ + 1];
    
    	system("clear");
    	printf("Select a course to rate: \n\n");
    
    	
    	for(i=1;i<=__COURSE__AMOUNT__;i++){
    		if((bytesRead = read(sockfd,message,BUFSIZ))>0){
    		 	message[bytesRead] = '\0';
    		 	sscanf(message,"&#37;s",courses[i].courseCode);	
    		 	courses[i].courseNumber = i;
    			printf("%d. %s\n",i, message);
    		 }
    	}	
    	
    	return 0;
    }
    Appreciate any help
    Last edited by INFERNO2K; 07-19-2007 at 03:35 PM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > I'm still getting the read "hang" on the client. I am not sure what I am doing wrong.
    You're not reading the manual page for select() to find out how to specify a timeout in the last parameter.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    Quote Originally Posted by Salem View Post
    > I'm still getting the read "hang" on the client. I am not sure what I am doing wrong.
    You're not reading the manual page for select() to find out how to specify a timeout in the last parameter.
    Same result using a struct timeval

    Code:
    int send_Courses_Menu(int clientSocket){
    
    	int i, retval;
    	fd_set writefds;
    	struct timeval to;
    	
    	to.tv_sec = 3;
    	to.tv_usec = 0;
    	
    	FD_ZERO(&writefds);
    	FD_SET(clientSocket,&writefds);
    
    	for(i=1;i<=__COURSE__AMOUNT__;i++){		
    		retval = select(clientSocket+1,NULL,&writefds,NULL,&to);
    		if(retval){
    			write(clientSocket,courses[1].courseCode,strlen(courses[1].courseCode));
    		}
    	}
    	
    	return 0;	
    }

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Select problem
    By saipkjai in forum Networking/Device Communication
    Replies: 4
    Last Post: 02-08-2008, 09:57 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. read/write through udp socket
    By xErath in forum Networking/Device Communication
    Replies: 3
    Last Post: 05-22-2005, 05:43 PM
  4. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

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