Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.02 KB | None | 0 0
  1.     /*
  2.     * bmpServer.c
  3.     * 1917 serve that 3x3 bmp from lab3 Image activity
  4.     *
  5.     * Created by Tim Lambert on 02/04/12.
  6.     * Containing code created by Richard Buckland on 28/01/11.
  7.     * Copyright 2012 Licensed under Creative Commons SA-BY-NC 3.0.
  8.     *
  9.     */
  10.      
  11.     #include <stdlib.h>
  12.     #include <stdio.h>
  13.     #include <netinet/in.h>
  14.     #include <string.h>
  15.     #include <assert.h>
  16.     #include <unistd.h>
  17.      
  18.     int waitForConnection (int serverSocket);
  19.     int makeServerSocket (int portno);
  20.     void serveBMP (int socket);
  21.      
  22.     #define SIMPLE_SERVER_VERSION 1.0
  23.     #define REQUEST_BUFFER_SIZE 1000
  24.     #define DEFAULT_PORT 1917
  25.     #define NUMBER_OF_PAGES_TO_SERVE 10
  26.     // after serving this many pages the server will halt
  27.      
  28.     int main (int argc, char *argv[]) {
  29.     printf ("************************************\n");
  30.     printf ("Starting simple server %f\n", SIMPLE_SERVER_VERSION);
  31.     printf ("Serving bmps since 2012\n");
  32.     int serverSocket = makeServerSocket (DEFAULT_PORT);
  33.     printf ("Access this server at http://localhost:%d/\n", DEFAULT_PORT);
  34.     printf ("************************************\n");
  35.     char request[REQUEST_BUFFER_SIZE];
  36.     int numberServed = 0;
  37.     while (numberServed < NUMBER_OF_PAGES_TO_SERVE) {
  38.     printf ("*** So far served %d pages ***\n", numberServed);
  39.     int connectionSocket = waitForConnection (serverSocket);
  40.     // wait for a request to be sent from a web browser, open a new
  41.     // connection for this conversation
  42.     // read the first line of the request sent by the browser
  43.     int bytesRead;
  44.     bytesRead = read (connectionSocket, request, (sizeof request)-1);
  45.     assert (bytesRead >= 0);
  46.     // were we able to read any data from the connection?
  47.     // print entire request to the console
  48.     printf (" *** Received http request ***\n %s\n", request);
  49.     //send the browser a simple html page using http
  50.     printf (" *** Sending http response ***\n");
  51.     serveBMP(connectionSocket);
  52.     // close the connection after sending the page- keep aust beautiful
  53.     close(connectionSocket);
  54.     numberServed++;
  55.     }
  56.     // close the server connection after we are done- keep aust beautiful
  57.     printf ("** shutting down the server **\n");
  58.     close (serverSocket);
  59.     return EXIT_SUCCESS;
  60.     }
  61.      
  62.     void serveBMP (int socket) {
  63.     char* message;
  64.     // first send the http response header
  65.     // (if you write stings one after another like this on separate
  66.     // lines the c compiler kindly joins them togther for you into
  67.     // one long string)
  68.     message = "HTTP/1.0 200 OK\r\n"
  69.     "Content-Type: image/bmp\r\n"
  70.     "\r\n";
  71.     printf ("about to send=> %s\n", message);
  72.     write (socket, message, strlen (message));
  73.     // now send the BMP
  74.     unsigned char bmp[] = {
  75.     0x42,0x4d,0x5a,0x00,0x00,0x00,0x00,0x00,
  76.     0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00,
  77.     0x00,0x00,0x03,0x00,0x00,0x00,0x03,0x00,
  78.     0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00,
  79.     0x00,0x00,0x24,0x00,0x00,0x00,0x13,0x0b,
  80.     0x00,0x00,0x13,0x0b,0x00,0x00,0x00,0x00,
  81.     0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x07,
  82.     0xff,0x07,0x07,0x07,0x07,0x07,0xff,0x00,
  83.     0x00,0x0e,0x07,0x07,0x07,0x66,0x07,0x07,
  84.     0x07,0x07,0x07,0x00,0x00,0x0d,0x07,0x07,
  85.     0x07,0x07,0x07,0x07,0xff,0xff,0xff,0x00,
  86.     0x00,0x0d};
  87.      
  88.      
  89.     write (socket, bmp, sizeof(bmp));
  90.     }
  91.      
  92.      
  93.     // start the server listening on the specified port number
  94.     int makeServerSocket (int portNumber) {
  95.     // create socket
  96.     int serverSocket = socket (AF_INET, SOCK_STREAM, 0);
  97.     assert (serverSocket >= 0);
  98.     // error opening socket
  99.     // bind socket to listening port
  100.     struct sockaddr_in serverAddress;
  101.     bzero ((char *) &serverAddress, sizeof (serverAddress));
  102.     serverAddress.sin_family = AF_INET;
  103.     serverAddress.sin_addr.s_addr = INADDR_ANY;
  104.     serverAddress.sin_port = htons (portNumber);
  105.     // let the server start immediately after a previous shutdown
  106.     int optionValue = 1;
  107.     setsockopt (
  108.     serverSocket,
  109.     SOL_SOCKET,
  110.     SO_REUSEADDR,
  111.     &optionValue,
  112.     sizeof(int)
  113.     );
  114.      
  115.     int bindSuccess =
  116.     bind (
  117.     serverSocket,
  118.     (struct sockaddr *) &serverAddress,
  119.     sizeof (serverAddress)
  120.     );
  121.     assert (bindSuccess >= 0);
  122.     // if this assert fails wait a short while to let the operating
  123.     // system clear the port before trying again
  124.     return serverSocket;
  125.     }
  126.      
  127.     // wait for a browser to request a connection,
  128.     // returns the socket on which the conversation will take place
  129.     int waitForConnection (int serverSocket) {
  130.     // listen for a connection
  131.     const int serverMaxBacklog = 10;
  132.     listen (serverSocket, serverMaxBacklog);
  133.     // accept the connection
  134.     struct sockaddr_in clientAddress;
  135.     socklen_t clientLen = sizeof (clientAddress);
  136.     int connectionSocket =
  137.     accept (
  138.     serverSocket,
  139.     (struct sockaddr *) &clientAddress,
  140.     &clientLen
  141.     );
  142.     assert (connectionSocket >= 0);
  143.     // error on accept
  144.     return (connectionSocket);
  145.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement