简体   繁体   中英

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. I'm using NetMQ/ZMQ to do so. 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. What am I missing? Both devices are connected to the same WIFI hotspot.

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 !

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 ;)

You rather want to use the IP of your PC which is in the same network as your phone. Like eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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