private void Disconnect() { Instances.Disconnect(); endPoint = null; socket = null; }
private void DisConnect() { Instances.Disconnect(); stream = null; packet = null; receviceBuffer = null; socket = null; }
private void ReceivceCallBacks(IAsyncResult result) { try { byte[] _data = socket.EndReceive(result, ref endPoint); socket.BeginReceive(ReceivceCallBacks, null); if (_data.Length < 4) { Instances.Disconnect(); return; } HandleData(_data); } catch (Exception e) { Disconnect(); } }
private void ReceviceCallBack(IAsyncResult result) { try { int _byteLength = stream.EndRead(result); if (_byteLength <= 0) { Instances.Disconnect(); // only close socket return; } byte[] _data = new byte[_byteLength]; Array.Copy(receviceBuffer, _data, _byteLength); packet.Reset(HandleData(_data)); stream.BeginRead(receviceBuffer, 0, dataBufferSize, ReceviceCallBack, null); // loop throw } catch (Exception e) { DisConnect(); Console.WriteLine($"Error Receiving Data :{ e }"); } }