NetworkStream.Socket 属性

定义

获取基础 Socket

C#
public System.Net.Sockets.Socket Socket { get; }
C#
protected System.Net.Sockets.Socket Socket { get; }

属性值

表示基础网络连接的 Socket

示例

下面的代码示例检索基础 Socket 以验证活动连接。

C#
using System;
using System.Net;
using System.Net.Sockets;

public class MyNetworkStream_Sub_Class : NetworkStream
{

    public MyNetworkStream_Sub_Class(Socket socket, bool ownsSocket) :
        base(socket, ownsSocket)
    {
    }
    // You can use the Socket method to examine the underlying Socket.
    public bool IsConnected
    {
        get
        {
            return this.Socket.Connected;
        }
    }

    public bool CanCommunicate
    {
        get
        {
            if (!this.Readable | !this.Writeable)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }

注解

派生自 NetworkStream 的类可以使用此属性获取基础 Socket。 如果需要超出NetworkStream所提供的访问权限,请使用从 Socket 属性返回的基础Socket

备注

只能通过此类或派生类访问此属性。

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1