Export (0) Print
Expand All

NetworkStream.Socket Property

Gets the underlying Socket.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

protected Socket Socket { get; }

Property Value

Type: System.Net.Sockets.Socket
A Socket that represents the underlying network connection.

Classes deriving from NetworkStream can use this property to get the underlying Socket. Use the underlying Socket returned from the Socket property if you require access beyond that which NetworkStream provides.

NoteNote:

This property is accessible only through this class or a derived class.

The following code example retrieves the underlying Socket to verify an active connection.

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;
            }
        }
    }
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;


__gc class MyNetworkStream_Sub_Class : public NetworkStream {
public:
    MyNetworkStream_Sub_Class(System::Net::Sockets::Socket* socket, bool ownsSocket) : NetworkStream(socket, ownsSocket) {}

    // You can use the Socket method to examine the underlying Socket.
    __property bool get_Connected() 
    {
        return this->Socket->Connected;
    }

    __property bool get_CanCommunicate() 
    {
        if (!this->Readable | !this->Writeable) 
        {
            return false;
        } else 
        {
            return true;
        }
    }

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft