我需要一些真正的幫助。我不確定如何解決CRC不匹配錯誤。所有我想要做的是從我的客戶端發送一個字節數組到我的服務器,我得到這個錯誤:UNet客戶端斷開錯誤:在NetworkClient.Connect上的CRCMismatch
UNet Client Disconnect Error: 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
@ZayedUpal我剛剛在帖子末尾添加了這些類 – greyBow
HLAPI CRC是已知NetworkBehaviour腳本和它們使用的通道(與NetworkSettingsAttribute一起)的哈希。是的,如果兩個不同(並且不兼容)的Unity項目或同一個項目的版本彼此交談,則會發生這種情況。 嘗試重建您的應用程序(或應用程序,如果客戶端和服務器是分開的)。如果你改變一些網絡代碼,並忘記重建,可能會導致這種情況。 –