I. Content Review and Supplement
- Brief description of three handshakes and four waves
- The three handshake
- accept accepts connections waiting for clients in the process
- connect client initiates a syn link request
- If you get the server-side response ack, you will also receive a syc link request from the server-side.
- After the client responds to ack, a link to the tcp protocol is established
- The process of three handshakes is coded by accept and connect, and the details are not reflected in socket.
- The four wave.
- There are close methods in the code corresponding to server and client.
- The close operation initiated by each end is a fin disconnection request. Once the disconnection confirmation ack is obtained, the data transmission at one end can be terminated.
- If both ends initiate close, then there are two requests and two replies, a total of four operations.
- You can end the data transmission at both ends, indicating that the link is disconnected
- The three handshake
- Characteristics of tcp protocol and udp protocol
- tcp protocol: a connection-oriented, streaming, reliable, slow, full-duplex communication
- Mail file http web
- udp protocol: a datagram-oriented, connectionless, unreliable, fast and efficient communication protocol that can complete one-to-one, one-to-many, many-to-one, many-to-many
- Online Watching of Instant Chat Tool Video
- tcp protocol: a connection-oriented, streaming, reliable, slow, full-duplex communication
2. Sticky Phenomenon
- Definition: After executing multiple commands at the same time, the result is likely to be only one part, while executing other commands, the other part of the result is received before execution, which is the phenomenon of sticky package.
- Cause of sticky: Only TCP has sticky phenomenon, UDP will never stick.
- Data Transfer in TCP Protocol
- Unpacking mechanism of tcp protocol
- Flow-Oriented Communication Characteristics
- Data Transfer in TCP Protocol
- There are two cases of stickiness:
- Case 1 Sender's Caching Mechanism
- Case 2 Receiver's Caching Mechanism
- Conclusion:
- Sticky phenomenon only occurs in tcp protocol
- On the surface, sticky packet problem is mainly due to the buffer mechanism of sender and receiver, and the characteristics of tcp protocol oriented to traffic communication.
- In fact, it's mainly because the receiver doesn't know the boundaries between messages and how many bytes of data to extract at one time.
- tcp protocol sticking phenomenon
- What is the sticky phenomenon?
- Packets occurring at the sender
- Because the sending time interval of two data is short + the length of data is small
- So the optimization mechanism of tcp protocol sends two messages as one message
- In order to reduce the network latency of "acknowledge receipt" in tcp protocol
- Occurs re-receiving end sticky
- Because the data transmitted in tcp protocol has no boundaries, it is too late to receive more than one data
- Data will stick together on the cache side of the receiving core
- Essence: The boundaries of receiving information are not clear
- Packets occurring at the sender
- Solving the sticking problem
- Custom Protocol 1
- First, the header is sent. The header length is 4 bytes. The content is the byte length of the message to be sent.
- The struct module pack converts all digits into 4 bytes in a fixed way
- Resend message
- First, the header is sent. The header length is 4 bytes. The content is the byte length of the message to be sent.
- Custom Protocol 2
- We specialize in protocols for sending files.
- Send the byte length of the header dictionary first, then send the dictionary (the dictionary contains the name and size of the file), and then send the content of the file.
- Custom Protocol 1
- What is the sticky phenomenon?
3. Verify the Legality of Client
-
Client is for user-login validation
- Your users can see your client source code, so they don't need to write their own client.
-
The client is provided to the machine for use
# Method 1: # server.py import os import hashlib import socket def get_md5(secret_key,randseq): md5 = hashlib.md5(secret_key) md5.update(randseq) res = md5.hexdigest() return res def chat(conn): while True: msg = conn.recv(1024).decode('utf-8') print(msg) conn.send(msg.upper().encode('utf-8')) sk = socket.socket() sk.bind(('127.0.0.1',9000)) sk.listen() secret_key = b'alexsb' while True: conn,addr = sk.accept() randseq = os.urandom(32) conn.send(randseq) md5code = get_md5(secret_key,randseq) ret = conn.recv(32).decode('utf-8') print(ret) if ret == md5code: print('Is a legitimate client') chat(conn) else: print('Not a legitimate client') conn.close() sk.close() # client.py import hashlib import socket import time def get_md5(secret_key,randseq): md5 = hashlib.md5(secret_key) md5.update(randseq) res = md5.hexdigest() return res def chat(sk): while True: sk.send(b'hello') msg = sk.recv(1024).decode('utf-8') print(msg) time.sleep(0.5) sk = socket.socket() sk.connect(('127.0.0.1',9000)) secret_key = b'alexsb' randseq = sk.recv(32) md5code = get_md5(secret_key,randseq) sk.send(md5code.encode('utf-8')) chat(sk) sk.close()
# Mode two: # server.py import os import hmac import socket def get_hmac(secret_key,randseq): h = hmac.new(secret_key,randseq) res = h.digest() return res def chat(conn): while True: msg = conn.recv(1024).decode('utf-8') print(msg) conn.send(msg.upper().encode('utf-8')) sk = socket.socket() sk.bind(('127.0.0.1',9000)) sk.listen() secret_key = b'alexsb' while True: conn,addr = sk.accept() randseq = os.urandom(32) conn.send(randseq) hmaccode = get_hmac(secret_key,randseq) ret = conn.recv(16) print(ret) if ret == hmaccode: print('Is a legitimate client') chat(conn) else: print('Not a legitimate client') conn.close() sk.close() # client.py import socket import time import hmac def get_hmac(secret_key,randseq): h = hmac.new(secret_key,randseq) res = h.digest() return res def chat(sk): while True: sk.send(b'hello') msg = sk.recv(1024).decode('utf-8') print(msg) time.sleep(0.5) sk = socket.socket() sk.connect(('127.0.0.1',9000)) secret_key = b'alexsb' randseq = sk.recv(32) hmaccode = get_hmac(secret_key,randseq) sk.send(hmaccode) chat(sk) sk.close()
4. Implementing concurrency of tcp protocol directly
-
Socket server module directly implements concurrent server side of tcp protocol
# server.py import socketserver class Myserver(socketserver.BaseRequestHandler): def handle(self): # The handle method is automatically triggered, and self. request = conn msg = self.request.recv(1024).decode('utf-8') self.request.send('1'.encode('utf-8')) msg = self.request.recv(1024).decode('utf-8') self.request.send('2'.encode('utf-8')) msg = self.request.recv(1024).decode('utf-8') self.request.send('3'.encode('utf-8')) server = socketserver.ThreadingTCPServer(('127.0.0.1',9000),Myserver) server.serve_forever() # client1.py import socket sk = socket.socket() sk.connect(('127.0.0.1',9000)) for i in range(3): sk.send(b'hello,yuan') msg = sk.recv(1024) print(msg) sk.close() # client2.py import socket sk = socket.socket() sk.connect(('127.0.0.1',9000)) for i in range(3): sk.send(b'hello,wusir') msg = sk.recv(1024) print(msg) sk.close()