CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    Sep 2007
    Posts
    55

    Quick Question about Reading from HTML Page

    Ok So our Professor gave us an assignment for a program, I"VE never worked with C before. i've done c++,java,html and some PERL

    anyways
    basically we repeat a webpage like this:
    http://cor.cs.uky.edu:8800/CS316post.html

    (I have the actual webpage done no prob) it's working like it should. (it's basically a copy)
    anyways this is just a beginning web programming class so it's nothing fancy.

    ANYWAYS
    we basically are given some of like a C server....code, it's supposed to be kinda like an apache code (btw im assuming this is C#, he said it's C, arent C# and C the same thing? if not i apologize)

    but lets say I put some stuff in the text field and select radio buttons, basically my webpage I send back once I hit submit should say this:

    file+directory=%7Epaulp%2FHTML&option=number&data=HELLOTHAR&hiddenValue=0

    im basically reprinting the tokens back? pretty easy right? well i dont know C at ALL
    but I did get this much:
    but see it needs to be able to do this both in POST and GET formats, I figure POST is the easiest.....since it just shows what is selected, altho im unsure if what i have is a POST or a GET method......
    im so confused.

    Code:
    int main(int argc, char *argv[])
    {
    	FILE *file_ptr;
    	char portnum[10];
    	char readData[10];
    	const char PARMFILE[] = "parm.txt";
    	int scannum;
    	int foundport = 0;
    	// Signal SigCatcher to eliminate zombies
    
    	signal(SIGCHLD,SigCatcher);
        file_ptr = fopen(PARMFILE, "r");   // open file for reading
    	if (file_ptr != NULL) {		
    		// use fscanf to read line and get tokens from line
    		scannum = fscanf(file_ptr,"%s",readData);
       		while(scannum > 0 && foundport == 0)  {
    		   // First token is port number
    		   strcpy(portnum,readData);	
    		   // ONLY reads one token :( i need to read all
    
    		   foundport = 1;
    		   // For program 1, need to keep reading multiple lines....
    		   scannum = fscanf(file_ptr,"%s",readData);
    		}
    	} else {
    			error("Cannot open parameter file\n");
    	}
    thats the basic reading line code that i was able to get.
    parm.txt is like a text file with port numbers and such (i dunno much about ports and such....im afraid)
    the rest is stuff HE provided, but anyways basically I need to read all tokens and print them back. also when i try to compile and run this file i get a "error on binding" yet it compiles fine gcc -o (using unix compiler) so i dunno *** is with that. we use a multilab type of thing on putty to do all this and run the server.
    cor.cs.uky.edu is the schools server I do know that much.

    sorry if I sound confusing but basically I have to edit server.c (the code I just gave) to print out what his did (the tokens of what was put in the cs316.html radio and text buttons)
    I got it to print out 1 (i think) but I need it to print out all of them.

    also I have the error cases if file doesn't open but no need for those in here.


    any idea's? thanks (TOTAL new to C so any idea's will help)





    Here is the rest of his code.....dunno if it helps tho as it's mostly just opening ports and such (i think)
    Code:
             // When request received fork a new thread to handle it
             pid = fork();
             if (pid < 0)
                 error("ERROR on fork\n");
             if (pid == 0)  {  // request received
                 close(sockfd);
    			 // Create thread and socket to handle 
                 httpthread(newsockfd,cli_addr);
                 exit(0);
             }
             else close(newsockfd);
         } /* end of while */
         return 0; /* we never get here */
    }
    
    //  httpthread
    // There is a separate instance of this function 
    // for each connection.  It handles all communication
    // once a connnection has been established.
    // If the server is canceled while waiting for a client
    // request, a "zombie" may result. See the link in the prolog.
    // We will ignore this in this simple program.
    void httpthread (int sock,  // socket to use
    struct sockaddr_in cli_addr) // client address
    
    {
       int n;
       char space[] = " ";
       char buffer[10000];  // buffer for all data from client
       bzero(buffer,10000);
       char buffer1[10000]; // buffer for each read of client data
                            // buffer1 is concatentated to buffer 
    						// on each read
       bzero(buffer1,10000);
     
       int count = 1;
       fd_set read_flags,write_flags; 
        FD_ZERO(&read_flags); // Zero the flags 
        FD_ZERO(&write_flags); 
     	FD_SET(sock, &read_flags);
    	struct timeval waitd; 
    	waitd.tv_sec = 1;     // Make select wait up to 1 second for data
        waitd.tv_usec = 0;    // and 0 milliseconds.
    	// read data from client 
    	while (count > 0) {  // select command sets count
    		n = read(sock,buffer1,9999);
    		if (n <= 0){
    		 error("ERROR reading from socket in httpthread");
    		 } else {
    			strcat(buffer,buffer1); // concatenate it to previous data
     			bzero(buffer1,1000);
    		 }
    		// select checks if data is available from client
    		// count is zero when all data is read
    		count=select(sock+1, &read_flags,&write_flags,
                   (fd_set*)0,&waitd);  
    	}
    	// You can use fscanf or strtok to extract data from 
    	// client request:
    	//    request type(GET POST)
    	//	  file name for GET request
    	// This program doesn't care, just echoes back 
    	// what was sent to it
    	handleRequest(sock, buffer);
        // On return, log request
    	// File locking
    	// For a "real" server, a lock must be obtained
    	// on the log file before writing to it 
    	// This program (and program 1) will ignore this
    	FILE *file_ptr;
    	// Open log file for appending
    	file_ptr = fopen("logfile.txt", "a");    
    	time_t timer;
    	timer=time(NULL);  // get time or request
    	// get client IP address and write to file
    	fputs((char*)inet_ntoa(cli_addr.sin_addr), file_ptr);
    	fputs(space, file_ptr);
    	// get time of request and write to file
    	fputs((char*)asctime(localtime(&timer)), file_ptr);
    	fclose(file_ptr);
    	
      
    }
    
    // handleRequest 
    void handleRequest(int sock,  // socket to use
    				  char* buffer) // buffer of client data
    { 
    	int buffersize;	  // buffer size as integer
    	char buffsizechar[5];  // buffer size as chars to send
    	// reponse line:
    	write(sock,HTTP,strlen(HTTP));
    	write(sock,RC200,strlen(RC200));
    	write(sock,RC200MSG,strlen(RC200MSG));
    	// headers:
    	write(sock,CONTENTTYPE,strlen(CONTENTTYPE));
    	write(sock,CONTENTLENGTH,strlen(CONTENTLENGTH));
    	buffersize = strlen(buffer);  // size as int
    	// zero the char size field
    	bzero(buffsizechar,sizeof(buffsizechar)); 
    	sprintf(buffsizechar,"%i",buffersize);  // convert to chars
    	write(sock,buffsizechar,strlen(buffsizechar));
    	write(sock,BLANKLINE,strlen(BLANKLINE)); // end last header line
    	// headers terminated by blank line
    	write(sock,BLANKLINE,strlen(BLANKLINE));
    	// Data received from client echoed back to client
    	write(sock,buffer,strlen(buffer)); 
        return;
    Last edited by MercenaryFH; September 15th, 2008 at 01:55 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured