Like Tree8Likes
  • 1 Post By Click_here
  • 6 Post By ledow
  • 1 Post By Annonymous

select server help

This is a discussion on select server help within the C Programming forums, part of the General Programming Boards category; So i wrote a server that incorporates the select function but it is not working properly. I know that I ...

  1. #1
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302

    select server help

    So i wrote a server that incorporates the select function but it is not working properly. I know that I am missing something vital but what? I am trying to make it echo the message from the client back to the client.

    This is the output of the client ->
    Code:
    blah@blah:~/Documents/nix-chat-ui$ ./client 127.0.0.1 12345
    Made a connection to 127.0.0.1
    Message: hey
    server: hey
    Message: hey
    server: hey
    Message: hey
    blah@blah:~/Documents/nix-chat-ui$
    The client closes after 3 calls.




    The output from the asynch server
    Code:
    blah@blah:~/Documents/nix-chat-ui$  ./select 127.0.0.1 12345
    Listening for incoming connections.
    
    
    Select() was successful.
    ./select: New connection from 127.0.0.1 on socket 4
    
    
    Select() was successful.
    Which you can see hangs.


    The server code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    int main(int argc, char *argv[]) {
    fd_set master;
    // the temorary fd list for select call
    fd_set read_fds;
    
    struct sockaddr_in serv_addr, cli_addr;
    struct hostent *server;
    int fdmax;
    int i, j;
    FD_ZERO(&master);
    FD_ZERO(&read_fds);
    
    	if((argc < 3) && (argc > 4)) {
    	printf("USAGE: %s + IP Address + Port No.\n", argv[0]);
    	exit(EXIT_FAILURE);
    	}
    
    int debug;
    	if(argc == 4) {
    	debug = atoi(argv[3]);
    	}
    
    /* get the listener */
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    	if(sockfd < 0) {
    	printf("SOCKET(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    		else if((sockfd) && (debug == 1)) {
    		printf("Successfully created socket.\n");
    		}
    int yes = 1;
    int sockopt = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
    	if(sockopt == -1) {
    	printf("SETSOCKOPT(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    		else if((sockopt) && (debug == 1)){
    		printf("Setsockopt() was successful.\n");
    		}
    
    server = gethostbyname(argv[1]); 
    	if(server == NULL) {
    	printf("GETHOSTBYNAME(NULL) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    		else if((server) && (debug == 1)) {
    		printf("Successfuly got host by name.\n");
    		}
    
    int portno = atoi(argv[2]);
    	if(portno < 0) {
    	printf("ATOI(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	if((portno == 0) && (debug == 1)) {
    	printf("ATOI(0) - DEBUG MSG --> %s.\n", strerror(errno));
    	}
    
    	if((portno) && (debug == 1)) {
    	printf("Successfully binded to port %d.\n", portno);
    	}
    
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    //memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
    serv_addr.sin_port = htons(portno);
    memset(&(serv_addr.sin_zero), '\0', 8);
    
    int binder = bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
    	if(binder < 0) {
    	printf("BIND(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	if((binder == 0) && (debug == 1)) {
    	printf("BIND(0) - DEBUG MSG --> %s.\n", strerror(errno));
    	}
    	
    	if((binder) && (debug == 1)) {
    	printf("Bind was successful.\n");
    	}
    
    int listener = listen(sockfd, 10);
    	if(listener < 0) {
    	printf("Listen(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	if((listener == 0) && (debug == 1)) {
    	printf("LISTEN(0) - DEBUG MSG --> %s.\n", strerror(errno));
    	}
    		else {
    		printf("Listening for incoming connections.\n");
    		 if((listener) && (debug == 1)) {
    		 printf("Entering main loop.\n");
    		 }
    		}
     
    FD_SET(sockfd, &master);
    //Watch the current FD.
    fdmax = sockfd; 
    
    for( ; ; ) {
    read_fds = master;
    char buffer[4096];
    
    int selector = select(fdmax+1, &read_fds, NULL, NULL, NULL);
    	if(selector < 0) {
    	printf("SELECT(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    		else {
    		printf("\n\nSelect() was successful.\n");
    		}
    
    // Run through the existing connections looking for data to be read
    for(i = 0; i <= fdmax; i++) {
    	if(FD_ISSET(i, &read_fds)) { 
    
    	if(i == sockfd) {
    	 if(debug == 1) {
    	 printf("i == sockfd DEBUG MSG --> %s.\n", strerror(errno));
    	 }
    
    	socklen_t clilen;
    	clilen = sizeof(cli_addr);
    
    	int newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
    	if(newsockfd < 0) {
    	printf("Accept(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	if((newsockfd == 0) && (debug == 1)) {
    	printf("ACCEPT(0) - DEBUG MSG --> %s.\n", strerror(errno));
    	}
    
    		else {
    		 if(debug == 1) {
    		 printf("Accept(1) was successful.\n");
    		 }
    
    		 // Add new FD to masterset.
    		 FD_SET(newsockfd, &master); 
    		 
    		 if(debug == 1) {
    		 printf("newsockfd == %d\n", newsockfd);
    		 printf("fdmax == %d\n", fdmax);
    		 }
    
    		 if(newsockfd > fdmax) { 
    		  if(debug == 1) {
    		  printf("newsockfd > fdmax.\n");
    		  printf("fdmax == newsockfd.\n");
    		  }
    
    		 fdmax = newsockfd;
    		 } 
    
    		 if((newsockfd < fdmax) && (debug == 1)) {
    		 printf("newsockfd < fdmax.\n");
    		 }
    
    		 if((newsockfd == fdmax) && (debug == 1)) {
    		 printf("newsockfd == fdmax.\n");
    		 }
    
    		printf("%s: New connection from %s on socket %d\n", argv[0], inet_ntoa(cli_addr.sin_addr), newsockfd);
    		}
    	}
    
    		else { // Start of read loop
    		ssize_t bytes_read = recv(i, buffer, sizeof(buffer), 0);
    		 if(bytes_read <= 0) {
    
    		 if(bytes_read < 0) {
    		 printf("RECV(-1) error --> %s.\n", strerror(errno));
    		 exit(EXIT_FAILURE);
    		 }
    
    		 if(bytes_read == 0) {
    		 // Connection closed by remote host
    		 printf("%s: socket %d hung up\n", argv[0], i);
    		 }
    
    		 FD_CLR(i, &master);
    		 close(i);
    		}
    
    		else { // Start of inner read loop if true
    		 if((bytes_read) && (debug == 1)) {
    		 printf("Recieve was successful.\n");
    		 }
    
    		 FD_CLR(i, &master);
    		 close(i);
    
    		for(j = 0; j <= fdmax; j++) { // Start of inner for loop to send data
    		// Echo message to all
    		if(FD_ISSET(j, &read_fds)) {
    
    		 if(debug == 1) {
    		 printf("j is in the masterset.\n");
    		 }
    
    		if((j == sockfd) && (debug == 1)) {
    		printf("j == sockfd DEBUG MSG --> %s.\n", strerror(errno));
    		}
    
    		if((j != sockfd) && (debug == 1)) {
    		printf("j != sockfd DEBUG MSG --> %s.\n", strerror(errno));
    		}
    
    		if((j == i) && (debug == 1)) {
    		printf("j == i DEBUG MSG --> %s.\n", strerror(errno));
    		}
    
    		if((j != i) && (debug == 1)) {
    		printf("j != i DEBUG MSG --> %s.\n", strerror(errno));
    		}
    
      		// Except the listener and ourselves 
    		  if((j != sockfd) && (j != i)) {
    		   if(debug == 1) {
    		   printf("j != sockfd && j != i");
    		   }
    
      	  	  ssize_t bytes_written = send(j, buffer, bytes_read, 0);
    		  if(bytes_written < 0) {
      	  	  printf("WRITE(-1) error --> %s.\n", strerror(errno));
    		  }
    
    		  if((bytes_written == 0) && (debug == 1)) {
    		  printf("WRITE(0) - DEBUG MSG --> %s.\n", strerror(errno));
    		  }
    
    		  if((bytes_written) && (debug == 1)) {
    		  printf("WRITE(1) was successful.\n");
    		  }
    
      		 }
    		}
    		} // End of inner for loop to send data
    		} // Inner read if read > 0
    	} // End of read loop
    	}
    	}
    }
    
    return 0;
    }
    I am sure the client is good. I have written many of them and they work with my regular synchrous servers but I'll post the client code anyway.

    The client
    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <stdlib.h> 
    #include <unistd.h> 
    #include <sys/socket.h> 
    #include <sys/types.h> 
    #include <netdb.h> 
    #include <netinet/in.h> 
    #include <arpa/inet.h>
    #include <errno.h>
    
    int main(int argc, char *argv[]) {  
    struct hostent *server; 
    struct sockaddr_in serv_addr/*, cli_addr*/;
    
    	if((argc < 3) && (argc > 4)) {
    	fprintf(stderr, "Ussage: %s + IP Address + port No. Append a 1 to turn verbose on.\n", argv[0]);
    	exit(EXIT_FAILURE);
    	}
    
    int debug;
    	if(argc == 4) {
    	debug = atoi(argv[3]);
    	}
    
    
    int sockfd = socket(AF_INET, SOCK_STREAM, 0); 
    	if(sockfd < 0) {
    	printf("SOCKET(-1) error ---> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	if((sockfd == 0) && (debug == 1)) {
    	printf("SOCKET(0) error ---> %s.\n", strerror(errno));
    	}
    
    	if((sockfd) && (debug == 1)) {
    	printf("DEBUG MSG --> SOCKET was successful.\n");
    	}
    
    int yes = 1; 
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
    
    bzero((char *) &serv_addr, sizeof(serv_addr)); 
    server = gethostbyname(argv[1]); 
    	if(server == NULL) {
    	fprintf(stderr, "No such host.\n");
    	printf("%s\n", strerror(errno)); 
    	exit(EXIT_FAILURE);
    	}
    
    		else if((server) && (debug == 1)) {
    		printf("DEBUG MSG - Successfully got host by name.\n");
    		}
    
    int portno = atoi(argv[2]);
    	if(portno < 0) {
    	printf("PORTNO(0) error ---> %s.\n", strerror(errno));
    	}
    
    	if((portno == 0) && (debug == 1)) {
    	printf("DEBUG MSG - ATOI(0) error ---> %s.\n", strerror(errno));
    	}
    
    	if((portno) && (debug == 1)) {
    	printf("Successfully binded to port %d.\n", portno);
    	}
    
    serv_addr.sin_family = AF_INET;
    memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length); 
    serv_addr.sin_port = htons(portno);
    
    int connector = connect(sockfd, (const struct sockaddr *)&serv_addr, sizeof(serv_addr));
    	if(connector < 0) { 
    	fprintf(stderr, "%s. CONNECT()\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    		else {
    		printf("Made a connection to %s\n", inet_ntoa(serv_addr.sin_addr)); 
    		}
    
    for( ; ; )
    {
    char buffer[4096];
    
    printf("Message: ");
    fgets(buffer, sizeof(buffer), stdin);
    ssize_t bytes_written = write(sockfd, buffer, strlen(buffer));
    	if(bytes_written < 0) { 
    	printf("WRITE(-1) error ---> %s.\n", strerror(errno));
    	}
    
    	if(bytes_written == 0) {
    	//printf("WRITE(0) error ---> %s.\n", strerror(errno));
    	printf("Nothing was written.\n");
    	}
    
    	if((bytes_written) && (debug == 1)) { 
    	printf("DEBUG MSG - WRITE was successful.\n");
    	}
    
    ssize_t bytes_read = read(sockfd, buffer, sizeof(buffer));
    	if(bytes_read < 0) {
            //fprintf(stderr, "Error reading message from %s\n", inet_ntoa(cli_addr.sin_addr));
            printf("READ(-1) error ---> %s.\n", strerror(errno));
            exit(EXIT_FAILURE);
            }
    
    	//Test to see if the buffer is blank.
            if((bytes_read == 0) && (debug == 1)) {
            printf("READ(0) error ---> %s.\n", strerror(errno));
            }
    		else {
    		printf("server: %s", buffer);
    		}
    
    }
    
    close(sockfd);
    
    return 0;
    }
    I am missing some parameters to the read/recv calls or do I need to enable non-blocking mode??
    Last edited by Annonymous; 10-14-2012 at 11:58 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    Code:
        if((argc < 3) && (argc > 4)) {
    This should be "or" - argc can't be below 3 and above 4 at the same time.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    Seriously, you've been here well over a year, have nearly 300 posts, and your code is still an eyesore
    SourceForge.net: Indentation - cpwiki

    Not to mention "let's do everything in main because I can't be bothered to write a function so the code goes on and on and on...."

    Refactor it, and indent it, then people might spend some time on reading it.
    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.

  4. #4
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Server(moddified indentation).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    int main(int argc, char *argv[]) {
    fd_set master;
    // the temorary fd list for select call
    fd_set read_fds;
    
    struct sockaddr_in serv_addr, cli_addr;
    struct hostent *server;
    
    int fdmax, i, j;
    
    FD_ZERO(&master);
    FD_ZERO(&read_fds);
    
    	if((argc < 3) || (argc > 4)) {
    	printf("USAGE: %s + IP Address + Port No.\n", argv[0]);
    	exit(EXIT_FAILURE);
    	}
    
            int debug;
    	if(argc == 4) {
    	debug = atoi(argv[3]);
    	}
    
            int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    	if(sockfd < 0) {
    	printf("SOCKET(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    		else if((sockfd) && (debug == 1)) {
    		printf("Successfully created socket.\n");
    		}
    
            int yes = 1;
            int sockopt = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
    	if(sockopt == -1) {
    	printf("SETSOCKOPT(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    		else if((sockopt) && (debug == 1)){
    		printf("Setsockopt() was successful.\n");
    		}
    
            server = gethostbyname(argv[1]); 
    	if(server == NULL) {
    	printf("GETHOSTBYNAME(NULL) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    		else if((server) && (debug == 1)) {
    		printf("Successfuly got host by name.\n");
    		}
    
            int portno = atoi(argv[2]);
    	if(portno < 0) {
    	printf("ATOI(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	if((portno == 0) && (debug == 1)) {
    	printf("ATOI(0) - DEBUG MSG --> %s.\n", strerror(errno));
    	}
    
    	if((portno) && (debug == 1)) {
    	printf("Successfully binded to port %d.\n", portno);
    	}
    
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    //memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
    serv_addr.sin_port = htons(portno);
    memset(&(serv_addr.sin_zero), '\0', 8);
    
            int binder = bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
    	if(binder < 0) {
    	printf("BIND(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	if((binder == 0) && (debug == 1)) {
    	printf("BIND(0) - DEBUG MSG --> %s.\n", strerror(errno));
    	}
    
    	if((binder) && (debug == 1)) {
    	printf("Bind was successful.\n");
    	}
    
            int listener = listen(sockfd, 10);
    	if(listener < 0) {
    	printf("Listen(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	if((listener == 0) && (debug == 1)) {
    	printf("LISTEN(0) - DEBUG MSG --> %s.\n", strerror(errno));
    	}
    		else {
    		  printf("Listening for incoming connections.\n");
    		  if((listener) && (debug == 1)) {
    		  printf("Entering main loop.\n");
    		  }
    		}
    
    FD_SET(sockfd, &master);
    //Watch the current FD.
    fdmax = sockfd; 
    
    for( ; ; ) {
    read_fds = master;
    char buffer[4096];
    
            int selector = select(fdmax+1, &read_fds, NULL, NULL, NULL);
    	if(selector < 0) {
    	printf("SELECT(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    		else {
    		printf("\n\nSelect() was successful.\n");
    		}
    
    // Run through the existing connections looking for data to be read
    for(i = 0; i <= fdmax; i++) {
    	if(FD_ISSET(i, &read_fds)) { 
    	
    	if(i == sockfd) {
    	if(debug == 1) {
    	printf("i == sockfd DEBUG MSG --> %s.\n", strerror(errno));
    	}
    
    	socklen_t clilen;
    	clilen = sizeof(cli_addr);
    
    	int newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
    	if(newsockfd < 0) {
    	printf("Accept(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	if((newsockfd == 0) && (debug == 1)) {
    	printf("ACCEPT(0) - DEBUG MSG --> %s.\n", strerror(errno));
    	}
    
    		else {
    		if(debug == 1) {
    		printf("Accept(1) was successful.\n");
    		}
    
    		// Add new FD to masterset.
    		FD_SET(newsockfd, &master); 
    	
    		if(debug == 1) {
    		printf("newsockfd == %d\n", newsockfd);
    		printf("fdmax == %d\n", fdmax);
    		}
    
    		if(newsockfd > fdmax) { 
    		if(debug == 1) {
    		printf("newsockfd > fdmax.\n");
    		printf("fdmax == newsockfd.\n");
    		}
    
    		fdmax = newsockfd;
    		} 
    
    		if((newsockfd < fdmax) && (debug == 1)) {
    		printf("newsockfd < fdmax.\n");
    		}
    
    		if((newsockfd == fdmax) && (debug == 1)) {
    		printf("newsockfd == fdmax.\n");
    		}
    
    		printf("%s: New connection from %s on socket %d\n", argv[0], inet_ntoa(cli_addr.sin_addr), newsockfd);
    		}
    	}
    	
    		else { // Start of read loop
    		ssize_t bytes_read = recv(i, buffer, sizeof(buffer), 0);
    		if(bytes_read <= 0) {
    	
    		if(bytes_read < 0) {
    		printf("RECV(-1) error --> %s.\n", strerror(errno));
    		exit(EXIT_FAILURE);
    		}
    
    		if(bytes_read == 0) {
    		// Connection closed by remote host
    		printf("%s: socket %d hung up\n", argv[0], i);
    		}
    
    		FD_CLR(i, &master);
    		close(i);
    		}
    
    			else { // Start of inner read loop if true
    			if((bytes_read) && (debug == 1)) {
    			printf("Recieve was successful.\n");
    			}
    
    			FD_CLR(i, &master);
    			close(i);
    
    			for(j = 0; j <= fdmax; j++) { // Start of inner for loop to send data
    			// Echo message to all
    			if(FD_ISSET(j, &read_fds)) {
    
    			if(debug == 1) {
    			printf("j is in the masterset.\n");
    			}
    	
    			if((j == sockfd) && (debug == 1)) {
    			printf("j == sockfd DEBUG MSG --> %s.\n", strerror(errno));
    			}
    
    			if((j != sockfd) && (debug == 1)) {
    			printf("j != sockfd DEBUG MSG --> %s.\n", strerror(errno));
    			}
    
    			if((j == i) && (debug == 1)) {
    			printf("j == i DEBUG MSG --> %s.\n", strerror(errno));
    			}
    
    			if((j != i) && (debug == 1)) {
    			printf("j != i DEBUG MSG --> %s.\n", strerror(errno));
    			}
    
    			// Except the listener and ourselves 
    			if((j != sockfd) && (j != i)) {
    			if(debug == 1) {
    			printf("j != sockfd && j != i");
    			}
    
    			ssize_t bytes_written = send(j, buffer, bytes_read, 0);
    			if(bytes_written < 0) {
    			printf("WRITE(-1) error --> %s.\n", strerror(errno));
    			}
    
    			if((bytes_written == 0) && (debug == 1)) {
    			printf("WRITE(0) - DEBUG MSG --> %s.\n", strerror(errno));
    			}
    	
    			if((bytes_written) && (debug == 1)) {
    			printf("WRITE(1) was successful.\n");
    			}
    
    			}
    			}
    			} // End of inner for loop to send data
    			} // Inner read if read > 0
    		} // End of read loop
    	}
    }
    }
    
    return 0;
    }
    Hope that's better.
    Last edited by Annonymous; 10-15-2012 at 01:58 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    Did you click review post before submitting?

    I don't consider five consecutive braces in the same column (line 255 onwards) to be indicative of good indentation.

    It's about half better.

    Remember, do NOT mix spaces and tabs when posting to the forum, you will not see what your editor sees.
    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 Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    No, I didn't click review. So, do you see what I'm doing wrong?

  7. #7
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by Annonymous View Post
    So, do you see what I'm doing wrong?
    Apart from code-dumping and not listening to a forum admin who told you what you need to do before people like me (i.e. not a forum admin) bother to look at the code?

    Dump-and-run is not the way to seek help. If you think I'm being rude, I'm afraid your code-dump and expectation of help when you refuse to learn from previous postings is much more offensive.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  8. #8
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Apart from code-dumping and not listening to a forum admin who told you what you need to do before people like me (i.e. not a forum admin) bother to look at the code?
    Which I did fix up. Its a lot of code considering the heavily nested if/else structure it's built on. If I try to indent it any more I will most likely debeautify the code. You could take a look at my original post if that's what your looking for.

    Dump-and-run is not the way to seek help. If you think I'm being rude, I'm afraid your code-dump and expectation of help when you refuse to learn from previous postings is much more offensive.
    I'm not dumping and running. Although that's what it might seem like. I have no problem doing the work as long as suggestions are made. I think your confusing my intentions with my lack of social skills. Which are horrible. If your not up for helping me, then that's cool. If your skills are not up to par... then that's cool too. Just please don't act like your above me.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    If I try to indent it any more I will most likely debeautify the code
    What you are being asked to do is indent your code every time you use braces.
    i.e.
    Code:
    int main(void)
    {
        ....
        if (banana != delicious)
        {
            if (apple != delicious)
            {
                if (pear != delicious)
                {
                    you = fussy;
                }
            }
        }
    
        return 0;
    }
    Last edited by Click_here; 10-15-2012 at 06:43 PM.
    Matticus likes this.
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    From your revised post:

    Quote Originally Posted by Annonymous View Post
    Code:
                if((bytes_written) && (debug == 1)) {
                printf("WRITE(1) was successful.\n");
                }
    
                }
                }
                } // End of inner for loop to send data
                } // Inner read if read > 0
            } // End of read loop
        }
    }
    }
    
    return 0;
    }
    Also, as Salem originally said:

    Not to mention "let's do everything in main because I can't be bothered to write a function so the code goes on and on and on...."

    Refactor it, and indent it, then people might spend some time on reading it.
    The code you're asking us to look at (and which you've had no other replies to, notice, because of these problems) is horrendously unreadable. Who cares, I write unreadable code too. But the problem is that you're asking for help, and you've been on these forums for a while now, and you're expecting people to take the time to read your code, so you could take the time to format it nicely to save them the hassle of doing it for you. One cleanup for you, is a cleanup for EVERYONE that looks at the code.

    I'd like to see it broken down into functions (open_socket, returning true or false, close_socket, read_string_from_socket or whatever), I'd like not to have five parentheses nested on the same column at the end of your code with no indication of where inner loops / conditionals ended (seriously, it's almost impossible to see without literally tracing back through the code by hand and counting the parentheses as you go). I'd like the whole "if(debug ==1)" junk thrown in the bin if I'm honest, or at least wrapped in a preprocessor conditional (#ifdef DEBUG) so I can just remove it without having to guess where those debug code blocks begin and end as they are sprayed throughout your code.

    I suspect your code would be about 20-30 lines long, of actual content. That's perfectly viable for someone on this forum to skim and say "There's your problem" even if it's not perfect. But 266 lines of badly-nested parentheses are hiding your problem behind a mess of, quite literally, code.

    It's not that I think I'm any better at you - I'm not a "perfect" programmer by a long stretch, see my post history where I often get corrected on some side-issue or use a different method to others that's imperfect. It's that I think I could make a better job of asking for help, by putting more effort into doing that. You're not afraid of the work, that much is clear, but you've not made it easy for anyone to help. What you've done is dumped the whole company accounts on the table of a tax inspector and said "There you go, get on with that". Except we're not tax inspectors. We don't get paid to do it. We have little incentive to help you beyond simple courtesy and wanting to help you learn. But that doesn't extend to making your code readable for you. Like the tax inspector in real life, we'll throw it back at you and say "That's not good enough - prepare a proper document that I can read without having to wade through the entire cruft of your business".

    The fact that people are making multiple posts on your thread means we're checking it to see if it's at the stage where we can help you yet. It's not. And though I *could* throw it into Eclipse, reformat it to my preferred style, move all your debug code out to the preprocessor, move it all into function or at least short code blocks that I can understand run it and THEN debug it for you, chances are I won't while you're not trying to help ME (and the others here) do that.
    Last edited by ledow; 10-16-2012 at 07:33 AM. Reason: fixed quoting (see? We're not all perfect!)

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  11. #11
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    I truely respect what you have just said here! I was going to leave this alone and try and figure it out on my own but after seeing this post, I saw that I was being a stubborn jack**s! I will break it down and remove the unnecessary code and fix the indentation and post the results and hopefully someone can spot what I can't. Thanks Ledow!

  12. #12
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    I took out all the junk and fixed it. I was the slopy write function. We had and still have massive power outage all over the east coast after that hurricane. My part of Jersey just got power back but no cable. I hope that anyone else on this forum/board that's also from the tri-state is OK.

    Here's the working server. Only thing is the server handles connections seperately. So it wont echo one message to all. Now that I have it working that's the next step. I'm putting this up for a basis for other people trying to get a select server working.
    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    int main(int argc, char *argv[]) {
    fd_set master;
    fd_set read_fds;
    
    struct sockaddr_in serv_addr, cli_addr;
    struct hostent *server;
    int fdmax, i;
    FD_ZERO(&master);
    FD_ZERO(&read_fds);
    
    	 if(argc < 3) {
    	 printf("USAGE: %s + IP Address + Port No.\n", argv[0]);
    	 exit(EXIT_FAILURE);
    	 }
    
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    	if(sockfd < 0) {
    	printf("SOCKET(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    int yes = 1;
    int sockopt = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
    	if(sockopt == -1) {
    	printf("SETSOCKOPT(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    server = gethostbyname(argv[1]); 
    	if(server == NULL) {
    	printf("GETHOSTBYNAME(NULL) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    int portno = atoi(argv[2]);
    	if(portno < 0) {
    	printf("ATOI(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	if(portno == 0) {
    	printf("ATOI(0) - DEBUG MSG --> %s.\n", strerror(errno));
    	}
    
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    //memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
    serv_addr.sin_port = htons(portno);
    memset(&(serv_addr.sin_zero), '\0', 8);
    
    //Bind() returns zero upon success.
    int binder = bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
    	if(binder < 0) {
    	printf("BIND(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    int listener = listen(sockfd, 10);
    	if(listener < 0) {
    	printf("Listen(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    		//Listen() return zero upon success.
    		else {
    		printf("Listening for incoming connections.\n");
    		}
    
    FD_SET(sockfd, &master);
    fdmax = sockfd; 
    
    for( ; ; ) {
    read_fds = master;
    char buffer[4096];
    
    int selector = select(fdmax+1, &read_fds, NULL, NULL, NULL);
    	if(selector < 0) {
    	printf("SELECT(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
     for(i = 0; i <= fdmax; i++) {
    	if(FD_ISSET(i, &read_fds)) {
    
    	  if(i == sockfd) {
    	  socklen_t clilen;
    	  clilen = sizeof(cli_addr);
    
    	  int newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
    	    if(newsockfd < 0) {
    	    printf("Accept(-1) error --> %s.\n", strerror(errno));
    	    exit(EXIT_FAILURE);
    	    }
    
    	    if(newsockfd == 0) {
    	    printf("ACCEPT(0) - DEBUG MSG --> %s.\n", strerror(errno));
    	    }
    	        
    	    	else {
    	    	  FD_SET(newsockfd, &master); 
    	    	  if(newsockfd > fdmax) {
    	    	  fdmax = newsockfd;
    	    	  }
    	
    	    	printf("%s: New connection from %s on socket %d\n", argv[0], inet_ntoa(cli_addr.sin_addr), newsockfd);
    	    	}
    	  }
    	
    	  	else {
    	  	ssize_t bytes_read = recv(i, buffer, sizeof(buffer), 0);
    	  	if(bytes_read <= 0) {
    
    	  	  if(bytes_read < 0) {
    	  	  printf("RECV(-1) error --> %s.\n", strerror(errno));
    	  	  exit(EXIT_FAILURE);
    	  	  }
    
    	  	  if(bytes_read == 0) {
    	  	  printf("%s: socket %d hung up\n", argv[0], i);
    	  	  FD_CLR(i, &master);
    	  	  close(i);
    	  	  }
    
    	  	}
    
    	  		else if(bytes_read) {
    	  		printf("Recieve was successful.\n");
    	  		ssize_t bytes_written = write(i, buffer, strlen(buffer));
    	  		  if(bytes_written < 0) {
    	  		  printf("WRITE(-1) error ---> %s.\n", strerror(errno));
    	  		  }
    
    	  		  if(bytes_written == 0) {
    	  		  printf("Nothing was written.\n");
    	  		  }
    
    	  		  if(bytes_written) {
    	  		  printf("Write was successful.\n");
                              memset(buffer, '\0', strlen(buffer));
    	  		  }
    
    	  		}
    
    	  	}
    	}
     }
    }
    
    return 0;
    }
    Last edited by Annonymous; 10-31-2012 at 01:50 PM.
    Click_here likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chat server not using select
    By Per Andersson in forum C Programming
    Replies: 1
    Last Post: 07-06-2012, 09:12 AM
  2. TCP Server with select() function
    By netpumber in forum Windows Programming
    Replies: 6
    Last Post: 10-24-2011, 11:19 AM
  3. Select(); server help!
    By klipseracer in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-02-2008, 11:16 PM
  4. Running two UDP server simultaneously using select()
    By PiJ in forum Networking/Device Communication
    Replies: 15
    Last Post: 01-31-2008, 10:49 AM
  5. select() server
    By chrismiceli in forum Linux Programming
    Replies: 0
    Last Post: 09-09-2003, 09:56 PM

Tags for this Thread


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