【问题标题】:UNet Client Disconnect Error: CRCMismatch on NetworkClient.ConnectUNet 客户端断开连接错误:NetworkClient.Connect 上的 CRCMismatch
【发布时间】:2018-04-01 19:55:15
【问题描述】:

我需要一些真正的帮助。我不确定如何解决 CRC 不匹配错误。我要做的就是将一个字节数组从我的客户端发送到我的服务器,我收到了这个错误:

UNet 客户端断开连接错误:CRCMismatch UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

我已经尝试了一个多月来连接各个点并让网络正常运行,但我向前迈出的每一步都会导致向后退五步。我基本上一直在兜圈子,我已经完成了所有存在的统一网络教程,但我仍然很迷茫。我真的需要帮助解决这个问题。有人可以帮我吗?我还在下面附上了完整的代码。提前谢谢!

客户端

public class Client : NetworkBehaviour {

NetworkClient client = new NetworkClient();

private const int MAX_CONNECTION = 100;

private string serverIP = "127.0.0.1";
private int port = 5708;
private int hostId;
private int webHostId;
private int reliableChannel;
private int reliableSeqChannel;
private int reliableFragChannel;
private int unreliableChannel;
private int unreliableSeqChannel;

private string playerName;
private int connectionId;
private float connectionTime;
private bool isStarted = false;
private bool isConnected = false;
private bool readyToSendMsg = false;
private byte error;

private GameObject infoDisplayText;

public Texture2D texToSend;
string typeToSend = "Deer";
string idToSend = "1";
int strengthToSend = 80;
int hitPointsToSend = 2;

private string GetPlayerName()
{
    switch (Network.player.ipAddress.ToString())
    {
        case "192.168.1.160":
            playerName = "SMO Server";
            break;

        case "192.168.1.161":
            playerName = "SMO Client 1";
            break;

        case "192.168.1.162":
            playerName = "SMO Client 2";
            break;

        case "192.168.1.163":
            playerName = "SMO Client 3";
            break;

        case "192.168.1.164":
            playerName = "SMO Client 4";
            break;

        default:
            playerName = "SMO UNREG";
            break;
    }
    return playerName;
}

private void Start()
{
    infoDisplayText = GameObject.Find("InfoDisplay");

    client.RegisterHandler(AnimalDataMsgType.SYSTEM_CONNECT, OnConnected);
    client.RegisterHandler(AnimalDataMsgType.SYSTEM_DISCONNECT, OnDisconnected);
    client.RegisterHandler(AnimalDataMsgType.SYSTEM_ERROR, OnError);

    client.Connect(serverIP, port);
}

public void Connect()
{
    string pName = GetPlayerName();

    if (pName == "")
        return;

    playerName = pName;

    NetworkTransport.Init();

    ConnectionConfig cc = new ConnectionConfig();

    reliableChannel = cc.AddChannel(QosType.Reliable);
    reliableSeqChannel = cc.AddChannel(QosType.ReliableSequenced);
    reliableFragChannel = cc.AddChannel(QosType.ReliableFragmented);
    unreliableChannel = cc.AddChannel(QosType.Unreliable);
    unreliableSeqChannel = cc.AddChannel(QosType.UnreliableSequenced);
    cc.PacketSize = 1440;
    cc.FragmentSize = 900;
    cc.ResendTimeout = 1000;
    cc.DisconnectTimeout = 5000;
    cc.ConnectTimeout = 1000;
    cc.MaxConnectionAttempt = 5;

    HostTopology topo = new HostTopology(cc, MAX_CONNECTION);

    hostId = NetworkTransport.AddHost(topo, 0);

    // Run client/server on different machines
    //hostID = NetworkTransport.AddHost(topo, port, null);  

    connectionId = NetworkTransport.Connect(hostId, serverIP, port, 0, out error);

    infoDisplayText = GameObject.Find("InfoDisplay");
    infoDisplayText.GetComponent<Text>().text += playerName + " connected.\n";

    connectionTime = Time.time;
    isConnected = true;
}

private void Update()
{
    if (!isConnected)
        return;

    int recHostId, connectionId, channelId;
    byte[] recBuffer = new byte[1024];
    int bufferSize = 1024;
    int dataSize;
    byte error;

    NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);

    switch (recData)
    {
        case NetworkEventType.ConnectEvent:
            Debug.Log("Player " + connectionId + " has connected");
            infoDisplayText.GetComponent<Text>().text += "Connected to Server.\n";
            break;
    }
}

public void SendOnButtonPress()
{
    if (readyToSendMsg == true)
        SendTexture(texToSend, typeToSend, idToSend, strengthToSend, hitPointsToSend);
}

//Call to send the Texture and a simple string message
public void SendTexture(Texture2D tex, string type, string id, int strength, int hitpoints)
{
    AnimalData animalData = new AnimalData();

    animalData.Tex = tex.GetRawTextureData();
    animalData.Type = type;
    animalData.Id = id;
    animalData.Strength = strength;
    animalData.Hitpoints = hitpoints;

    client.Send(AnimalDataMsgType.animalData, animalData);
}

private void OnConnected(NetworkMessage netMsg)
{
    readyToSendMsg = true;
    Debug.Log("Connected to server");
}

private void OnDisconnected(NetworkMessage netMsg)
{
    readyToSendMsg = false;
    Debug.Log("Disconnected from server");
}

private void OnError(NetworkMessage netMsg)
{
    //SystemErrorMessage errorMsg = reader.SmartRead<SystemErrorMessage>();
    // Debug.Log("Error connecting with code " + errorMsg.errorCode);
    Debug.Log("Error connecting.");
}

服务器端

public class Server : NetworkBehaviour
{
    private const int MAX_CONNECTION = 100;
    private int port = 5708;
    private int hostId;
    private int webHostId;
    private int reliableChannel;
    private int reliableSeqChannel;
    private int reliableFragChannel;
    private int unreliableChannel;
    private int unreliableSeqChannel;

    private bool isStarted = false;
    private byte error;

    private GameObject infoDisplayText;

    private void Awake()
    {
        infoDisplayText = GameObject.Find("InfoDisplay");
    }

    private void Start()
    {
        NetworkTransport.Init();

        ConnectionConfig cc = new ConnectionConfig();

        reliableChannel = cc.AddChannel(QosType.Reliable);
        reliableSeqChannel = cc.AddChannel(QosType.ReliableSequenced);
        reliableFragChannel = cc.AddChannel(QosType.ReliableFragmented);
        unreliableChannel = cc.AddChannel(QosType.Unreliable);
        unreliableSeqChannel = cc.AddChannel(QosType.UnreliableSequenced);

        HostTopology topo = new HostTopology(cc, MAX_CONNECTION);

        hostId = NetworkTransport.AddHost(topo, port, null);

        if (NetworkTransport.IsStarted)
        {
            isStarted = true;
            Debug.Log("NetworkTransport is Started.");
            infoDisplayText.GetComponent<Text>().text += "NetworkTransport is Started.\n";
        }

        Debug.Log("Server Started.");
        infoDisplayText.GetComponent<Text>().text += "Server Started.\n";

        setupRegisterHandler();
    }
    private void Update()
    {
        if (!isStarted)
            return;

        int recHostId, connectionId, channelId;
        byte[] recBuffer = new byte[1024];
        int bufferSize = 1024;
        int dataSize;
        byte error;

        NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);

        switch (recData)
        {
            case NetworkEventType.ConnectEvent:
                Debug.Log("Player " + connectionId + " has connected");
                infoDisplayText.GetComponent<Text>().text += "Player " + connectionId + " has connected\n";
                break;
        }
    }

    // Create a client and connect to the server port
    public void setupRegisterHandler()
    {
        NetworkServer.Listen(port);
        Debug.Log("Registering server callbacks");
        NetworkServer.RegisterHandler(AnimalDataMsgType.animalData, OnTextureReceive);
    }

    //Called when texture is received
    public void OnTextureReceive(NetworkMessage netMsg)
    {
        AnimalData animalData = netMsg.ReadMessage<AnimalData>();

        string type = animalData.Type;
        Debug.Log("Type : " + type);

        string id = animalData.Id;
        Debug.Log("ID : " + id);

        int strength = animalData.Strength;
        Debug.Log("Strength : " + strength);

        int hitpoints = animalData.Hitpoints;
        Debug.Log("Hit Points : " + hitpoints);

        //Your Received Texture2D
        Texture2D receivedtexture = new Texture2D(1280, 1024);
        receivedtexture.LoadRawTextureData(animalData.Tex);
        receivedtexture.Apply();

        Debug.Log(type + " data received!");
        infoDisplayText.GetComponent<Text>().text += type + " data received!\n";
    }
}

AnimalDataMsgType 类

    public class AnimalDataMsgType
{
    public static short animalData = MsgType.Highest + 1;
    public static short SYSTEM_CONNECT = MsgType.Connect;
    public static short SYSTEM_DISCONNECT = MsgType.Disconnect;
    public static short SYSTEM_ERROR = MsgType.Error;
}

AnimalData 类

public class AnimalData : MessageBase
{
    public byte[] Tex;      // data coming from CanvasController
    public string Type;     // data coming from CanvasController
    public string Id;       // data coming from GameManager
    public int Strength;    // data coming from PlayerController
    public int Hitpoints;   // data coming from PlayerController
    public bool IsAlive;    // data coming from PlayerController
}

【问题讨论】:

  • 能否请您也提供 AnimalDataMsgType 和 AnimalData 类?那么,我可以试着弄清楚吗?
  • @ZayedUpal 我刚刚在帖子末尾添加了这些课程
  • HLAPI CRC 是已知 NetworkBehaviour 脚本和它们使用的通道的哈希值(使用 NetworkSettingsAttribute)。是的,如果两个不同(且不兼容)的 Unity 项目或同一项目的版本相互通信,就会发生这种情况。尝试重建您的应用程序(或应用程序,如果客户端和服务器是分开的)。如果您更改了一些网络代码,可能会导致忘记重建。
  • @greyBow 你可以给你选择服务器游戏对象的项目窗口的屏幕截图吗?您如何管理 NetworkManager?
  • @SergiyKlimkov 这是有道理的。所以客户端和服务器都在同一个 Unity 项目中。从我的客户端脚本中的 Connect 函数,我正在初始化并初始连接到我的服务器,在服务器端,我正在 Start 函数中立即启动我的服务器。该连接事件成功。如果那个成功,是什么让我的`client.Connect(serverIP, port);`失败?我需要为该连接创建一个新的 ConnectionConfig 吗?我只是不确定自己做错了什么。

标签: c# unity3d networking crc


【解决方案1】:

我通过在client.Connect(serverIP, port); 之前添加client.Configure(cc, 1); 解决了我的问题。问题是我的 NetworkClient 客户端 connectionConfig 配置文件需要设置为在我的 NetworkTransport HostTopology 中定义的相同配置设置,并且没有将 connectionConfig 设置为我的 client.Connect 函数,它会导致 CRC 不匹配。此外,在创建我的 NetworkClient 实例后发送消息时,我使用client.SendByChannel,这样我就可以设置我的 channelId。

【讨论】:

    【解决方案2】:

    部分或全部 ConnectionConfig 值必须在主机和客户端上匹配。当我指定不同的 DisconnectTimeout 值时遇到了同样的问题。

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-01
      • 2021-05-11
      相关资源
      最近更新 更多