简体   繁体   English

无法使用 NetMQ 在 Unity3d 和 Python 之间发送消息

[英]Unable to send messages between Unity3d and Python using NetMQ

I would like to transfer messages back and forth between Android and Python using ZMQ.我想使用 ZMQ 在 Android 和 Python 之间来回传输消息。 I'm using NetMQ/ZMQ to do so.我正在使用 NetMQ/ZMQ 这样做。 Following is my Requester code.以下是我的请求者代码。

public class Requester : RunAbleThread
{
    
    protected override void Run()
    {
        ForceDotNet.Force(); 
        using (RequestSocket client = new RequestSocket())
        {
            client.Connect("tcp://localhost:5556");

            for (int i = 0; i < 10 && Running; i++)
            {
                Debug.Log("Sending Hello");
                client.SendFrame("Hello");

                string message = null;
                bool gotMessage = false;
                while (Running)
                {
                    gotMessage = client.TryReceiveFrameString(out message); 
                    if (gotMessage) break;
                }

                if (gotMessage) Debug.Log("Received " + message);
            }
        }

        NetMQConfig.Cleanup(); 
    }
}

Following is my receiver's code.以下是我的接收器代码。

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5556")

while True:
    #  Wait for next request from client
    message = socket.recv()
    print("Received request: %s" % message)

    socket.send(b"World")

The above code works perfectly fine in Editor mode.上面的代码在编辑器模式下工作得很好。 However, when I run the same code in Android, the requester is not receiving any messages at all.但是,当我在 Android 中运行相同的代码时,请求者根本没有收到任何消息。 What am I missing?我错过了什么? Both devices are connected to the same WIFI hotspot.两台设备都连接到同一个WIFI热点。

You are using您正在使用

client.Connect("tcp://localhost:5556")

The localhost is your device itself!本地主机是您的设备本身!

It worked in the editor since the editor and the python is both running on the same device !它在编辑器中工作,因为编辑器和 python 都在同一设备上运行!

Since the python server is not running on your phone it is no use to try to connect to the port 5556 of your phone ;)由于 python 服务器未在您的手机上运行,​​因此尝试连接到手机的5556端口是没有用的;)

You rather want to use the IP of your PC which is in the same network as your phone.您更愿意使用与手机在同一网络中的 PC 的 IP。 Like eg像例如

client.Connect("tcp://192.168.1.234:5556")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM