• 22 网络编程--TCP和UDP实现聊天例子


    1、TCP简单的一次对话,单通信

    客户端:

    import socket
    HOST = 'localhost'
    PORT = 50006
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    client.connect((HOST,PORT))
    client.sendall(b'hello,python')
    data = client.recv(1024)
    print('Received',data)

    服务端:

    import socket
    HOST = ''
    PORT = 50006
    sock_server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock_server.bind((HOST,PORT))
    sock_server.listen(1)
    conn,addr = sock_server.accept()
    
    while True:
        data =conn.recv(1024)
        if not data:break
        conn.send(data)

    2、循环收发数据,单通信(TCP)

    客户端:

    import socket
    HOST = 'localhost'
    PORT = 50006
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    client.connect((HOST,PORT))
    while True:
        cmd = input('>>').strip()
        if not cmd:break
    
        client.send(cmd.encode('utf-8'))
        data = client.recv(1024)
        print('Received',data.decode('utf-8'))

    服务端:

    import socket
    HOST = ''
    PORT = 50006
    sock_server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock_server.bind((HOST,PORT))
    sock_server.listen(1)
    conn,addr = sock_server.accept()
    
    while True:
        data =conn.recv(1024)
        if not data:break
        conn.send(data)

    3、双方都说话(TCP)

    客户端:

    import socket
    HOST = 'localhost'
    PORT = 50000
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    client.connect((HOST,PORT))
    while True:
        cmd = input('>>').strip()
        if not cmd:break
        client.send(cmd.encode('utf-8'))
    
        data = client.recv(1024)
        print('Received',data.decode('utf-8'))

    服务端:

    import socket
    HOST = ''
    PORT = 50000
    sock_server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock_server.bind((HOST,PORT))
    sock_server.listen(1)
    
    
    while True:
        conn, addr = sock_server.accept()
        while True:
            data =conn.recv(1024)
            print(f"收到客户端的的数据:>{data.decode('utf-8')}")
            if not data:break
            response = input('回复客户端>>').strip()
            conn.send(response.encode('utf-8'))

    4、基于UDP协议实现

    客户端:

    import socket
    ip_port = ('127.0.0.1',9000)
    BUFSIZE = 1024
    udp_client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    
    while True:
        MSG = input('>>').strip()
        udp_client.sendto(MSG.encode('utf-8'),ip_port)
        back_msg,addr = udp_client.recvfrom(BUFSIZE)
        print(back_msg.decode('utf-8'))

    服务端:

    import socket
    ip_socket =('127.0.0.1',9000)
    BUFSIZE = 1024
    udp_server = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    udp_server.bind((ip_socket))
    
    while True:
        msg,addr = udp_server.recvfrom(BUFSIZE)
        print(msg.decode('utf-8'),addr)
    
        response = input('>>').strip()
        udp_server.sendto(response.encode('utf-8'),addr)
  • 相关阅读:
    [国嵌攻略][113][字符设备驱动模型]
    [国嵌攻略][112][使用字符设备驱动]
    [国嵌攻略][111][硬件访问技术]
    [国嵌攻略][110][驱动开发概述]
    [国嵌攻略][109][Linux系统调用]
    [国嵌攻略][108][Linux内核链表]
    [国嵌攻略][107][Linux进程管理子系统]
    [国嵌攻略][106][Linux内存管理子系统]
    关于坐标转换!重要!(转)
    【总结整理】关于切图工具及自定义地图
  • 原文地址:https://www.cnblogs.com/foremostxl/p/9664765.html
Copyright © 2020-2023  润新知