Unity3d 5.x的极简主义Python服务器

时间:2016-07-29 08:57:45

标签: c# python unity3d

TL; DR:我在Unity3D客户端应用程序和服务器上的UDP python侦听器之间进行通信时遇到问题。

我试图通过Unity 5.x NetworkTransport LLAPI和Python 3.x套接字模块从Unity3D游戏客户端获得一个低级别的呼叫和响应

目标: 将发送到服务器的消息退回到客户端。

问题:

  • 当我运行Unity3d客户端时,套接字打开,服务器每秒打印一个新的recvfrom数据,但Unity从不接收退回的数据。
  • 在大约10秒后,客户端会收到Timeout错误以及DisconnectEvent。

状态:

客户:Unity 5.4

服务器:亚马逊AWS,端口8888打开

服务器端python app:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('', 8888))

print ('Listening on port 8888')

while True:
    data, addr = s.recvfrom(4096)

    if data:
        for i in range(0, len(data)):
            print (data[i])
        print (data, addr)
        s.sendto(data, addr)

客户端Unity网络类:

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class NetworkExecutor : MonoBehaviour
{
    const string addr = IP_ADDR;
    const int port = PORT;

    bool connected = false;

    // Doing lots of error checks but no need to save them. Let's def this and hold onto it.
    byte e;

    void Start ()
    {
        // Some testing involves waiting for responses or repetitious send/receive calls.
        // We do this in coroutines for both efficiency and human sanity.
        StartCoroutine(TestNetwork());
    }

    IEnumerator TestNetwork()
    {
        // Define network configurations.
        ConnectionConfig config = new ConnectionConfig();
        int reliableChannel = config.AddChannel(QosType.Reliable);
        int maxConnections = 10;
        HostTopology hostTopo = new HostTopology(config, maxConnections);

        // Initialize and connect network with config.
        NetworkTransport.Init();
        int hostId = NetworkTransport.AddHost(hostTopo);
        int connectionId = NetworkTransport.Connect(hostId, addr, port, 0, out e);

        Debug.Log("<b>Connect.</b> Host ID: " + hostId + " Connection ID: " + connectionId);
        ErrStr(e);

        // Send test message.
        byte[] msg = System.Text.Encoding.UTF8.GetBytes("Send string");
        NetworkTransport.Send(hostId, connectionId, reliableChannel, msg, 4096, out e);
        Debug.Log("<b>Send.</b> Msg: " + msg);
        ErrStr(e);

        // Receive test message.
        int recHostId;
        int recConnectionId;
        int recChannelId;
        int recSize;
        msg = System.Text.Encoding.UTF8.GetBytes("Unmodified byte buffer.");
        NetworkEventType eventType = NetworkTransport.Receive(out recHostId, out recConnectionId, out recChannelId, msg, 4096, out recSize, out e);
        Debug.Log("<b>Receive.</b> Type: " + eventType + " Msg: " + System.Text.Encoding.UTF8.GetString(msg));
        Debug.Log("(hID:" + recHostId + " cID:" + recConnectionId + " chId:" + recChannelId + " " + recSize + ")");
        ErrStr(e);

        NetworkTransport.Disconnect(hostId, connectionId, out e);
        ErrStr(e);

        yield break;
    }

    string ErrStr(byte e)
    {
        switch ((NetworkError)e)
        {
            case NetworkError.Ok:
                return "Ok";
            case NetworkError.WrongHost:
                return "<color=red>Wrong Host</color>";
            case NetworkError.WrongConnection:
                return "<color=red>Wrong Connection</color>";
            case NetworkError.WrongChannel:
                return "<color=red>Wrong Channel</color>";
            case NetworkError.NoResources:
                return "<color=red>No Resources</color>";
            case NetworkError.BadMessage:
                return "<color=red>Bad Message</color>";
            case NetworkError.Timeout:
                return "<color=red>Timeout</color>";
            case NetworkError.MessageToLong:
                return "<color=red>Message Too Long</color>";
            case NetworkError.WrongOperation:
                return "<color=red>Wrong Operation</color>";
            case NetworkError.VersionMismatch:
                return "<color=red>Version Mismatch</color>";
            case NetworkError.CRCMismatch:
                return "<color=red>CRC Mismatch</color>";
            case NetworkError.DNSFailure:
                return "<color=red>DNS Failure</color>";
            default:
                return "<color=red><b>Big problem, we don't know this error code.</b></color>";
        }
    }
}

**原谅这个烂摊子。根据我的自然冲动,这里忽略了许多编码惯例和良好做法。这是因为该应用程序仅用于理解Unity和Python最基本的低级网络使用。当可以建立原始信号量时,它将被丢弃并正确重写。

1 个答案:

答案 0 :(得分:8)

UDPTCP都是标准协议。这意味着无论您使用何种编程语言,它们都应该能够相互通信。

您的python代码使用的是标准UDP代码。您的Unity代码。您使用的NetworkTransport API仅用于两个Unity应用程序之间的通信。它是一个LLAPI库,是一个基于UDP的锡层。同样,意味着用于Unity和标准UDP连接之间的连接,但是在两个Unity程序之间。

要与python UDP代码通信,必须在C#代码中使用System.Net.Sockets命名空间中的UdpClient类。这是Unity中的example UDP代码。