如何使用TCP在两个Unity应用之间进行通信?

时间:2019-09-03 17:31:17

标签: c# unity3d tcp networkstream

更新

我弄清楚了问题所在。我试图通过TCP传输过多的数据,这导致冻结。由于某种原因,这没有在编辑器中显示出来……谁知道是什么原因。如果其他人偶然发现了这个问题(在像Unity这样的程序中,函数不断循环,并且始终在处理数据),请考虑您正在移动过多无关数据。

原始帖子

我遇到了很多问题,希望我能得到一些指导。

简而言之,我想知道如何使用TCP在同一台计算机上通信两个Unity应用。我已经在编辑器中运行了它,但是当两个应用程序都构建完成后,通讯很快就会中断。

这真让我感到难过,因为我不明白为什么应用程序可以在编辑器环境中运行,而不能在官方版本中运行。

当我使用TCP在同一台计算机上的两个Unity应用之间进行通信时,只要其中一个保持在Unity环境中,它就可以工作。也就是说,如果我构建一个应用程序,然后在Unity编辑器中打开另一个应用程序,则TCP通信可以正常进行。

这里有更多背景知识:我的一个应用程序正在充当用户界面,另一个正在与窥镜连接以提供游戏中对象的全息显示。最初,它们被组合为一个应用程序-但要在两个分辨率不同的显示器之间运行Unity的多显示器支持时遇到了很多麻烦。 Looking Glass工厂甚至提供了一个预制件来执行此操作,但是在当前的SDK中已被破坏。因此,我不得不使用套接字在两个应用程序之间进行接口,每个监视器一个。

我正在使用C#的TCP侦听器类:https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.tcplistener?view=netframework-4.8

和TCP客户端类:https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient?view=netframework-4.8

当前,UI充当TCPListener,产生全息图的应用程序是TCPClient。在上述每个应用程序中,我使用两个队列-IncomingMessages队列和Outgoing Messages队列-它们是在主线程和网络线程之间共享的全局变量。

TCP侦听器:

private void Start()
{
    incomingMessages = new Queue();
    outgoingMessages = new Queue();

    Application.runInBackground = true;
    thread = new Thread(new ThreadStart(Receive));
    thread.Start();

    //stuff happens that's irrelevant to this question.  And then...   
}

void Receive()
{
    TcpListener server = null;

    try
    {
        // Set the TcpListener on port 13000.
        Int32 port = 13000;
        IPAddress localAddr = IPAddress.Parse("127.0.0.1");

        // TcpListener server = new TcpListener(port);
        server = new TcpListener(localAddr, port);

        // Start listening for client requests.
        server.Start();

        // Buffer for reading data
        Byte[] bytes = new Byte[256];
        String data = null;

        // Enter the listening loop.

        Debug.Log("About to reenter main while in Server...");

        while (threadContinue)
        {
            Debug.Log("Waiting for a connection... ");

            // Perform a blocking call to accept requests.
            // You could also user server.AcceptSocket() here.

            TcpClient client = server.AcceptTcpClient();
            Debug.Log("Connected!");

            data = null;

            // Get a stream object for reading and writing
            NetworkStream stream = client.GetStream();

            int i;

            // Loop to receive all the data sent by the client.
            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                // Translate data bytes to a ASCII string.
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                Debug.Log("Received from Client: " + data);

                lock (this)

                incomingMessages.Enqueue(data);

                string response = supplyData();
                byte[] msg = System.Text.Encoding.ASCII.GetBytes(response);
                // Send back a response.
                stream.Write(msg, 0, msg.Length);
                Debug.Log("Sent to Client: " + response);
            }

            // Shutdown and end connection
            client.Close();  
        }
    }


    catch (SocketException e)
    {
        Debug.Log("SocketException: ");
        Debug.Log(e);
    }
    finally
    {
        // Stop listening for new clients.
        server.Stop();
    }
        Debug.Log("Exiting 'Receive'");
}

这是TCP客户端。它会尝试定期连接,以及在有新数据可用时进行连接。这样一来,它就可以定期从服务器接收信息,并在可用时共享新数据:

void Start()
{
    //prepare networking
    Application.runInBackground = true;

    outgoingMessages = new Queue();
    incomingMessages = new Queue();

    thread = new Thread(new ThreadStart(Connect));
    thread.Start();

    //stuff happens that's irrelevant to this question...
}

private void Connect()
{
    String server = "127.0.0.1";
    Int32 port = 13000;
    string message = "";
    while (threadContinue == true)
    {
        if (timeToConnect())
        {
            lastConnection = ourTime;
            if (outgoingMessages.Count > 0)
                message = outgoingMessages.Dequeue().ToString();
            else
                message = "Nothing to report.";

            try
            {
                // Create a TcpClient.
                // Note, for this client to work you need to have a TcpServer 
                // connected to the same address as specified by the server, port
                // combination.

                client = new TcpClient(server, port);

                // Translate the passed message into ASCII and store it as a Byte array.
                Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

                // Get a client stream for reading and writing.
                //  Stream stream = client.GetStream();

                stream = client.GetStream();

                // Send the message to the connected TcpServer. 
                stream.Write(data, 0, data.Length);

                Debug.Log("Sent to Server: " + message);


                // Buffer to store the response bytes.
                data = new Byte[256];

                // String to store the response ASCII representation.
                String responseData = String.Empty;

                // Read the first batch of the TcpServer response bytes.
                Int32 bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

                lock (this)

                    incomingMessages.Enqueue(responseData);

                Debug.Log("Received from Server: " + responseData);


                stream.Close();
                client.Close();

            }
            catch (ArgumentNullException e)
            {
                Debug.Log("ArgumentNullException: ");
                Debug.Log(e);

                outgoingMessages.Enqueue(message);
            }
            catch (SocketException e)
            {
                Debug.Log("SocketException: ");
                Debug.Log(e);
                outgoingMessages.Enqueue(message);
            }
        }
    }
}


private bool timeToConnect()
{
    if ((ourTime - lastConnection > NETWORK_DELAY) || (outgoingMessages.Count > 0))
        return true;
    return false;
}

在单独的线程中实例化,以便Unity的主线程可以不受阻碍地继续运行。

再次-它可以在编辑器中使用,但是当我构建它时,它会损坏。

1 个答案:

答案 0 :(得分:1)

更新

我弄清楚了问题所在。我试图通过TCP传输过多的数据,这导致冻结。由于某种原因,这并没有在编辑器中显示出来……只是在导出的应用程序中出现了。谁知道是什么原因。如果还有其他人偶然发现了这个问题...您通过构建可通过网络进行通信的多个应用程序来绕过Unity的多显示功能...则认为您负担的队列太多了。