Practice _ Network Programming Fundamentals

  • Three-way handshake build connection:
    establish a two-way channel, establish a good connection.
    Three-way handshake:
    The first handshake: connection is established, the client sends syn packets to the server, and enter SYN_SENT state, waiting for the server to confirm
    second handshake: server receives syn packets, confirm the customer's SYN, while sending a SYN packet , i.e. SYN + ACK packet, then the server enters a state SYN_RECV;
    third handshake: the client receives the SYN + ACK packet to the server, the server sends an acknowledgment packet ACK (ack = y + 1) , this packet transmission is completed, the client client and server into the ESTABLISHED (TCP connection succeeds) state, complete the three-way handshake.

  • Four wave breaking the connection:
    the first client sends a FIN request to the server, and stops sending data. The client enters the FIN-WAIT-1 (1 terminating wait) state.
    Second, the server receives a request, an ACK confirmation, the server into the CLOSE-WAIT (wait off) state.
    Waiting, after the client receives the acknowledgment server, in which case, the client enters (the last data may also be sent by the server need to accept) (2 termination waiting) state, waiting for the server to send a request FIN FIN-WAIT-2.
    After the third time, the server sends the last data is completed, the client requests to send a FIN, the server enters the LAST-ACK (acknowledgment last) state, waiting for an acknowledgment of the client.
    Fourth, the client requests the server receives FIN, ACK acknowledgment sent, the client enters the TIME-WAIT (wait) state. (Note: At this time the TCP connection has not been released, it has been put through 2MSL (maximum segment lifetime) time, when the client revoke the corresponding TCB, enter CLOSED state only as long as the server has received confirmation sent by the client, Now enter the CLOSED state. Similarly, after the revocation of TCB, is over the TCP connection can be seen, the end time of the server TCP connections than the client earlier.)

2.基于TCP开发一款远程CMD程序
客户端连接服务器后,可以向服务器发送命令
服务器收到命令后执行,无论执行是否成功,无论执行几遍,都将执行结果返回给客户端
注意: 执行系统指令使用subprocess模块完成.

server 端代码:
import socket
import subprocess

server = socket.socket()

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

server.listen(5)

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

    while True:
        try:
            cmd = conn.recv(1024).decode('utf8')
            print(f'接收到命令:{cmd}')

            if cmd == 'q':
                break

            res = subprocess.check_call([cmd])

            if str(res) == '0':
                msg = '命令执行成功!'

            else:
                msg = f'命令执行失败!{str(res)}'

            send_msg = conn.send(msg.encode('utf8'))

        except Exception as e:
            print(e)

            send_msg = conn.send(str(e).encode('utf8'))

            continue

    conn.close()
client 端代码

import socket

client = socket.socket()

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

while True:
    send_msg = input('给服务器发送命令:')

    client.send(send_msg.encode('utf8'))

    if send_msg == 'q':
        break

    data = client.recv(1024).decode('utf8')
    print(f'服务器返回信息:{data}')

client.close()

注意:用以上两段代码模拟客户端和服务器,代码要在两个命令框执行(python解释器不行),并且要先执行server,只有server开始监听了,client才能连接。

Guess you like

Origin www.cnblogs.com/allenchen168/p/11695606.html