Exemplo n.º 1
0
 private void ListenerThread()
 {
     if (!udpFlag)
     {
         if (udpClient != null)
         {
             try
             {
                 udpClient.Close();
             }
             catch (Exception) { }
         }
         tcpHandler              = new TCPHandler(port);
         tcpHandler.dataChanged += new TCPHandler.DataChanged(ProcessReceivedData);
         //tcpHandler.numberOfClientsChanged += new TCPHandler.NumberOfClientsChanged(numberOfClientsChanged);
     }
     else
     {
         while (!shouldStop)
         {
             if (udpFlag)
             {
                 if (udpClient == null | PortChanged)
                 {
                     udpClient = new UdpClient(port);
                     udpClient.Client.ReceiveTimeout = 1000;
                     iPEndPoint  = new IPEndPoint(IPAddress.Any, port);
                     PortChanged = false;
                 }
                 if (tcpHandler != null)
                 {
                     tcpHandler.Disconnect();
                 }
                 try
                 {
                     bytes  = udpClient.Receive(ref iPEndPoint);
                     portIn = iPEndPoint.Port;
                     NetworkConnectionParameter networkConnectionParameter = new NetworkConnectionParameter();
                     networkConnectionParameter.bytes = bytes;
                     ipAddressIn = iPEndPoint.Address;
                     networkConnectionParameter.portIn      = portIn;
                     networkConnectionParameter.ipAddressIn = ipAddressIn;
                     ParameterizedThreadStart pts = new ParameterizedThreadStart(this.ProcessReceivedData);
                     Thread processDataThread     = new Thread(pts);
                     processDataThread.Start(networkConnectionParameter);
                 }
                 catch (Exception)
                 {
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
        private void ReadCallback(IAsyncResult asyncResult)
        {
            NetworkConnectionParameter networkConnectionParameter = new NetworkConnectionParameter();
            Client client = asyncResult.AsyncState as Client;

            client.Ticks             = DateTime.Now.Ticks;
            NumberOfConnectedClients = GetAndCleanNumberOfConnectedClients(client);
            //if (numberOfClientsChanged != null)
            //numberOfClientsChanged();
            if (client != null)
            {
                int           read;
                NetworkStream networkStream = null;
                try
                {
                    networkStream = client.NetworkStream;

                    read = networkStream.EndRead(asyncResult);
                }
                catch (Exception)
                {
                    return;
                }


                if (read == 0)
                {
                    //OnClientDisconnected(client.TcpClient);
                    //connectedClients.Remove(client);
                    return;
                }
                byte[] data = new byte[read];
                Buffer.BlockCopy(client.Buffer, 0, data, 0, read);
                networkConnectionParameter.bytes  = data;
                networkConnectionParameter.stream = networkStream;
                if (dataChanged != null)
                {
                    dataChanged(networkConnectionParameter);
                }
                try
                {
                    networkStream.BeginRead(client.Buffer, 0, client.Buffer.Length, ReadCallback, client);
                }
                catch (Exception)
                {
                }
            }
        }