Thread: Server code Errors that I do not understand

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    Server code Errors that I do not understand

    I've been writing this code for a simple server and am having problems. It says there is something wrong with the way I declared my for loops. I looked at some code I had written previously, and the only difference was the variable I placed in the comparison. here is the portion of the code with the for loops.
    Code:
    #include <stdio.h>                                                
    #include <unistd.h>                                              
    #include <stdlib.h>                                               
    #include <errno.h>                                                
    #include <sys/types.h>                                            
    #include <sys/socket.h>                                           
    #include <netdb.h>                                                
    #include <string.h>    
    #include <fcntl.h>  
    #include <sys/stat.h>
    #include <time.h>                                         
    #define BUFFERSIZE 2048
    
    struct connectInfo{
    	int  connection;
    	char* root;
    };
    void handleConnection(void *v) {
      struct connectInfo ci = *((struct connectInfo*)v);
      int connection=ci.connection;
      char* root=ci.root;
      free(v);
      struct stat statbuf;
      printf("Connection open, using file descriptor %d\n", connection);
    
      // Create a file using the file descriptor
      FILE *incomingConnection = fdopen(connection, "r");
      FILE *outgoingConnection = fdopen(connection, "w");
      if (incomingConnection == NULL || outgoingConnection == NULL) {
        fprintf(stderr, "Error opening file\n");
        exit(1);
      }
      
      setlinebuf(outgoingConnection);
      char buffer[BUFFERSIZE];
    while (fgets(buffer, BUFFERSIZE, incomingConnection) != NULL) {
    	char* end=fgets(buffer, BUFFERSIZE, incomingConnection);
    	int fd=open(end, O_RDONLY, 0);
        	time_t timer=time(NULL);
    	if (errno==0){
    		size_t length= (intmax_t)statbuf.st_size;
    		printf("HTTP/1.1 200 OK \n Date:%s \n Server:myServer version 1.1 \n Last-Modified: %s \n Content-Length: %d \n Connection: close\nContent-Type:text/html\n\n",asctime(localtime(&timer)), (intmax_t) statbuf.st_mtime, (intmax_t)statbuf.st_size);
    		buffer[0]='\0';
    		size_t bytes_printed=0;
    		while (bytes_printed!=length){
    			read(fd, buffer, BUFFERSIZE);
    			for (int i=0; i<BUFFERSIZE; i++){
    				printf("%c", buffer[i]);
    			}
    	
    		}
    		close(fd);
    		if errno != 0{
    			printf("An error has occurred while closing %s \n", errno)
    		}
    	}
    	else{
    		chdir("/home/student/m/marescal/Desktop");
    		int fd=open("page_404", O_RDONLY, 0);
    		printf("HTTP /1.1 404 Not Found\nDate: %s \nServer: myServer version 1.1\n Content-Length: %d\nConnection:clos\nContent-Type: text/html\n\n",asctime(localtime(&timer)),  (intmax_t)statbuf.st_size);
    		read(fd, buffer, BUFFERSIZE);
    		for(int i=0; i<BUFFERSIZE; i++){
    			if (buffer[i])!=='\0'{
    				printf("%c", buffer(i));
    			}
    	}
    			close(fd);
    								
      }
    //... more code
    Last edited by cloudsword; 12-04-2009 at 01:58 AM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    How about copying and pasting your actual errors, rather than making US do all the work?!?!?!

    This:
    Code:
    if (buffer[i])!=='\0'{
    is a syntax error.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    myServer.c:49: error: ‘for’ loop initial declaration used outside C99 mode
    myServer.c:55: error: expected expression before ‘!=’ token
    myServer.c:64: error: ‘for’ loop initial declaration used outside C99 mode
    myServer.c:65: error: expected expression before ‘!=’ token

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    thanks rags
    I corrected some of my code, but I am still getting the for loop errors

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cloudsword View Post
    thanks rags
    I corrected some of my code, but I am still getting the for loop errors
    Because you are declaring an int inside the condition:
    Code:
    for(int i=0; i<BUFFERSIZE; i++)
    which is more or less pointless, but it is allowed under c99.

    Declare i with everything else then just use:
    Code:
    for(i=0; i<BUFFERSIZE; i++)
    Remember, you can reuse i in both loops.
    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

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  3. Need software to help to understand C source code
    By Kincider in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2006, 09:44 PM
  4. I want to understand this code
    By BadProgrammer in forum C++ Programming
    Replies: 9
    Last Post: 12-18-2003, 02:39 PM
  5. Weird errors in code
    By sirSolarius in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2003, 09:55 AM

Tags for this Thread