Developers and designers can select different arrangements and architecture for their computers to develop an internet service. In this article, we are talking about the most commonly used
client-server model. There are mainly two roles in this model:
- client: A client computer initiates a request for the resources and the information from the server computer.
- server: A server computer processes the client request and responds with appropriate information.
To conduct communication between a client and a server a common language is necessary. We can develop and use any language we desire to make these computers communicate with each other. But to make it usable, compatible, and widely accessible we need a standardized language (aka protocol). This is where the HTTP comes in. - HTTP stand for Hypertext Transfer Protocol.
- HTTP protocol defines the structure for request and response messages. It is like sending a parcel to your friend an address structured as street name, city name, state name, zip code, and country code.
- HTTP defines operations that can be performed in a given resource which is called HTTP request methods. It is also referred to as HTTP verbs. Some examples are:
- The GET method requests a representation/state of the specified resource/data. It should only retrieve data.
- The POST method submits an entity to the specified resource, often causing a change in the server.
- HTTP protocol also defines different sets of status codes to indicate different states that may occur during communication like successful responses (200-299), client error responses (400-499), server error responses (500-599), etc.
- HTTP requests and responses share a similar structure and are composed of:
- A single start-line describing the requests/response to be implemented, or it's status of whether successful or a failure.
- An optional set of HTTP headers specifying the request/response or describing the body included in the message.
- A blank line indicating all meta-information for the request has been sent.
- An optional body containing data associated with the request (like the content of an HTML form) or the document associated with a response. The presence of the body and its size is specified by the start-line and HTTP headers.
The software model of the Internet can be defined by two types of layered models: OSI model and IP model Internet Protocol Suite. OSI model stands for Open Systems Interconnection model. It defines the standard for the Internet and networking. Hence, it is also called the reference model. IP model stands for Internet Protocol model. It is practically implemented and used by the Internet. Without going into much depth main points are:
- Every computer (client or server), and network devices that supports the Internet implements the IP suite model.
- These models are structured as layers hence called layered structure
- Each layer has its responsibility and hides its implementation (inner working) from other layers
- Each layer provides an interface to layers immediately above or below itself
In IP suite model, Application layer combines all three layers from OSI model i.e Application + Presentation + Session
In IP suite model, Link layer combines lower two layers from OSI model i.e Physical + Datalink
The lowermost layer from each model connects with physical network devices like routers, switches, etc.
HTTP comes under the Application layer which is accessible to the user process. HTTP protocol uses the services provided by the Transport layer (TCP or UDP) to establish and conduct communication with other computers on the Internet. But to glue these two layers, the operating system provides an interface called socket API. Programming that utilizes socket API is called socket programming.
A socket is like the two ends of a water pipe that handles the flow of water. But the only difference is that the information flows in both directions in a socket. A socket can be identified by a socket address which combines protocol type, IP address, and port number. Socket API makes it easy to use different protocols and develop communication in a network. The following figure shows a general flow of communication using socket API. We will understand more about these socket functions later in our coding part.
This figure shows the socket functions implemented in the C language. In this article, we will be using socket functions from Java.
Note: Please first install
java JDKfor this project, setup your environment variables, and check everything is working well.
- Main project folder will contain a
resourcesfolder. In the
publicfolder, we will store all our
.htmlpages, and in the
resourcesfolder, we will store our resources like images, videos, etc.
- Our main server program
WebServer.javawill be in the project's root directory.
- If you are coding along with this article please store any video inside the
./resourcesfolder and name it
- The project structure should look something like this.
The above code does the following things:
- Creates a socket and assigns a port number to listen (aka server's socket address).
- The server's system listens to the assigned port for any new connection request from the client-side
- If the connection is successful and accepted by the server, it returns the client's socket address
- Initializes the
Connectionclass with the client's socket info to process the client's HTTP request and send back an HTTP response
The above code does the following things:
- Accesses the input stream from the link that bridges the server and the client socket. This stream is used to read the client's HTTP request.
- Accesses the output stream from the link that bridges the server and the client socket. This stream is used to send an HTTP response from the server computer.
- Open a command line in the project directory and compile the java file with the command
javac WebServer.java. Two class files
Connection.classwill be created.
- Then run the server with the command
- Open the browser, which will be our client, and you can open the
inspectwindows and then the
networktab in the browser to see the network activity
localhost:9090in the address bar to send a request to our server. Then our
index.htmlpage be loaded
- Now click the blue link
Serve me some cat videosto request the server for a video
- In the
networktab, we can notice that after clicking the link. Our client sends two requests to the server: one for the
cats.htmlpage and one for the video file.
What we have learned in this article is just a speck of technology used on the Internet. We should be thankful for all those brilliant minds and engineering that make the Internet possible. Most importantly stay curious, and learn.