NamedPipeClientStream.Connect 方法

定义

连接到等待的服务器。Connects to a waiting server.

重载

Connect()

连接到具有无限超时值的等待的服务器。Connects to a waiting server with an infinite time-out value.

Connect(Int32)

在指定的超时期间内连接到等待的服务器。Connects to a waiting server within the specified time-out period.

Connect()

连接到具有无限超时值的等待的服务器。Connects to a waiting server with an infinite time-out value.

public:
 void Connect();
public void Connect ();
member this.Connect : unit -> unit
Public Sub Connect ()

例外

已连接客户端。The client is already connected.

示例

下面的示例演示了使用命名管道将字符串从父进程发送到子进程的方法。The following example demonstrates a method to send a string from a parent process to a child process using named pipes. 此示例在子进程中创建一个 NamedPipeClientStream 对象,该对象随后连接到本地计算机上的一个管道。This example creates a NamedPipeClientStream object in a child process, which then connects to a pipe on the local computer. 服务器示例可在 NamedPipeServerStream 类中查看。The server example can be seen in the NamedPipeServerStream class. 此示例是为 NamedPipeServerStreamNamedPipeClientStream 类提供的更大示例的一部分。This example is part of a larger example provided for the NamedPipeServerStream and NamedPipeClientStream classes.

using System;
using System.IO;
using System.IO.Pipes;

class PipeClient
{
    static void Main(string[] args)
    {
        using (NamedPipeClientStream pipeClient =
            new NamedPipeClientStream(".", "testpipe", PipeDirection.In))
        {

            // Connect to the pipe or wait until the pipe is available.
            Console.Write("Attempting to connect to pipe...");
            pipeClient.Connect();

            Console.WriteLine("Connected to pipe.");
            Console.WriteLine("There are currently {0} pipe server instances open.",
               pipeClient.NumberOfServerInstances);
            using (StreamReader sr = new StreamReader(pipeClient))
            {
                // Display the read text to the console
                string temp;
                while ((temp = sr.ReadLine()) != null)
                {
                    Console.WriteLine("Received from server: {0}", temp);
                }
            }
        }
        Console.Write("Press Enter to continue...");
        Console.ReadLine();
    }
}
Imports System.IO
Imports System.IO.Pipes
Imports System.Security.Principal

Class PipeClient

    Shared Sub Main(ByVal args As String())

        Dim pipeClient As New NamedPipeClientStream("localhost", _
                    "testpipe", PipeDirection.In, PipeOptions.None)

        ' Connect to the pipe or wait until the pipe is available.
        Console.WriteLine("Attempting to connect to the pipe...")
        pipeClient.Connect()

        Console.WriteLine("Connect to the pipe.")
        Console.WriteLine("There are currently {0} pipe server instances open.", _
                          pipeClient.NumberOfServerInstances)

        Dim sr As New StreamReader(pipeClient)
        Dim temp As String

        temp = sr.ReadLine()
        While Not temp Is Nothing
            Console.WriteLine("Received from server: {0}", temp)
            temp = sr.ReadLine()
        End While
        Console.Write("Press Enter to continue...")
        Console.ReadLine()
    End Sub
End Class

注解

此方法使用无限超时值调用 Connect(Int32) 方法。This method calls the Connect(Int32) method with an infinite time-out value.

此方法等待管道实例变为可用。This method waits for a pipe instance to become available. Connect 可能会在从 NamedPipeServerStream 对象调用 WaitForConnection 之前返回,但在 Connect 返回之前,WaitForConnection 将不会返回。Connect might return before WaitForConnection is called from the NamedPipeServerStream object, but WaitForConnection will not return until Connect has returned.

在连接 NamedPipeClientStream 对象之后但在服务器调用 WaitForConnection之前写入管道的任何数据都可供服务器在调用 WaitForConnection后使用。Any data written to the pipe after a NamedPipeClientStream object has connected, but before the server has called WaitForConnection, will be available to the server following the call to WaitForConnection.

Connect(Int32)

在指定的超时期间内连接到等待的服务器。Connects to a waiting server within the specified time-out period.

public:
 void Connect(int timeout);
public void Connect (int timeout);
[System.Security.SecurityCritical]
public void Connect (int timeout);
member this.Connect : int -> unit
Public Sub Connect (timeout As Integer)

参数

timeout
Int32

在连接超时之前等待服务器响应的毫秒数。The number of milliseconds to wait for the server to respond before the connection times out.

属性

例外

未能在指定的 timeout 时期内连接到服务器。Could not connect to the server within the specified timeout period.

timeout 小于 0 且未设置为 Infinitetimeout is less than 0 and not set to Infinite.

已连接客户端。The client is already connected.

服务器连接至其他客户端并且超时期间已过期。The server is connected to another client and the time-out period has expired.

注解

此方法等待管道实例变为可用。This method waits for a pipe instance to become available. Connect 可能会在从 NamedPipeServerStream调用 WaitForConnection 之前返回,但在 Connect 返回之前,WaitForConnection 将不会返回。Connect might return before WaitForConnection is called from the NamedPipeServerStream, but WaitForConnection will not return until Connect has returned. timeout 参数设置为 Infinite 以指定无限超时值。You set the timeout parameter to Infinite to specify an infinite time-out value.

在连接 NamedPipeClientStream 对象之后但在服务器调用 WaitForConnection之前写入管道的任何数据都可供服务器在调用 WaitForConnection后使用。Any data written to the pipe after a NamedPipeClientStream object has connected, but before the server has called WaitForConnection, will be available to the server following the call to WaitForConnection.

适用于