Git Product home page Git Product logo

tcpwebserver's Introduction

TCP Web Server

A program that serves web objects from an existing directory to HTTP clients over the Internet using TCP. Watch a GIF of me interacting with it below!

request  

Features

  • Supports HTTP GET requests.
  • Operates in non-persistent HTTP mode; once the object is served, the server closes the underlying TCP connection.
  • Handles multiple connections simultaneously using multi-threading; spawns a new thread and creates a new Socket for each incoming TCP connection request (unlike UDP, where a single socket is used for all incoming requests and all messages are demultiplexed to the same socket). See the following image:



image

In the above image, you see a service process P1 open three different Sockets for three different connecting clients; every client's segments are demultiplexed to a different Socket at P1.

  • To prevent non-responsive clients from hogging server resources, if the server does not receive an HTTP message from the client after the initial 3-way handshake, the server closes the connection and sends an error message with status code 408. Note that this only occurs if the client is connecting using telnet or certain other application layer protocols. With something like a browser, the handshake is automatically (ie. implicitly) followed by an HTTP request.
  • After server is shutdown, waits reasonable amount of time for current requests to be serviced before terminating.
  • If the server can recover from exceptions (ie. the exception only affected a worker thread), then the server continues with normal execution. Otherwise, it terminates.

Usage/Limitations

  • The root directory of the web server, where the objects are located, is specified as a command line input parameter. If the object path in the GET request is /object-path, then the file containing the object is located on the absolute path server-root/object-path in the file system, where server-root is the root directory of the web server.
  • -p <port_number> specifies the server's port; default is 2025
  • -t <idle_connection_timeout> specifies the time after which the server closes the TCP connection in milli-seconds; default is 0 (which means infinity, ie. idle connections are not closed)
  • -r <server-root> is the root directory of the web server (where all its HTTP objects are located); default is the current directory (directory in which program is ran)
  • quit is typed in the system terminal to shut the server down.
  • Sends responses with HTTP version HTTP/1.1; only returns responses with the following status codes/phrases:
    200 OK
    400 Bad Request
    404 Not Found
    408 Request Timeout
    
  • Assumes all header lines in the HTTP request are formatted correctly; only an error in the HTTP request line can trigger a 400 Bad Request error. A properly formatted request line consists of three mandatory parts which are separated by one or more spaces, as follows: GET /object-path HTTP/1.1. The command GET and protocol HTTP/1.1 are fixed, while the object-path is optional. If no object-path is provided, ie. the request only specifies "/", then index.html is assumed by default.

If you want to start up the project on your local machine:

  1. Download the code as a ZIP:

    download  

  2. Unzip the code:

    unzip  

  3. Open the folder in an IDE, such as VSCode:

    open  

  4. Start the server by compiling all files and then running ServerDriver.java, as follows:

    javac *.java
    java ServerDriver -p <port_number> -t <idle_connection_timeout> -r <server_root>
    



server  

  1. Send a request to the server using telnet, a web browser, or any other application layer protocol:

    request

tcpwebserver's People

Contributors

prempreetbrar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.