zoukankan      html  css  js  c++  java
  • socket编程-udp

    server.py

    # socket编程-udp
    from socket import *
    import time
    
    ip_port = ('127.0.0.1', 8080)
    buffer_size = 1024
    
    udp_server = socket(AF_INET, SOCK_DGRAM)  # SOCK_DGRAM代表数据报式套接字,即udp
    udp_server.bind(ip_port)
    
    while True:  # 不需要listen及accept建立连接,直接进入通讯循环
        data, addr = udp_server.recvfrom(buffer_size)
        data = (data.decode('utf-8'))
        print(data)
        back_time = time.strftime('%Y-%m-%d %X')
        newstr = '%s %s' % (back_time, data)
        udp_server.sendto(newstr.encode('utf-8'), addr)
    
    udp_server.close()

    client.py

    from socket import *
    
    ip_port = ('127.0.0.1', 8080)
    buffer_size = 1024
    
    udp_client = socket(AF_INET, SOCK_DGRAM)
    
    while True:
        msg = input('===>').strip()
        udp_client.sendto(msg.encode('utf-8'), ip_port)
        data, addr = udp_client.recvfrom(buffer_size)
        print(data.decode('utf-8'))
    
    udp_client.close()
  • 相关阅读:
    JVM系列三:JVM参数设置、分析
    JVM系列二:GC策略&内存申请、对象衰老
    HotSpot VM GC 的种类
    2.静态库和动态库
    1.GCC编译过程
    6.树
    5.队列
    4.栈
    3.线性表
    2.算法
  • 原文地址:https://www.cnblogs.com/dangrui0725/p/9479099.html
Copyright © 2011-2022 走看看