Exemplo n.º 1
0
        public async Task DisposeSocketDirectly_ReadWriteThrowIOException()
        {
            using (Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
                    listener.Listen(1);

                    Task <Socket> acceptTask = listener.AcceptAsync();
                    await Task.WhenAll(acceptTask, client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, ((IPEndPoint)listener.LocalEndPoint).Port)));

                    using (Socket serverSocket = await acceptTask)
                        using (DerivedNetworkStream server = new DerivedNetworkStream(serverSocket))
                        {
                            serverSocket.Dispose();

                            Assert.Throws <IOException>(() => server.Read(new byte[1], 0, 1));
                            Assert.Throws <IOException>(() => server.Write(new byte[1], 0, 1));

                            Assert.Throws <IOException>(() => server.BeginRead(new byte[1], 0, 1, null, null));
                            Assert.Throws <IOException>(() => server.BeginWrite(new byte[1], 0, 1, null, null));

                            Assert.Throws <IOException>(() => { server.ReadAsync(new byte[1], 0, 1); });
                            Assert.Throws <IOException>(() => { server.WriteAsync(new byte[1], 0, 1); });
                        }
                }
        }
Exemplo n.º 2
0
        public async Task ReadableWriteableProperties_Roundtrip()
        {
            using (Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
                    listener.Listen(1);

                    Task <Socket> acceptTask = listener.AcceptAsync();
                    await Task.WhenAll(acceptTask, client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, ((IPEndPoint)listener.LocalEndPoint).Port)));

                    using (Socket server = await acceptTask)
                        using (DerivedNetworkStream serverStream = new DerivedNetworkStream(server))
                        {
                            Assert.True(serverStream.Readable && serverStream.Writeable);

                            serverStream.Readable = false;
                            Assert.False(serverStream.Readable);
                            Assert.False(serverStream.CanRead);
                            Assert.Throws <InvalidOperationException>(() => serverStream.Read(new byte[1], 0, 1));

                            serverStream.Readable = true;
                            Assert.True(serverStream.Readable);
                            Assert.True(serverStream.CanRead);
                            await client.SendAsync(new ArraySegment <byte>(new byte[1], 0, 1), SocketFlags.None);

                            Assert.Equal(1, await serverStream.ReadAsync(new byte[1], 0, 1));

                            serverStream.Writeable = false;
                            Assert.False(serverStream.Writeable);
                            Assert.False(serverStream.CanWrite);
                            Assert.Throws <InvalidOperationException>(() => serverStream.Write(new byte[1], 0, 1));

                            serverStream.Writeable = true;
                            Assert.True(serverStream.Writeable);
                            Assert.True(serverStream.CanWrite);
                            await serverStream.WriteAsync(new byte[1], 0, 1);

                            Assert.Equal(1, await client.ReceiveAsync(new ArraySegment <byte>(new byte[1], 0, 1), SocketFlags.None));
                        }
                }
        }