zoukankan      html  css  js  c++  java
  • python实现UDP通讯

    Environment

    Client:Windows
    Server:KaLi Linux(VM_virtul)
    Network:Same LAN

    Client

    UDPClient.py

    #-*- coding: utf-8 -*-
    #@Time : 2021/3/27 17:30
    #@Author : HUGBOY
    #@File : UDPClient.py
    #@Software: PyCharm
    
    from socket import *
    
    HOST = '192.168.1.xxx'
    PORT = 666
    
    address = (HOST, PORT)
    s = socket(AF_INET, SOCK_DGRAM)
    
    while True:
        message = input('Send message:')
        s.sendto(message.encode('ascii'), address)
        data = s.recvfrom(1024)
        print(data.decode('ascii'))
    
    s.close()
    
    

    Server

    UDPServer.py

    #-*- coding: utf-8 -*-
    #@Time : 2021/3/27 17:35
    #@Author : HUGBOY
    #@File : UDPServer.py
    #@Software: PyCharm
    
    from socket import *
    
    HOST = '192.168.1.xxx'
    PORT = 666
    
    s = socket(AF_INET, SOCK_DGRAM)
    
    s.bind((HOST, PORT))
    
    print('...Waiting for message...')
    
    while True:
        data, address = s.recvfrom(1024)
        print('Accept message:' + data.decode('ascii'))
        Reply = input('Send message:')
        s.sendto(Reply.encode('ascii'),address)
    s.close()
    

    Effect

    1.Server

    2.Client

    3.Server

    4.Client

    ...


    ________________________________________________________

    Every good deed you do will someday come back to you.

    Love you,love word !
  • 相关阅读:
    spring原理
    架构师和数学
    项目经理需要注意的地方
    如何快速掌握一门新技术
    项目管理要做啥
    编程原则
    架构设计的常用思想
    聊聊编程范式
    程序员与哲学家
    IT人员如何有效规划自己时间
  • 原文地址:https://www.cnblogs.com/hugboy/p/Python_UDP.html
Copyright © 2011-2022 走看看