Thread: please help with select on linux sockets

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    79

    please help with select on linux sockets

    The code compiles and runs ok, but when i connect to it using netcat nothing happens, well i think netcat connects because it starts running through the for loop.

    However when i type something in netcat to send it does not appear on the screen
    I added in error checking, all it does is repeadly prints to the screen

    select running ok.......
    crap
    crap
    crap
    select running ok......
    crap
    crap
    crap

    etc

    So FD_ISSET(count,&read_fds)

    must not be working properly as its ment to return a non zero when it matches them.

    Code:
    FD_SET(sock,&master);
    	fdmax = sock;
    
    	//infinate loop to run though connections
    	int count;
    	int j;
    	for(;;){
    
    	read_fds = master;
    	if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1){
    
    		Die("Error: Select Malfunctions");
    		
    
    	}
    	printf("select running ok...\n");
    	//select is ok - read through fd's looking for data
    	
    	for(count = 0; count < fdmax; count++){
    
    		if(FD_ISSET(count,&read_fds) != 0){
    		
    			//something set read it
    			printf("Something is set\n");
    			if(count == sock)
    			{
    				//new connection
    				int client_length = sizeof(client_address);
    				if(connected_socket = accept(sock,(struct sockaddr *) &client_address, &client_length)== -1){
    				Die("Error Couldn't accept connection\n");
    			;
    
    				}
    				printf("Connection Accepted\n");
    
    				//added new connection ad to fd
    				FD_SET(connected_socket,&master);
    				if(connected_socket > fdmax){
    
    					fdmax = connected_socket;
    				}
    
    				printf("\n\nNew connection From %s on socket %d",inet_ntoa(client_address.sin_addr), connected_socket);
    			
    			}
    
    			
    				
    
    			
    
    		 }//if statement checking for fd isset
    
    	         else{//nothing
    			printf("crap\n");
    			}
    		
    
    	}//for loop for fd_sets
    
    
    	}//eternal for loop

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I can pretty much promise everything in the API works properly, since most of the internet depends on linux sockets.

    However, I'll concurr it is not always easy to get it to work. I'm not sure exactly what your problem is, but I do see a potential design flaw.

    Code:
    fdmax = sock;
    I presume that is the server socket, since it's what you use with accept().
    Code:
    	for(count = 0; count < fdmax; count++){
    Why do this? fds 0-2 are stdin, stdout, and stderr. You should not be polling them, or polling fd's willy-nilly. You poll only the fd's which are actually part of one of your sets. [edit: actually I guess you aren't polling them...anyway]

    Now, you do advance fdmax to include new sockets:
    Code:
    fdmax = connected_socket;
    Chances are you could just count from sock up to fdmax, but I still think you need to maintain an array of open descriptors (possibly you could examine how the fd_set bit array works and get it from there). Your method of setting this number is also very sloppy:
    Code:
    if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1){
    That should be the actual number of readable sockets, not the number of the highest one+1. There is only one socket in read_fds initially, but you are telling select there are at least 4.

    Of course, that maybe does not explain why the code isn't working; sloppiness is not complete condemnation

    You could try a little more debugging:

    Code:
    fprintf(stderr,"read_fds: %ld\n",read_fds);
    for(count = 0; count < fdmax; count++){
    		if(FD_ISSET(count,&read_fds) != 0){
                            fprintf(stderr,"\t-->%d\n",count);
    Last edited by MK27; 04-15-2010 at 08:17 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if(connected_socket = accept(sock,(struct sockaddr *) &client_address, &client_length)== -1)
    Watch your precedence rules.

    You've written
    if ( a = (b == c) )

    You want
    if ( (a = b) == c )
    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.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    In the statement below
    Code:
    if(FD_ISSET(count, &read_fds) != 0)   /* sock is a file descriptor, not count */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with sockets under linux
    By principii in forum Linux Programming
    Replies: 7
    Last Post: 10-20-2010, 02:31 AM
  2. linux sockets problem gethostbyname
    By mushy in forum C Programming
    Replies: 9
    Last Post: 04-14-2010, 04:49 PM
  3. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  4. Linux for Windows!
    By Strut in forum Linux Programming
    Replies: 2
    Last Post: 12-25-2002, 11:36 AM
  5. Need help with Linux sockets
    By junbin in forum Linux Programming
    Replies: 1
    Last Post: 07-21-2002, 12:42 PM