Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python进行数据广播和接收_Python_Networking_Udp - Fatal编程技术网

用Python进行数据广播和接收

用Python进行数据广播和接收,python,networking,udp,Python,Networking,Udp,我正在尝试广播一些数据,并使用python接收这些数据。 这是我想出的代码 from socket import * import threading class PingerThread (threading.Thread): def __init__(self): threading.Thread.__init__(self) def run (self): print 'start thread' cs = socket(

我正在尝试广播一些数据,并使用python接收这些数据。 这是我想出的代码

from socket import *
import threading

class PingerThread (threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run (self):
        print 'start thread'
        cs = socket(AF_INET, SOCK_DGRAM)
        cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
        cs.sendto('This is a test', ('192.168.65.255', 4499))

a = PingerThread() 
a.start()

cs = socket(AF_INET, SOCK_DGRAM)
data = cs.recvfrom(1024) # <-- waiting forever
从套接字导入*
导入线程
类PingerThread(threading.Thread):
定义初始化(自):
threading.Thread.\uuuuu init\uuuuuu(自)
def运行(自):
打印“开始线程”
cs=插座(自动线、插座)
cs.setsockopt(SOL_插座,SO_REUSEADDR,1)
cs.setsockopt(SOL_套接字,SO_广播,1)
cs.sendto('这是一个测试',('192.168.65.255',4499))
a=PingerThread()
a、 开始()
cs=插座(自动线、插座)

data=cs.recvfrom(1024)#您的线程可以在开始侦听之前发送其数据

在线程中添加一个循环以停止问题

from socket import *
import threading
import time

class PingerThread (threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run (self):
        for i in range(10):
          print 'start thread'
          cs = socket(AF_INET, SOCK_DGRAM)
          cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
          cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
          cs.sendto('This is a test', ('192.168.1.3', 4499))
          time.sleep(1)

a = PingerThread() 
a.start()


cs = socket(AF_INET, SOCK_DGRAM)
try:
    cs.bind(('192.168.1.3', 4499))
except:
    print 'failed to bind'
    cs.close()
    raise
    cs.blocking(0)
data = cs.recvfrom(1024) # <-- waiting forever
print data
从套接字导入*
导入线程
导入时间
类PingerThread(threading.Thread):
定义初始化(自):
threading.Thread.\uuuuu init\uuuuuu(自)
def运行(自):
对于范围(10)内的i:
打印“开始线程”
cs=插座(自动线、插座)
cs.setsockopt(SOL_插座,SO_REUSEADDR,1)
cs.setsockopt(SOL_套接字,SO_广播,1)
cs.sendto('这是一个测试',('192.168.1.3',4499))
时间。睡眠(1)
a=PingerThread()
a、 开始()
cs=插座(自动线、插座)
尝试:
cs.bind(('192.168.1.3',4499))
除:
打印“绑定失败”
政务司司长(关闭)
提升
cs.阻塞(0)

data=cs.recvfrom(1024)#代码中有三个问题

  • 侦听器未绑定到任何内容
  • 打开的插座未关闭
  • 有时,线程生成得太快,以至于侦听器错过了广播数据
  • 这是修改后的工作代码

    from socket import *
    import time
    import threading
    
    port = 4490
    class PingerThread (threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
    
        def run (self):
            print 'start thread'
            cs = socket(AF_INET, SOCK_DGRAM)
            cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
            cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
    
            time.sleep(0.1) # issue 3 solved
            cs.sendto('This is a test', ('192.168.65.255', port))
    
    a = PingerThread()
    a.start()
    
    cs = socket(AF_INET, SOCK_DGRAM)
    try:
        cs.bind(('192.168.65.255', port)) # issue 1 solved
    except:
        print 'failed to bind'
        cs.close()
        raise
        cs.blocking(0)
    
    data = cs.recvfrom(20)  
    print data
    cs.close() # issue 2 solved
    

    你不需要告诉你正在接收的套接字在哪里收听吗?你可能想看看是哪一层糖很好地覆盖了这个应用程序。