【问题标题】:python Raspberry pi receive udp data from spesefic port Visual Studiopython Raspberry pi 从特定端口 Visual Studio 接收 udp 数据
【发布时间】:2016-07-28 03:39:51
【问题描述】:

我目前将 Raspberry Pi 3 作为本地互联网上的文件共享设备。一个 5' 显示器连接到它,我希望能够通过 udp 数据包向它发送命令。 我对 c# 了解很多,但我对 python 完全陌生。我在 Visual Studio 中编程。 我已经用 c# 编写了发送程序

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress serverAddr = IPAddress.Parse(IPADDRESS);
IPEndPoint endPoint = new IPEndPoint(serverAddr, 2522);
byte[] send_buffer = UTF8Encoding.UTF8.GetBytes(TEXT);
sock.SendTo(send_buffer, endPoint);

我知道它之所以有效,是因为我的 UDP-Chat 正在努力工作。 问题是,我如何通过 python 在我的树莓派上接收它? 我试过了:

import socket

UDP_IP = "192.168.1.11" #Which is my local ip for my computer
UDP_PORT = 2522

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
   data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
   print (data)

但我在执行“python Receiver.py”时显示此消息:

Traceback (most recent call last):
  File "Receiver.py", line 7, in <module>
    sock.bind(("192.168.1.11", UDP_PORT))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Cannot assign requested address

我创建了一个新的,命名为 test.py,将它放在同一个文件夹中并运行它。简单的打印“hello world!”,它按预期工作。 我在这里做错了吗?我需要为我的 RPi3 安装额外的东西吗?请帮忙。

【问题讨论】:

    标签: python visual-studio udp raspberry-pi raspbian


    【解决方案1】:

    我想通了。我将 UDP_IP = "192.168.1.11" 更改为 UDP_IP = "" 像这样:

    import socket
    
    UDP_PORT = 2522
    UDP_IP = ""
    
    sock = socket.socket(socket.AF_INET, # Internet
                 socket.SOCK_DGRAM) # UDP
    sock.bind((UDP_IP, UDP_PORT))
    
    while True:
        data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
        print (data)
    

    我之前尝试过 UDP_IP = "",但它给了 Visual Studio Red 线。所以我将它更改为 UDP_IP = "",它工作正常。那个小空间破坏了代码。

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 2017-12-24
      • 2021-04-16
      • 2016-03-26
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多