Ejemplo n.º 1
0
        private void StartListening()
        {
            NetworkStream      networkStream      = _localClient.GetStream();
            NetworkStreamState networkStreamState = new NetworkStreamState()
            {
                networkStream = networkStream,
                buffer        = new byte[1024]
            };

            networkStream.BeginRead(networkStreamState.buffer, 0, networkStreamState.buffer.Length, OnDataReadCompleted, networkStreamState);
        }
Ejemplo n.º 2
0
        private void OnDataReadCompleted(IAsyncResult AsyncResult)
        {
            NetworkStreamState networkStreamState = (NetworkStreamState)AsyncResult.AsyncState;
            int numberOfReadBytes = 0;

            try
            {
                numberOfReadBytes = networkStreamState.networkStream.EndRead(AsyncResult);
            }
            catch
            {
                numberOfReadBytes = 0;
            }
            if (numberOfReadBytes == 0)
            {
                //networkStreamState.networkStream.Close(0);
                return;
            }
            //networkStreamState.networkStream.Close(0);
            byte[] receivedBytes = new byte[numberOfReadBytes];
            Buffer.BlockCopy(networkStreamState.buffer, 0, receivedBytes, 0, numberOfReadBytes);
            if (DatagramReceivedEvent != null)
            {
                Delegate[] delArray = DatagramReceivedEvent.GetInvocationList();
                foreach (Delegate del in delArray)
                {
                    try
                    {
                        EventHandler <TcpDatagramReceivedEventArgs <byte[]> > method = (EventHandler <TcpDatagramReceivedEventArgs <byte[]> >)del;
                        method.BeginInvoke(this, new TcpDatagramReceivedEventArgs <byte[]>(_localClient, receivedBytes), OnReceivedDatagramProcessComplete, null);
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
            networkStreamState.networkStream.BeginRead(networkStreamState.buffer, 0, networkStreamState.buffer.Length, OnDataReadCompleted, networkStreamState);
        }