System.Net.Sockets.NetworkStream.Dispose C# (CSharp) Метод

Dispose() защищенный Метод

protected Dispose ( bool disposing ) : void
disposing bool
Результат void
        protected override void Dispose(bool disposing)
        {
#if DEBUG
            using (DebugThreadTracking.SetThreadKind(ThreadKinds.User))
            {
#endif
                // Mark this as disposed before changing anything else.
                bool cleanedUp = _cleanedUp;
                _cleanedUp = true;
                if (!cleanedUp && disposing)
                {
                    // The only resource we need to free is the network stream, since this
                    // is based on the client socket, closing the stream will cause us
                    // to flush the data to the network, close the stream and (in the
                    // NetoworkStream code) close the socket as well.
                    if (_streamSocket != null)
                    {
                        _readable = false;
                        _writeable = false;
                        if (_ownsSocket)
                        {
                            // If we own the Socket (false by default), close it
                            // ignoring possible exceptions (eg: the user told us
                            // that we own the Socket but it closed at some point of time,
                            // here we would get an ObjectDisposedException)
                            Socket chkStreamSocket = _streamSocket;
                            if (chkStreamSocket != null)
                            {
                                chkStreamSocket.InternalShutdown(SocketShutdown.Both);
                                chkStreamSocket.Close(_closeTimeout);
                            }
                        }
                    }
                }
#if DEBUG
            }
#endif
            base.Dispose(disposing);
        }

Usage Example

Пример #1
0
        public static void GenerateLoad(int packetsPerBurst, int sleepMilliseconds)
        {
            IPAddress address = (from a in Dns.GetHostAddresses(Environment.MachineName)
            where a.AddressFamily == AddressFamily.InterNetwork
            select a).FirstOrDefault();

            Console.WriteLine("IP Address is: {0}", address.ToString());

            Socket listenSocket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            listenSocket.Bind(new IPEndPoint(address, Port));
            listenSocket.Listen(10);

            Socket sendSocket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            sendSocket.Connect(new IPEndPoint(address, Port));
            NetworkStream writeStream = new NetworkStream(sendSocket, true);

            Socket receiveSocket = listenSocket.Accept();
            NetworkStream readStream = new NetworkStream(receiveSocket, true);
            BinaryReader reader = new BinaryReader(readStream);

            byte[] buffer = new byte[PacketSize];
            while (!Console.KeyAvailable)
            {
                for (int i=0; i<packetsPerBurst; i++)
                {
                    writeStream.Write(buffer, 0, buffer.Length);
                    readStream.Read(buffer, 0, buffer.Length);
                }
                
                if (sleepMilliseconds > 0)
                {
                    Thread.Sleep(sleepMilliseconds);
                }

            }
            Console.ReadKey();

            writeStream.Dispose();
            readStream.Dispose();
            listenSocket.Dispose();
        }
All Usage Examples Of System.Net.Sockets.NetworkStream::Dispose