Day 28 Network Programming 2

Network Programming 2

TCP three-way handshake and four wave

TCP protocol

tcp send a link as the most basic objects, each TCP connection has two endpoints, the endpoints we call socket (socket), which is defined as the port number of the IP address that is spliced ​​to form the socket.

Establish TCP connection - the three-way handshake

If a client needs to download the data to the server, the client and server must establish a link, this link is the three-way handshake

  • The very beginning of the client and server is handled closed state, the client take the initiative to open, server-side passive open
  1. The client initiates a request to the server, there will be a SYN header
  2. After the server receives the client's request, the client will respond, and the modified header SYN back to the client, and there is a ACK header
  3. When the client receives the server response begins to enter the connected state is established, and then send a request to the server requesting ACK belt, so that the server can enter a connected state

Why use three-way handshake

  • once

Clients directly into a connected state, sends a request to the server into a connected state

Cons: After the server receives the request, the client can not find the corresponding

  • twice
  1. The client temporarily into a connected state, a request is sent to the server
  2. Direct access to the server connection state, transmits a confirmation request to the client, the client receives directly into the connected state

Cons: After the server receives the client's request, into a connected state, occupying a port, such as the client does not respond after sending the request, the port has been occupied by the presence of the short-term, the hacker can initiate a request forgery virtual clients through this principle, short intensive server port in time, resulting in server downtime, there is the risk of two handshakes

TCP connection end - Four waving

  • After the data transfer is complete, both sides need to release the link, the very beginning, the client and server are in an established state, and then take the initiative to shut down the client, server-side passive close
  1. The client sends a request to bring FIN header to the server, requiring broken link
  2. After the server receives the client's request, with a return request header indicates ACK has been confirmed (FIN header case no return, and the server may also need to transmit some data)
  3. After the service ended data transmission services ,, head end will send a FIN request with a report, telling the client side data has been sent over, it requires the client to confirm
  4. The client receives confirmation request service side, after the return of the loan ACK header to inform the server link is closed, after a certain time while the client itself off (2msl)
  • If only three links, but the client may hang up after the second connection, the client does not receive the data transmission between the steps 2-3

Client + server to achieve chat

Server

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server.bind(('127.0.0.1', 8000))

server.listen(5)

print('start...')

while True:
    conn, client_address = server.accept()

    username = conn.recv(1024)
    username = username.decode('utf-8')

    while True:
        data = conn.recv(1024)
        data = data.decode('utf-8')

        print(f'{username}:{data}')

        msg = input('输入:')

        conn.send(msg.encode('utf-8'))

Client

import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

client.connect(('127.0.0.1', 8000))

username = input('请输入您的用户名')
client.send(username.encode('utf-8'))

while True:
    msg = input('请输入:')

    client.send(msg.encode('utf-8'))

    data = client.recv(1024)
    data = data.decode('utf-8')

    print(f'淘宝客服助手:{data}')

SSH server and client

ssh server

import socket, subprocess

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server.bind(('192.168.11.34', 8002))
server.listen(5)

print('start...')

while True:
    conn, client_address = server.accept()
    print(client_address)

    while True:
        cmd = conn.recv(1024)
        cmd = cmd.decode('utf-8')
        print(cmd)

        pipeline = subprocess.Popen(cmd,
                                    shell=True,
                                    stderr=subprocess.PIPE,
                                    stdout=subprocess.PIPE)

        stderr = pipeline.stderr.read()
        stdout = pipeline.stdout.read()

        conn.send(stderr)
        conn.send(stdout)

ssh client

import socket

client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

client.connect(('192.168.11.34',8002))

while True:
    cmd=input('请输入命令')
    client.send(cmd.encode('utf-8'))

    data=client.recv(10240)
    print(data.decode('gbk'))# 因为通过subprocess执行的命令字符编码随系统,所以解码要用gbk

Guess you like

Origin www.cnblogs.com/masterjian924/p/11094531.html