In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how TCP protocol implements HTTP server". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian to study and learn "how TCP protocol implements HTTP server" together.
This article mainly introduces go language based on tcp protocol to achieve a simple http server, you can learn to:
1. How to implement a simple http server
2. Deepen the understanding of http protocol through wrong http protocol format
Usually we use http servers, such as apache, nginx are a complete and powerful server, if we do not look at the source code can not understand the internal implementation of http server. If we don't implement a simple http server ourselves, we may not understand http well, and we should learn not only to know it but also to know why.
This article starts with tcp, then uses tcp socket to send data to client in http format, where client refers to browser.
TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte stream-based transport layer communication protocol.
TCP protocol belongs to transport layer protocol, http protocol belongs to application layer protocol, http protocol is located above tcp protocol, http protocol is also based on tcp protocol implementation. Many application layer protocols are based on tcp protocol implementation, such as: file transfer protocol ftp, ssh protocol, telenet protocol.
Here we begin our programming journey!
First listen to local tcp port 8888
listen, err := net.Listen("tcp", "127.0.0.1:8888")
Then print the listening information
fmt.Println("server start listening on port 8888")
Then delay the tcp stream
defer listen.Close()
After that, the for loop enters the monitoring, and if there is a connection, goroutine is started to process.
go
Next, let's look at the implementation of the handleClient function
go
The handleClient function first defines the response body and prints the length of the response body. Then define the response header, which starts with "HTTP/1.1 200 OK\n", which is the standard response header of http protocol, and then define the response type and the length of the response body. Response header and response body are separated by "\n\r\n".
Finally, it is sent to the browser in byte format.
Let's see how it works! Enter in browser
http://127.0.0.1:8888/
browser
You can see the browser displaying the data returned by the server we started. The response headers in Response Headers are exactly what we set.
Congratulations, you have completed your own http server!
But everything we have to draw inferences from one instance to another, let's try again if the http status is 500, what is the result returned?
http
browser
This http500 is what we often encounter in development, and now we have simulated it ourselves.
We can try to remove the separator between the response header and the response body
http
Let's see what happens.
browser
The result is that browsers can't recognize it, which is why there is http protocol. First, specify that each browser implements the protocol according to the protocol, so that there will be no problems.
Thank you for reading, the above is "TCP protocol how to implement HTTP server" content, after the study of this article, I believe we have a deeper understanding of how TCP protocol to implement HTTP server this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.