/// <summary> /// Функция-обработчик потока /// </summary> protected override void doThread() { while (mustWork) { try { IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 11000); socketListener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socketListener.Bind(ipEndPoint); socketListener.Listen(10); Logger.Add("Accepting connection"); Socket socketHandler = socketListener.Accept(); Logger.Add("Accepted connection"); socketHandler.ReceiveTimeout = 3000; IsWorking = true; while (mustWork) { byte[] bytes = new byte[1024]; int bytesLength = socketHandler.Receive(bytes); string package = Encoding.UTF8.GetString(bytes, 0, bytesLength); IsCorrect = true; try { Direction = deserializer.Do(package); } catch (Exception e) { IsCorrect = false; } switch (axis) { case RotationAxis.Pitch: Data = Direction.X; break; case RotationAxis.Roll: Data = Direction.Y; break; case RotationAxis.Yaw: Data = Direction.Z; break; } if (bytesLength == 0) break; } IsWorking = false; IsCorrect = true; socketListener.Close(); ipEndPoint = null; } catch (Exception e) { exception = new ExceptionServer(e.Message); closeSocket(); } } }
/// <summary> /// Закрывает сокет /// </summary> private void closeSocket() { try { socketListener.Shutdown(SocketShutdown.Both); } catch (Exception e) { exception = new ExceptionServer(e.Message); } socketListener.Close(); }