发送 UDP 消息有效,接收无效 - Unity3d 上的 C# [英] Sending UDP messages works, receiving not - C# on Unity3d

查看:146
本文介绍了发送 UDP 消息有效,接收无效 - Unity3d 上的 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在尝试在 Unity3d 中设置 UDP 连接,过去几天它一直给我带来一些麻烦:

I am trying to set up a UDP connection in Unity3d and it has been giving me some headace the past days:

我正在运行的 Unity 脚本应该接收并可能在未来发送 UDP 消息.在 Unity 旁边,我使用 Wireshark 来跟踪数据包,并使用 PacketSender 来生成和接收 UDP 消息.

The Unity script I am running is supposed to receive and maybe in the future send UDP messages. Next to Unity I am using Wireshark to track the packets and PacketSender to generate and receive UDP messages.

消息的来源是一个 PLC,它每秒发送一个包,其中包含两个用于测试的浮点数(8 个字节).PLC IP 为 192.168.0.1,它通过以太网直接连接到运行 Unity 的笔记本电脑.(没有交换机,没有路由器)

The source of the messages is a PLC which sends a package every second, containing two floats (8 bytes) for testing. The PLC IP is 192.168.0.1 and it is connected directly via Ethernet to the Laptop running Unity. (no switch, no router)

我的以太网端口的 IP 为 192.168.0.41,消息通过端口 8052 传入.

My Ethernet Port has the IP 192.168.0.41, and the messages are coming in via Port 8052.

我可以通过以下方式在 Wireshark 中看到的内容:

What I can see in Wireshark via :

  • 从 PLC 到达 Unity 的包
  • 从 Unity 到 PLC 的包
  • 包具有预期的结构

我可以在 PacketSender 中看到/执行的操作:

What I can see / do in PacketSender:

  • 来自 PLC 的包(通过端口 8052)
  • 向 Wireshark 中可见的 PLC 发送消息
  • 通过在此处接收包,可以肯定地说防火墙端口窗口正常工作.
  • 如果我已经启动了我的 Unity 接收脚本,我将无法使用 PacketSender 接收包...

... 这应该是一个指标,表明 Unity UDP 套接字已启动并正在运行,希望能够吞下消息.如果我调用 netstat -aon 我可以在预期的端口看到 Unity UPD 进程 &知识产权;0.0.0.0:8052192.168.0.41:8052,取决于它的创建方式.我也可以看到没有其他进程在使用相同的端口.

... which should be an indicator, that the Unity UDP socket is up and running, hopefully swallowing messages. If I call netstat -aon I can see the Unity UPD Process at the expected Port & IP; 0.0.0.0:8052 or 192.168.0.41:8052, depending on how it is created. Also I can see that no other process is using the same port.

但实际上在我的 Unity 脚本中接收和使用数据是行不通的.

有效的是从另一个 Unity 脚本接收通过本地 IP 127.0.0.1 发送的数据.

What works is to receive data send via the local IP 127.0.0.1 from another Unity script.

到目前为止,我已经尝试了几种创建套接字的方法:

By now I have tried several ways to create a socket:

var udpClient = new UdpClient(8052,AddressFamily.InterNetwork)
var udpClient = new UdpClient(8052)
UdpClient udpClient = new UdpClient(8052,AddressFamily.InterNetwork)
var udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

<小时>

我试过了...


I have tried...

  • ...同步或异步接收消息
  • ...在主线程或后台线程中运行接收循环
  • ...启用/禁用客户端阻止
  • ...将客户端绑定到一个 IP 或将其保留在 IPAddress.Any 上
  • ...关闭我在以太网端口上的 HyperV 交换机(仍然关闭)
  • ...在防火墙中打开相关端口
  • ...重新启动笔记本电脑
  • ...关于我可以在任何论坛中找到的所有解决方案.

感觉就像 C# 套接字和 Step7 套接字使用不同的 UDP,或者 C# 套接字在接收端没有按预期运行.包真的在套接字的门口,它忽略了它.

It feels like either the C# socket and the Step7 socket speak a different UDP or the C# socket is not functioning on the receiving side as expected. The package is really at the doorstep of the socket and it ignores it.

设置规范:PLC:CPU315-2 PN/DP笔记本电脑:戴尔 Latitude,i5,8GB RAM,Win10 Enterprise,64 位Unity: Unity 2017.3.1f1,唯一的资产是PlayMaker

Setup specs: PLC: CPU315-2 PN/DP Laptop: Dell Latitude, i5, 8GB RAM, Win10 Enterprise, 64-bit Unity: Unity 2017.3.1f1, Only asset is PlayMaker

当前测试代码:

我知道代码中缺少注释,但正如您可能已经猜到的那样,代码最近发生了很大变化.

I am aware of the lack of comments in the code, but as you might have guessed, the code changed a lot lately.

感谢您的帮助.

推荐答案

因此,在经历了一次非常令人沮丧的旅程之后,我找到了一个不太好但可以胜任的解决方法:

So after a very frustrating journey I found a work-around that is not nice but does the job:

我编写了一个 python 脚本,它打开一个套接字,接收外部数据并通过本地 IP 将其转发到 Unity.那行得通.

I wrote a python script that opens a socket, receives the external data and forwards it via the local IP to Unity. And that works.

代码如下:

# Import libraies
import socket
import time

# Set send IP adress and port
UDP_IP = "127.0.0.1"
UDP_PORT_Rec = 8052
UDP_PORT_Unity = 8055

print("Receiving on Port:" + str(UDP_PORT_Rec))
print("Sending to IP:" + UDP_IP + ":" + str(UDP_PORT_Unity))

# Set socket to send udp messages and bind port
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind(('',UDP_PORT_Rec));

## Keep sending message from file location
while True:
    data, addr = sock.recvfrom(1024) #buffer size, hopefully no problem if too big
    print("Received")

    sock.sendto(data, (UDP_IP, UDP_PORT_Unity))
    print("Send")

对您解决问题有用的工具:

Tools that are useful for your trouble shooting:

  • 安装 Wireshark!它可能是跟踪 Internet 流量的最佳工具.在 Wireshark 中可见的包对于普通应用程序可能不可见!
  • 使用数据包发送器来生成和接收流量.该应用程序使用一种基本方法来接收包 - 如果可以,那么您的程序也应该可以.
  • 使用 netstat -aon 检查您的套接字是否正在运行.
  • 完成:检查您的防火墙设置.
  • Install Wireshark! It is probably the best tool out there to track Internet traffic. Packages that are visible in Wireshark might not be visible for a normal application!
  • Use Packet Sender to generate and receive traffic. The application uses a basic approach to receive packages - If it can then your programm should be able to as well.
  • Check with netstat -aon if your socket is running.
  • For completion: Check your firewall settings.

这篇关于发送 UDP 消息有效,接收无效 - Unity3d 上的 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆