zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    些许注意事项(初学)
    第一个方法(初学)
    重载(初学)
    数组(初学)
    文件下载(初学)
    javaweb中上传视频,并且播放,用上传视频信息为例
    javaweb中上传图片并显示图片,用我要上传课程信息(里面包括照片)这个例子说明
    从后台servlet中,获取jsp页面输入的值,来删除用户一行信息
    第二式 观察者模式
    第一式 策略模式
  • 原文地址:https://www.cnblogs.com/foremostxl/p/9664765.html
Copyright © 2011-2022 走看看