In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to establish a TCP connection in http. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. Establish a TCP connection 1.1 the essence of the TCP server and client web service is a TCP server and the browser is a TCP client. The yellow part of figure 3 is the establishment of a TCP connection through which browsers can send http requests only if a connection is established. You may have noticed that I skipped DNS parsing and went straight to part 2 because you have to understand TCP connections before you can understand DNS parsing. The following code is a sample import socket on the TCP server side
# specify protocol
Server = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
# make the port reusable
Server.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# bind ip and port
Server.bind (('0.0.0.0, 8080))
# Monitoring
Server.listen (1)
# waiting for messages
Clientsocket, address = server.accept ()
# receive messages
Data = clientsocket.recv (1024)
# turn off socket
Clientsocket.close ()
Server.close ()
It is also very easy to write client code.
Import socket
Import time
Host = '127.0.0.1'
Port = 8081
Addr = (host, port)
Client = socket.socket (socket.AF_INET,socket.SOCK_STREAM)
# Connect to server
Client.connect (addr)
# send data to server
Client.send (baked I am client')
# receive the data returned by server
Revcdata = client.recv (1024)
# all the data received are of bytes type
Print (revcdata.decode (encoding='utf-8'))
Time.sleep (1)
Client.close ()
There is an important concept in the process of establishing TCP-3-way handshake. This question is often used in interviews to examine the candidate's knowledge of socket.
1.2 what is a TCP3 handshake
Before establishing a proper connection, the client and the server should test each other and ask each other if they are willing to establish a connection with themselves.
What is drawn above is a very simple understanding, but only describes the function, but does not answer what is the tcp3 handshake from the technical level, but if you want to explain it from the technical level, you must first understand that the TCP header structure below is the TCP header structure. When the client and the server send data, they must abide by the TCP/IP protocol and send the data in the specified format. The TCP header structure carries a lot of key information. For example, source port number and destination port number
Notice the white area in the middle of the image above, and find SYN and ACK, each of which occupies one bit bit, which is the key to the 3-way handshake:
The client sends a syn packet to the server. The so-called syn packet means that the SYN is 1.
After the server receives the syn packet, it sends the syn+ack packet. In this case, both SYN and ACK are 1.
The client sends an ACK packet to the server, and the ACK identification bit is 1
This is the 3-way handshake when TCP establishes a connection.
1.3 Why is it that there are 3 handshakes instead of 2 handshakes? it seems that two handshakes are enough. After receiving the SYN packet, the server just replies to ACK to confirm that the connection can be established. Why does the server have to send SYN and the client returns ACK? The requirement for 3 handshakes is to prevent invalid SYN packets from accidentally arriving at the server, causing inconsistencies between the two sides. Considering this situation, the client sent SYN packet S1 to the server, but coincidentally, S1 was stranded in the network due to network congestion or other reasons. after all, it is 108000 miles away from the client to the server, and there are many network devices such as switches and routers in the middle, and the network will also be stuck in traffic. After the network traffic jam, the client thought that S1 had lost its packet, so it sent another SYN packet, S2MagS2, to the server smoothly. If you only need to shake hands twice, then the server will establish a connection with an ACK. But don't forget that there is a S1 in the network who does not know that the client has sent S2 again. It dutifully passes through many obstacles and reaches the server after S2. If the connection established by S2 still exists at this time, then the server will ignore S1, but what if the connection established by S2 has been disconnected? The server will think that S1 is a new connection request, and the server will return to ACK to establish a connection. When the client receives the ACK, it will be a little confused. It has no idea what is going on and does not know what to do with the sudden arrival of the ACK packet. In this way, both sides will waste a lot of resources. The above accident occurs when the connection is established with 2 handshakes, but not with 3 handshakes. Both the server and the client have to send SYN packets and reply to each other's ACK packets. Only in this way can the connection be established. When the server replies to ACK, the SYN is set to 1, which eliminates the need to send SYN packets separately, and the connection can be established with 3 handshakes. The above is how to establish a TCP connection in http. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.