次の方法で共有


NetworkStream.Socket プロパティ

基になる Socket を取得します。

Protected ReadOnly Property Socket As Socket
[C#]
protected Socket Socket {get;}
[C++]
protected: __property Socket* get_Socket();
[JScript]
protected function get Socket() : Socket;

プロパティ値

基になるネットワーク接続を表す Socket

解説

NetworkStream の派生クラスは、このプロパティを使用して基になる Socket を取得します。 NetworkStream が提供する以上のアクセスが必要な場合は、 Socket プロパティが返した、基になる Socket を使用します。

メモ   このプロパティには、このクラスまたは派生クラスからでなければアクセスできません。

使用例

[Visual Basic, C#, C++] 基になる Socket を取得してアクティブな接続を確認する例を次に示します。

 
Public Class MyNetworkStream_Sub_Class
   Inherits NetworkStream
   
   
   Public Sub New(socket As Socket, ownsSocket As Boolean)
      MyBase.New(socket, ownsSocket)
   End Sub 'New
   
   ' Suppose you wanted a property for determining if Socket is connected. You can use
   ' the protected method 'Socket' to return underlying Socket.
   
   Public ReadOnly Property Connected() As Boolean
      Get
         Return Me.Socket.Connected
      End Get
   End Property
   
   ' You could also use public NetworkStream methods 'CanRead' and 'CanWrite'.
   
   Public ReadOnly Property CanCommunicate() As Boolean
      Get
         If Not Me.Readable Or Not Me.Writeable  Then
            Return False
         Else
            Return True
         End If
      End Get
   End Property
    
   Public Shared Sub DoSomethingSignificant()
   End Sub 'DoSomethingSignificant
    ' Do something significant in here
   

[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 Connected
    {
        get
        {
            return this.Socket.Connected;
        } 
    }
 
    public bool CanCommunicate
    {
        get
        {
            if (!this.Readable | !this.Writeable)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }

[C++] 
#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;
        }
    }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

NetworkStream クラス | NetworkStream メンバ | System.Net.Sockets 名前空間