指定されたリモート ネットワーク エンドポイントを使用してリモート TCP ホストにクライアントを接続します。
Overloads Public Sub Connect( _
ByVal remoteEP As IPEndPoint _)
[C#]
public void Connect(IPEndPointremoteEP);
[C++]
public: void Connect(IPEndPoint* remoteEP);
[JScript]
public function Connect(
remoteEP : IPEndPoint);
パラメータ
- remoteEP
接続先の IPEndPoint 。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | remoteEp が null 参照 (Visual Basic では Nothing) です。 |
SocketException | ソケットへのアクセス中にエラーが発生しました。詳細については「解説」を参照してください。 |
ObjectDisposedException | TcpClient が閉じています。 |
解説
このメソッドを呼び出して、指定した IPEndPoint への同期リモート ホスト接続を確立します。 Connect を呼び出す前に、IP アドレスとポート番号を使用して IPEndPoint クラスのインスタンスを作成しておく必要があります。この IPEndPoint を remoteEP パラメータとして使用します。 Connect メソッドは、接続するか失敗するまでブロックします。リモート ホストに接続した後、 GetStream メソッドを使用して、基になっている NetworkStream を取得します。この NetworkStream を使用して、データを送受信します。
メモ SocketException が発生した場合は、 SocketException.ErrorCode を使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのマニュアルから、エラーの詳細情報を確認できます。これは MSDN から入手できます。
使用例
[Visual Basic, C#, C++] IPEndPoint を使用してリモート ホストと接続する例を次に示します。
'Uses a remote end point to establish a socket connection.
Dim tcpClient As New TcpClient
Dim ipAddress As IPAddress = Dns.Resolve("www.contoso.com").AddressList(0)
Dim ipEndPoint As New IPEndPoint(ipAddress, 11004)
tcpClient.Connect(ipEndPoint)
[C#]
//Uses a remote end point to establish a socket connection.
TcpClient tcpClient = new TcpClient ();
IPAddress ipAddress = Dns.Resolve ("www.contoso.com").AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint (ipAddress, 11004);
tcpClient.Connect (ipEndPoint);
[C++]
//Uses a remote end point to establish a socket connection.
TcpClient* tcpClient = new TcpClient();
IPAddress* ipAddress = Dns::Resolve(S"www.contoso.com")->AddressList[0];
IPEndPoint* ipEndPoint = new IPEndPoint(ipAddress, 11004);
tcpClient->Connect(ipEndPoint);
[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 ファミリ, .NET Compact Framework - Windows CE .NET
参照
TcpClient クラス | TcpClient メンバ | System.Net.Sockets 名前空間 | TcpClient.Connect オーバーロードの一覧 | IPEndPoint | GetStream