2017-10-20 46 views
1

이 작업에 도움이 필요합니다. CRC 불일치 오류를 해결하는 방법을 모르겠습니다. 나는 한 달 이제 점들을 연결 및 네트워킹을 얻을 이상을 위해 노력했습니다UNet 클라이언트 연결 끊기 오류 : NetworkClient.Connect에서 CRCMismatch

UNet Client Disconnect Error: CRCMismatch UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

: 내 서버에 내 클라이언트에서 바이트 배열을 보낼 수 있습니다하려고 노력하고있어 모든이 오류를 받고 있어요 제대로 작동하려면,하지만 앞으로 나아갈 때마다 5 단계로 돌아갑니다. 나는 기본적으로 서클에 갔었는데, 나는 존재하는 모든 단결 네트워킹 튜토리얼을 해왔다. 그리고 나는 여전히 그렇게 길다. 이 문제를 해결하는 데 정말로 도움이 필요합니다. 누군가가 이걸 도와 주시겠습니까? 아래에 전체 코드를 첨부했습니다. 미리 감사드립니다.

클라이언트 측

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 
} 
+0

AnimalDataMsgType 및 AnimalData 클래스도 제공해 주실 수 있습니까? 그래서, 그것을 알아 내려고 할 수 있습니까? – ZayedUpal

+0

@ZayedUpal 방금 게시물 끝에 012 클래스를 추가했습니다. – greyBow

+0

HLAPI CRC는 알려진 NetworkBehaviour 스크립트의 해시 및 NetworkSettingsAttribute와 함께 사용하는 채널입니다. 그렇습니다. 두 개의 다른 (그리고 호환되지 않는) Unity 프로젝트 나 같은 프로젝트의 버전이 서로 대화를 나누면 발생합니다. 응용 프로그램 (또는 클라이언트와 서버가 별도 인 경우 응용 프로그램)을 다시 작성하십시오. 일부 네트워킹 코드를 변경하고 다시 작성하는 것을 잊어 버리면 발생할 수 있습니다. –

답변

0

내가 바로 client.Connect(serverIP, port); 전에 client.Configure(cc, 1);을 추가하여 내 문제를 해결했다. 문제는 내 NetworkClient 클라이언트 connectionConfig 프로필을 내 NetworkTransport HostTopology에 정의 된 것과 동일한 구성 설정으로 설정하고 connectionConfig를 client.Connect 함수에 설정하지 않고 CRC 불일치를 유발하는 것이 었습니다. 또한 NetworkClient 인스턴스를 만든 후 메시지를 보낼 때 client.SendByChannel을 사용하므로 내 channelId를 설정할 수 있습니다.