private void ClientThread() { try { Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); client.ReceiveTimeout = 1000; IPEndPoint end = new IPEndPoint(IPAddress.Any, m_port); client.Bind(end); while (m_weWantToBeConnected) { try { byte[] message = new byte[1024]; client.Receive(message, 0, message.Length, SocketFlags.None); my_owner.ParseRxPacket(message, message.Length); } catch { } } } catch (SystemException ex) { my_owner.debug.Append("\r\nCrash in ClientUDP ClientThread:\r\n" + ex.ToString()); } }
private void ServerThread() { while (m_weWantToBeConnected && (m_client != null)) { try { // once someone connects to us m_anyIP populates with their IP and port. byte[] message = m_client.Receive(ref m_serverEP); my_owner.ParseRxPacket(message, message.Length); } catch //(SystemException ex) { //my_owner.debug.Append("\r\nCrash in ServerUDP ServerThread:\r\n" + ex.ToString()); } } m_weWantToBeConnected = false; }
private void ServerThread() { try { NetworkStream stream = null; byte[] message = new byte[4096]; int bytesRead; while (weWantToBeConnected) { if ((m_client != null) && (m_client.Client != null) && m_client.Client.Connected) { m_client.Client.Disconnect(true); } m_client = new TcpClient(); TcpListener tcpListener = new TcpListener(IPAddress.Any, m_port); tcpListener.Start(); while (weWantToBeConnected) { if (!m_client.Connected) { m_client = tcpListener.AcceptTcpClient(); // blocks until someone connects stream = m_client.GetStream(); // configure socket } else { // Blocks until data is recieved, crashes when socket closes bytesRead = stream.Read(message, 0, 4096); my_owner.ParseRxPacket(message, bytesRead); } } } } catch (SystemException ex) { my_owner.debug.Append("\r\nError serverTCP ServerThread\r\n" + ex.ToString()); //my_owner.debug.Append("x"); } StopListening(); }
private void ServerThread() { try { while (weWantToBeConnected) { if (!server.Connected) { server = new TcpClient(m_ip, m_port); } byte[] message = new byte[4096]; int rxCount = server.Client.Receive(message); my_owner.ParseRxPacket(message, rxCount); } } catch //(SystemException ex) { //my_owner.debug.Append("\r\nCrash in ServerThread:\r\n" + ex.ToString()); } Disconnect(); }