指定したローカル エンドポイントを使用して、TcpListener クラスの新しいインスタンスを初期化します。
名前空間: System.Net.Sockets
アセンブリ: System (system.dll 内)
構文
'宣言
Public Sub New ( _
localEP As IPEndPoint _
)
'使用
Dim localEP As IPEndPoint
Dim instance As New TcpListener(localEP)
public TcpListener (
IPEndPoint localEP
)
public:
TcpListener (
IPEndPoint^ localEP
)
public TcpListener (
IPEndPoint localEP
)
public function TcpListener (
localEP : IPEndPoint
)
パラメータ
- localEP
リスナ Socket のバインド先のローカル エンドポイントを表す IPEndPoint。
例外
例外の種類 | 条件 |
---|---|
localEP が null 参照 (Visual Basic では Nothing) です。 |
解説
このコンストラクタを使用すると、受信接続の試行を待機するローカル IP アドレスおよびポート番号を指定できます。このコンストラクタを使用する前に、目的のローカル IP アドレスおよびポート番号を使用して IPEndPoint を作成しておく必要があります。このとき、IPEndPoint を localEP パラメータとしてコンストラクタに渡します。
どのローカル アドレスが割り当てられていてもかまわない場合は、IPAddress.Any をアドレス パラメータとして使用して IPEndPoint を作成します。すると、基になるサービス プロバイダが最も適切なローカル ネットワーク アドレスを割り当てます。複数のネットワーク インターフェイスがある場合は、これを使用することによってアプリケーションを簡素化できることがあります。使用するローカル ポートについても特に指定がない場合は、ポート番号 0 を使用して IPEndPoint を作成します。この場合、サービス プロバイダは 1024 ~ 5000 の範囲で使用できるポート番号を割り当てます。この方法を使用する場合、LocalEndpoint プロパティを使用することによって、既に割り当てられているローカル ネットワーク アドレスとポート番号を知ることができます。
Start メソッドを呼び出して、受信接続試行の待機を開始します。
注意
このメンバは、アプリケーションでネットワーク トレースが有効にされている場合にトレース情報を出力します。詳細については、「ネットワークのトレース」を参照してください。
使用例
ローカル エンドポイントを使用して、TcpListener クラスのインスタンスを作成するコード例を次に示します。
'Creates an instance of the TcpListener class by providing a local endpoint.
Dim ipAddress As IPAddress = Dns.Resolve(Dns.GetHostName()).AddressList(0)
Dim ipLocalEndPoint As New IPEndPoint(ipAddress, 11000)
Try
Dim tcpListener As New TcpListener(ipLocalEndPoint)
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
//Creates an instance of the TcpListener class by providing a local endpoint.
IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);
try{
TcpListener tcpListener = new TcpListener(ipLocalEndPoint);
}
catch ( Exception e ){
Console.WriteLine( e.ToString());
}
//Creates an instance of the TcpListener class by providing a local endpoint.
IPAddress^ ipAddress = Dns::Resolve( Dns::GetHostName() )->AddressList[ 0 ];
IPEndPoint^ ipLocalEndPoint = gcnew IPEndPoint( ipAddress,11000 );
try
{
TcpListener^ tcpListener = gcnew TcpListener( ipLocalEndPoint );
}
catch ( Exception^ e )
{
Console::WriteLine( e->ToString() );
}
// Creates an instance of the TcpListener class by providing a
// local endpoint.
IPAddress ipAddress = (IPAddress)Dns.Resolve(
Dns.GetHostName()).get_AddressList().get_Item(0);
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);
try {
TcpListener tcpListener = new TcpListener(ipLocalEndPoint);
}
catch (System.Exception e) {
Console.WriteLine(e.ToString());
}
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
.NET Framework
サポート対象 : 2.0、1.1、1.0
.NET Compact Framework
サポート対象 : 2.0、1.0
参照
関連項目
TcpListener クラス
TcpListener メンバ
System.Net.Sockets 名前空間
IPEndPoint クラス
Start