【问题标题】:UDP in Jython 2.7Jython 2.7 中的 UDP
【发布时间】:2016-02-22 20:23:09
【问题描述】:

我正在尝试编写一个 python 函数,将 UDP 消息发送到远程主机并接收回复,但我很难理解如何做到这一点。 我一直在看线程:Simple Python UDP Server: trouble receiving packets from clients other than localhost

我知道如何发送东西,但是如何以正确的顺序发送和接收?

提前致谢。

【问题讨论】:

    标签: python sockets udp


    【解决方案1】:

    请忽略,我知道了..

    import socket
    UDP_IP = "127.0.0.1"
    UDP_PORT = 3001
    MESSAGE = "asd"
    
    sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
    sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
    
    sock.bind((UDP_IP, UDP_PORT))
    
    while True:
        data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
        print "received message:", data
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 2015-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多