// Start is called before the first frame update void Start() { Debug.Log("Connecting to server..."); Debug.Log("In Start.."); ClientHandleNetworkPackets.InitializeNetworkPackages(); _clientSocket.BeginConnect(IP_ADDRESS, PORT, new AsyncCallback(ConnectCallback), _clientSocket); }
private void OnReceive() { byte[] _sizeInfo = new byte[4]; byte[] _receivedBuffer = new byte[1024]; int totalRead = 0; int currentRead = 0; try { currentRead = totalRead = _clientSocket.Receive(_sizeInfo); if (totalRead <= 0) { Debug.Log("You are not connected to the server."); } else { while (totalRead < _sizeInfo.Length && currentRead > 0) { currentRead = _clientSocket.Receive(_sizeInfo, totalRead, _sizeInfo.Length - totalRead, SocketFlags.None); totalRead += currentRead; } int messageSize = 0; messageSize |= _sizeInfo[0]; messageSize |= (_sizeInfo[1] << 8); messageSize |= (_sizeInfo[2] << 16); messageSize |= (_sizeInfo[3] << 24); byte[] data = new byte[messageSize]; totalRead = 0; currentRead = totalRead = _clientSocket.Receive(data, totalRead, data.Length - totalRead, SocketFlags.None); while (totalRead < messageSize && currentRead > 0) { currentRead = _clientSocket.Receive(data, totalRead, data.Length - totalRead, SocketFlags.None); totalRead += currentRead; } ClientHandleNetworkPackets.HandleNetworkInformation(data); } } catch (Exception ex) { Debug.Log("You are not connected to the server."); Debug.Log(ex); } }