The web server listens on a specific port for incoming HTTP requests, and when it receives a request, it parses the request to determine the requested resource such as a web page or imag
Trang 1VIETNAM - KOREA UNIVERSITY OF INFORMATION AND
COMMUNICATION TECHNOLOGY
Faculty Of Computer Science
REPORT
BUILDING A WEB SERVER WITH JAVA
SOCKETS
Students: LÊ PHÚ QUỐC
Class: 20GIT
Instructor: Assoc Prof PhD HUỲNH CÔNG PHÁP
Danang, January 2023
Trang 2VIETNAM - KOREA UNIVERSITY OF INFORMATION AND
COMMUNICATION TECHNOLOGY
Computer Science Department
REPORT
BUILDING A WEB SERVER WITH JAVA
SOCKETS
Students: LÊ PHÚ QUỐC
Class: 20GIT
Instructor: Assoc Prof PhD HUỲNH CÔNG PHÁP
Danang, January 2022
Trang 3THANK YOU
First of all, we would like to thank the lecturers of the Computer Department of Vietnam - Korea University of Information and Communication Technology and the instructor Assoc Prof Ph.D Huynh Cong Phap for guiding and instructing us I have successfully completed the report
In the process of making the report, errors cannot be avoided We look forward
to receiving your comments and corrections to make our report more complete, as well
as to learn more experiences to better complete the upcoming report
Students
Lê Phú Quốc
Trang 4THANK YOU 3
CONTENTS 4
LIST OF ACRONYMS 6
PREAMBLE 7
1 Introduction: 7
2 Target: 7
3 Report layout: 8
Chapter 1 OVERVIEW 9
1 Socket 9
2 HTTP Protocols 9
3 End of chapter 1: 11
Chapter 2 IMPLEMENTATION 12
1 Program structure 12
2 Implementation 13
3 Result 15
4 End of chapter 2 17
Chapter 3 CONCLUSIONS AND DEVELOPMENT ORIENTATIONS .18
1 Conclusions 18
1.1 Achievements 18
1.2 Limitations: 18
2 Development: 18
REFERENCES 19
Trang 5LIST OF FIGUR
Figure 1 Web Server Overview 11
Figure 2 Downloading 2 files Web Server and resource.zip 12
Figure 3 Extracting resource.zip 12
Figure 4 Executing WebServer.jar 13
Figure 5 Browsing to folder which contains files of resource.zip 13
Figure 6 Picture page 14
Figure 7 Response Header 15
Figure 8 Profile page and web server GUI 16
Y
Trang 61 Introduction:
A simple web server system is a software program that receives and responds to requests from clients over the internet It consists of one or more physical or virtual machines running a web server software program, which is responsible for processing incoming requests and sending responses back to the client The web server listens on
a specific port for incoming HTTP requests, and when it receives a request, it parses the request to determine the requested resource (such as a web page or image) and sends back an appropriate response The system typically also includes a database for storing and retrieving data related to the website, as well as other services such as email servers, caching servers, load balancers, and security components A simple web server system is a critical component of any website or web application, as it serves as the foundation for delivering content and services to users over the internet
2 Target:
- Learn and master the necessary languages to apply to the design
- Learn, read sources of documents and reference information
- Research the actual situation, through books, or the internet
- Understanding web architecture
- Learn TCP/IP, HTTP network protocols
- Network programming support libraries
3 Report layout:
After the Introduction, the report is presented in three chapters, specifically as follows:
- Chapter 1 Overview
In this chapter, the report presents the concepts and characteristics of the components used to implement the project
- Chapter 2 Implementation
The content presents the results of the process of implementing and developing the topic
- Chapter 3 Conclusions and development orientations
Trang 7The content presents achievements and limits and directions for development Finally, there are references and appendices related to the topic
Trang 8Chapter 1 OVERVIEW
1 Socket
A socket is a software endpoint that enables communication between different applications or processes over a network The TCP/IP protocol is a set of networking protocols that is used to establish a connection between different devices over the internet Together, sockets and TCP/IP provide a foundation for network
communication and data transfer
When two devices communicate using sockets and the TCP/IP protocol, they establish
a reliable and ordered stream of data between them This is done by breaking data into small packets that are transmitted over the internet, and then reassembling those packets at the destination device TCP/IP also provides a mechanism for error checking and correction, ensuring that the data that is received is identical to the data that was sent
Sockets and TCP/IP are used in many different types of applications, from web browsers to email clients to file transfer programs They are used to transfer a wide variety of data, including text, images, audio, and video
One important concept to understand when working with sockets and TCP/IP is the difference between a client and a server In this context, a client is a device that initiates a connection, while a server is a device that listens for incoming connections When a client initiates a connection, it sends a request to the server, which responds with a message This back-and-forth communication is the basis for many network applications, such as web browsing, email, and instant messaging
In summary, sockets and TCP/IP are essential components of network communication, allowing different devices to exchange data in a reliable and ordered manner By understanding how these technologies work, you can develop more effective and efficient network applications that can take advantage of the power and reach of the internet
2 HTTP Protocols
HTTP (Hypertext Transfer Protocol) is a protocol for transferring data over the Internet HTTP is used to transmit web pages, images, audio, video, and other documents between devices connected to the Internet
HTTP works on the Client-Server model When a client requests a resource from the server, the client sends a request to the server The server will return a response
Trang 9(response) with the resource requested by the client Requests and responses are sent between the client and the server as packets on the network
Every HTTP request and response has a standard format An HTTP request consists of the following parts:
- Request line: This is the first line of the request, containing information about the method (method), path (path) and version of HTTP that the client is using
- Header: Additional information fields of the request, like User-Agent, Accept-Encoding, Content-Type, etc These headers provide the server with information about the client and the client's request
- Body: The body of the request, which can be data sent to the server (like when POSTing data) or nothing (like when GET resources)
The HTTP response consists of the following parts:
- Status line: This is the first line of the response, containing information about the HTTP version, the status code, and a message describing that status code
- Header: Additional information fields of the response, like Server, Content-Type, Content-Length, etc These headers provide the client with information about the server's response
- Body: The body of the response, containing the requested resource (such as when GET resources) or the error message (such as when an error occurs during request processing)
HTTP also supports many methods (methods) to make different requests, like GET, POST, PUT, DELETE, etc Each method will have different purposes and uses, depending on the type of request the client wants to make
HTTP is a very important protocol in the transmission of data on the Internet, especially in accessing websites and web applications It is a widely used standard protocol and is supported by most web browsers, web servers, and devices connected
to the Internet
The status code is an important part of HTTP, used to provide information about the result of a request Status codes are sent in the status line of the response and are categorized into 5 main categories:
- 1xx (Informational): Information, being processed
- 2xx (Successful): Successful, the request was processed successfully
- 3xx (Redirection): Redirect, request is redirected to another address
- 4xx (Client Error): Client error, request is invalid or cannot be processed
- 5xx (Server Error): Server error, server cannot handle client request
HTTP headers are also important, as they provide additional information about the request and response Some important HTTP headers include:
- User-Agent: Information about the browser or application sending the request
- Content-Type: The data type of the content sent to the server or returned to the client
- Content-Length: The length of the content sent to the server or returned to the client
- Cookie: Information about the client's session on the server
Trang 10In an overview, HTTP is a very important protocol for the transmission of data on the Internet, especially in accessing websites and web applications Understanding HTTP will help you understand how websites work, which in turn can optimize performance and improve user experience
3 End of chapter 1:
Understanding the knowledge, components and technologies used to implement the project is the basis for the next phase of the project - implementation, which will be presented in the next chapter
Trang 11Chapter 2 IMPLEMENTATION
1 Program structure
Figure 1 Web Server Overview
Figure 1 illustrates the high-level structure of my Web Server, with 5 class and
1 interface:
- MainFrame: The main class, MainFrame is response for starting and stopping HttpServer This is also graphics user interface
- HttpServer: is responsible listening on port which is configured by MainFrame (or 443, default ssl port) and spawaning an instance of SocketHandler for each client connection Its start method enters an infinite loop, spawning a new thread for each accepted connection and handing the new socket off to a new SocketHandler and then awaiting a new client connection
- SocketHandler: in turn, is responseble for implementing the bulk of the HTTP
protocol itself Its goal is to create a Request object, fill it in with the parse
method, path, version and request headers, create a Response object and hand
Trang 12that off to the associated Handler to actually fill in and respond to the client Most of the actual parsing of the HTTP request is handed off to the Request object itself
- Request: Parse the request of client: get method, path, url, header, parameter, query parameter
- Response: is a relatively passive class; it respond with the text HTTP/1.1 STATUS MESSAGE, a variable-length lisst of headers in the from NAME: VALUE, a blank line, and then the response body The Handler should set a response code, invoke addHandler once for each response header it plans to return, add a body and then invoke send to complete the response
- Handler: is an interface
2 Implementation
Step 1: Download 2 files WebServer.jar and resource.zip
Figure 2 Downloading 2 files Web Server and resource.zip
Step 2: Extract file resource.zip
Figure 3 Extracting resource.zip
Trang 13Step 3: Execute file WebServer.jar
Figure 4 Executing WebServer.jar
Step 4: Browse to folder which contains files and folder of resource.zip
Figure 5 Browsing to folder which contains files of resource.zip
Step 5: Start server
Trang 143 Result
Figure 6 Picture page
Trang 164 End of chapter 2
After completing the topic, the group will evaluate the results achieved and consult with the lecturers for updates and future development directions
Trang 17Chapter 3 CONCLUSIONS AND DEVELOPMENT
ORIENTATIONS
1 Conclusions
1.1 Achievements
- Learn and master the necessary languages to apply to the design
- Learn, read sources of documents and reference information
- Research the actual situation, through books, or the internet
- Understanding web architecture
- Learn TCP/IP, HTTP network protocols
- Network programming support libraries
1.2 Limitations:
Besides the achievements, the web server still have some limitations:
- Http methods are not fully supported yet
2 Development:
- Programming a more complete web server
Trang 18REFERENCES