Ejemplo n.º 1
0
 // after serving this many pages the server will halt
 int main (int argc, char* argv[]) {
 printf ("************************************\n");
 printf ("Starting simple server %f\n", SIMPLE_SERVER_VERSION);
 printf ("Serving the Mandelbrot set since 2014\n");
 printf ("Access this server at http://localhost:%d/\n", DEFAULT_PORT);
 printf ("************************************\n");
 int serverSocket = makeServerSocket(DEFAULT_PORT);
 char request[REQUEST_BUFFER_SIZE];
 int numberServed = 0;
 while (numberServed < NUMBER_OF_PAGES_TO_SERVE) {
 printf ("*** So far served %d pages ***\n", numberServed);
 // STEP 1. wait for a request to be sent from a web browser,
 // then open a new connection for this conversation
 int connectionSocket = waitForConnection(serverSocket);
 // STEP 2. read the first line of the request
 int bytesRead = recv (connectionSocket, request, sizeof(request) - 1, 0);
 assert (bytesRead >= 0);
 // check that we were able to read some data from the connection
 // echo entire request to the console for debugging
 printf (" *** Received http request ***\n %s\n", request);
 // STEP 3. send the browser a simple html page using http
 printf (" *** Sending http response ***\n");
 serveHTML (connectionSocket);
 // STEP 4. close the connection after sending the page- keep aust beautiful
 close (connectionSocket);
 ++numberServed;
 }
 // close the server connection after we are done- keep aust beautiful
 printf ("** shutting down the server **\n");
 close (serverSocket);
 return EXIT_SUCCESS;
 }
Ejemplo n.º 2
0
int main (int argc, char *argv[]) {
    //used bmpserver.c
    printf ("************************************\n");
    printf ("Starting simple server %f\n", SIMPLE_SERVER_VERSION);
    printf ("Serving bmps since 2012\n");
    
    int serverSocket = makeServerSocket (DEFAULT_PORT);
    printf ("Access this server at http://localhost:%d/\n", DEFAULT_PORT);
    printf ("************************************\n");
    
    char request[REQUEST_BUFFER_SIZE];
    
    int numberServed = 0;
    while (numberServed < NUMBER_OF_PAGES_TO_SERVE) {
        testEscapeSteps();
        int connectionSocket = waitForConnection (serverSocket);
        // wait for a request to be sent from a web browser, open a new
        // connection for this conversation
        
        // read the first line of the request sent by the browser
        int bytesRead;
        bytesRead = read (connectionSocket, request, (sizeof request)-1);
        assert (bytesRead >= 0);
        // were we able to read any data from the connection?
        // using struct to assign and check different variables
        triordinate dat;
       
        //used sscanf since using parsing our function made the program very lond.
        if(sscanf (request,"GET /tile_x%lf_y%lf_z%d.bmp", &dat.x, &dat.y, &dat.z)){
            // print entire request to the console
            printf (" *** Received http request ***\n %s\n", request);
            
            //send the browser specific mandelbrot tile
            printf (" *** Sending http response ***\n");
            serveBMP(connectionSocket, dat);
            
        } else {
            // print entire request to the console
            printf (" *** Received http request ***\n %s\n", request);
            
            //send the browser specific mandelbrot tile
            printf (" *** Sending http response ***\n");
            serveHTML(connectionSocket);
        }
        
        // close the connection after sending the page- keep aust beautiful
        close(connectionSocket);
        
        numberServed++;
        
    }
    
    // close the server connection after we are done- keep aust beautiful
    printf ("** shutting down the server **\n");
    close (serverSocket);
    
    return EXIT_SUCCESS;
}
Ejemplo n.º 3
0
	int main (int argc, char *argv[]) {

	   printf ("************************************\n");
	   printf ("Starting simple server %f\n", SIMPLE_SERVER_VERSION);
	   printf ("Serving bmps since 2012\n");

	   int serverSocket = makeServerSocket (DEFAULT_PORT);
   printf ("Access this server at http://localhost:%d/\n", DEFAULT_PORT);
	   printf ("************************************\n");

	   char request[REQUEST_BUFFER_SIZE];

	   int numberServed = 0;
	   while (numberServed < NUMBER_OF_PAGES_TO_SERVE) {
			double x = 0;
			double y = 0;
			double zoom = 0;
			int flag = 0;
			/*printf("Enter x: \n");
	   	scanf("%lf", &x);
	   	printf("Enter y: \n");
	   	scanf("%lf", &y);
	   	printf("Enter zoom level: \n");
	   	scanf("%lf", &zoom);*/
	      printf ("*** So far served %d pages ***\n", numberServed);

	      int connectionSocket = waitForConnection (serverSocket);
	      // wait for a request to be sent from a web browser, open a new
    // connection for this conversation

      // read the first line of the request sent by the browser
	      int bytesRead;
	      bytesRead = read (connectionSocket, request, (sizeof request)-1);
	      assert (bytesRead >= 0);
	      // were we able to read any data from the connection?
			flag = sscanf(request, "GET /tile_x%lf_y%lf_z%lf.bmp", &x, &y, &zoom);
			printf("%d", flag);
	      // print entire request to the console
	      printf (" *** Received http request ***\n %s\n", request);

	      //send the browser a simple html page using http
	      printf (" *** Sending http response ***\n");
	      if (flag == 3){
				serveBMP(connectionSocket, x, y, zoom);
			}else{
					servePage(connectionSocket);
			}
	      // close the connection after sending the page- keep aust beautiful
	      close(connectionSocket);
			numberServed++;
	   }

	   // close the server connection after we are done- keep aust beautiful
	   printf ("** shutting down the server **\n");
	   close(serverSocket);

	   return EXIT_SUCCESS;
	}
Ejemplo n.º 4
0
int main (int argc, char *argv[]) {
    
    printf ("************************************\n");
    printf ("Starting simple server %f\n", SIMPLE_SERVER_VERSION);
    printf ("Serving the mandelbrot set since 2012\n");   
    
    int port = DEFAULT_PORT;
    if(argc > 1) {
        port = atoi(argv[1]);
    }
    int serverSocket = makeServerSocket (port);
    
    printf ("Access this server at http://localhost:%d/\n", port);
    printf ("************************************\n");
    
    char request[REQUEST_BUFFER_SIZE];
    
    int numberServed = 0;
    while (numberServed < NUMBER_OF_PAGES_TO_SERVE) {
        
        printf ("*** So far served %d pages ***\n", numberServed);
        
        int connectionSocket = waitForConnection (serverSocket);
        // wait for a request to be sent from a web browser, open a new
        // connection for this conversation
        
        // read the first line of the request sent by the browser  
        int bytesRead;
        bytesRead = read (connectionSocket, request, (sizeof request)-1);
        assert (bytesRead >= 0); 
        // were we able to read any data from the connection?
        // print entire request to the console 
        printf (" *** Received http request ***\n %s\n", request);
        input theInput;
        if (strncmp(request,"GET /X",6) == 0 && sscanf(request+5, "X-%lf-%lf-%d", &theInput.x, &theInput.y, &theInput.zoom) == 3){
            // printf("x = %lf, y = %lf, zoom = %d\n", theInput.x, theInput.y, theInput.zoom);
            // Used to check that the input values are read correctly
            //
            //send the browser a bmp tile page using http
            printf (" *** Sending http response ***\n");
            serveBMP(connectionSocket,theDimensions(theInput));
        } else {
            // Default page - enhance abstract viewer
            serveHTML(connectionSocket); 
        }
        
        // close the connection after sending the page- keep aust beautiful
        close(connectionSocket);
        
        numberServed++;
    } 
    
    // close the server connection after we are done- keep aust beautiful
    printf ("** shutting down the server **\n");
    close (serverSocket);
    
    return EXIT_SUCCESS; 
}
Ejemplo n.º 5
0
int main (int argc, char* argv[]) {
 
#ifdef _WIN32
    WSADATA wsaData;
    WSAStartup((2 << 8) + 2, &wsaData);
#endif
 
    puts("************************************");
    printf("Starting simple server %f\n", SIMPLE_SERVER_VERSION);
    puts("Serving poetry since 2011");
 
    int serverSocket = makeServerSocket(DEFAULT_PORT);
    printf("Access this server at http://localhost:%d/\n", DEFAULT_PORT);
    puts("************************************");
 
    char request[REQUEST_BUFFER_SIZE];
    int numberServed = 0;
    while ( numberServed < NUMBER_OF_PAGES_TO_SERVE) {
        printf("*** So far served %d pages ***\n", numberServed);
 
        int connectionSocket = waitForConnection(serverSocket);
        // wait for a request to be sent from a web browser, open a new
        // connection for this conversation
 
        // read the first line of the request sent by the browser
        int bytesRead = recv (connectionSocket, request, sizeof(request) - 1, 0);
        assert (bytesRead >= 0);
        // were we able to read any data from the connection?
 
        // print entire request to the console
        printf (" *** Received http request ***\n %s\n", request);
 
        //send the browser a simple html page using http
        puts (" *** Sending http response ***");
        serveHTML (connectionSocket);
 
        // close the connection after sending the page- keep aust beautiful
        close (connectionSocket);
        ++numberServed;
    }
 
    // close the server connection after we are done- keep aust beautiful
    puts("** shutting down the server **");
    close (serverSocket);
 
#ifdef _WIN32
    WSACleanup();
#endif
 
    return EXIT_SUCCESS;
}
Ejemplo n.º 6
0
int main (int argc, char *argv[]) {

   testPositivePower();

   printf ("************************************\n");
   printf ("Starting simple server %f\n", SIMPLE_SERVER_VERSION);
   printf ("Serving bmps since 2012\n");   
   
   int serverSocket = makeServerSocket (DEFAULT_PORT);   
   printf ("Access this server at http://localhost:%d/\n", DEFAULT_PORT);
   printf ("************************************\n");
   
   char request[REQUEST_BUFFER_SIZE];
   
   int numberServed = 0;
   while (numberServed < NUMBER_OF_PAGES_TO_SERVE) {
      
      printf ("*** So far served %d pages ***\n", numberServed);
   
      int connectionSocket = waitForConnection (serverSocket);

      int bytesRead;
      bytesRead = read (connectionSocket, request, (sizeof request)-1);
      assert (bytesRead >= 0); 

      printf (" *** Received http request ***\n %s\n", request);
   
      printf (" *** Sending http response ***\n");

      printf("%s\n", request);
 
      if (strstr(request, "bmp") != NULL) { 
         serveBMP(connectionSocket, request);
      } else { 

         serveHTML(connectionSocket);
      }

      close(connectionSocket);
   
      numberServed++;
   } 
   
   printf ("** shutting down the server **\n");
   close (serverSocket);
   
   return EXIT_SUCCESS; 
}
Ejemplo n.º 7
0
//---Finds connection with server---//
int startServer(void){

    int serverSocket = makeServerSocket (DEFAULT_PORT);   
    return serverSocket;
    
}
// MAIN FUNCTION -----------------------------------------------------------
int main(int argc, char *argv[])
{
    pthread_t worker;
    pthread_attr_t attr;
    setup(&attr);
    struct logInformation logInfo;
    struct threadArguments taTemp;
    
	parseCommandLineArguments(argc, argv);
    printf("New logging session started on port %i on %s", portNumber, ctime(&timeServerStarted));
    int sockid = makeServerSocket();
    
    // daemonize the process if debuggin mode is not set
    if (commandLineOptions.debuggingMode == FALSE)
    {
        if (daemon_init() != 0)
        {
            printf("Error: unable to daemonize process\n");
            exit(-1);
        }   
    }
    
    while (TRUE)
    {
        // allow an incoming call
        int success = listen(sockid, 1);
        if (success == -1)
        {
            printf("Error: unable to listen on socket\n");
            exit(1);
        }
        
        // wait for an incoming call
        struct sockaddr_in saddr;
        saddr.sin_family = AF_INET;
        socklen_t c_len = sizeof(saddr);
        int fd = accept(sockid, (struct sockaddr *)&saddr, &c_len);
        if (success == -1)
        {
            printf("Error: unable to accept connection on socket\n");
            exit(1);
        }
        //printf("address: %s\n", (char *)inet_ntoa(saddr.sin_addr));
        logInfo.IPaddress = malloc(sizeof((char *)inet_ntoa(saddr.sin_addr)));
        strcpy((logInfo.IPaddress), (char *)inet_ntoa(saddr.sin_addr));
        //printf("We got a call! Yay!\n");
        updateNumberServerRequests(); // update the number of server requests
        
        // get the current time and store it in the log structure
        time_t currentTime = time(0);
        char *timeString = asctime(gmtime(&currentTime)); // get the Greenwich Mean Time
        char *timeStringPointer = timeString;
        // step through the time string until we find the newline character and 
        // change it to a null character to remove it
        while (*timeStringPointer != '\n')
        {
            timeStringPointer++;
        }
        *timeStringPointer = '\0';
        logInfo.timeRequestReceived = timeString;        
        //printf("time request received %s\n", logInfo.timeRequestReceived);

        //int *fdPointer = malloc(sizeof(int));
        //*fdPointer = fd;
        // copy file descriptor for the socket and logInfo structure so we can pass it to a thread
        struct threadArguments *ta = malloc(sizeof(struct threadArguments));
        taTemp.fd = fd;
        taTemp.logInfo = logInfo;
        *ta = taTemp;

        if (commandLineOptions.debuggingMode == TRUE)
        {
            handleCall(ta); // only accept one request at a time
        }
        else
        {
            // create a new thread to handle the request
            pthread_create(&worker, &attr, handleCall, ta);
        }
    }
    
	return 0;
}
int main (int argc, char *argv[]) {
   
   // Test URL extraction function.
   testGetRequest();
     
   printf ("************************************\n");
   printf ("Starting simple server %f\n", SIMPLE_SERVER_VERSION);
   printf ("Serving the Mandelbrot since 2011\n");   
   
   int serverSocket = makeServerSocket (DEFAULT_PORT);   
   printf ("Access this server at http://localhost:%d/\n", DEFAULT_PORT);
   printf ("************************************\n");
   
   char request[REQUEST_BUFFER_SIZE];
   
   int numberServed = 0;
   while (numberServed < NUMBER_OF_PAGES_TO_SERVE) {
      
      printf ("*** So far served %d pages ***\n", numberServed);
      
      int connectionSocket = waitForConnection (serverSocket);
      // Wait for a request to be sent from a web browser, open a new
      // Connection for this conversation
      
      // Read the first line of the request sent by the browser  
      int bytesRead;
      bytesRead = read (connectionSocket, request, (sizeof request)-1);
      assert (bytesRead >= 0); 
      // Were we able to read any data from the connection?
      
      //determine if there is an X in the request
      if(isXFound(request)){
        
         // Read x,y,zoom from URL.
         getRequest(request);
         
         // Print entire request to the console 
         printf (" *** Received http request ***\n %s\n", request);
         
         // Send the browser a simple html page using http
         printf (" *** Sending http response ***\n");
         serveHTML(connectionSocket);
         
      } else {
         
         // Print entire request to the console 
         printf (" *** Received http request ***\n %s\n", request);
         
         // Send the browser a simple html page using http
         printf (" *** Sending http response ***\n");
         serveInteractiveJS(connectionSocket);
         
      }      
      
      // Close the connection after sending the page- keep aust beautiful
      close(connectionSocket);
         
      numberServed++;
      
   } 
   
   // close the server connection after we are done- keep aust beautiful
   printf ("** shutting down the server **\n");
   close (serverSocket);
   
   return EXIT_SUCCESS; 
}