NamedPipeClientStream.Connect Metod

Definition

Ansluter till en väntande server.

Överlagringar

Name Description
Connect()

Ansluter till en väntande server med ett oändligt timeout-värde.

Connect(Int32)

Ansluter till en väntande server inom den angivna tidsgränsen.

Connect()

Ansluter till en väntande server med ett oändligt timeout-värde.

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

Undantag

Klienten är redan ansluten.

Exempel

I följande exempel visas en metod för att skicka en sträng från en överordnad process till en underordnad process med hjälp av namngivna pipes. Det här exemplet skapar ett NamedPipeClientStream objekt i en underordnad process som sedan ansluter till ett rör på den lokala datorn. Serverexemplet kan visas i NamedPipeServerStream klassen. Det här exemplet är en del av ett större exempel som tillhandahålls för klasserna NamedPipeServerStream och NamedPipeClientStream .

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

Kommentarer

Den här metoden anropar Connect(Int32) metoden med ett oändligt timeout-värde.

Den här metoden väntar på att en pipe-instans ska bli tillgänglig. Connect kan returneras innan WaitForConnection anropas från objektet NamedPipeServerStream , men WaitForConnection returneras inte förrän Connect det har returnerats.

Alla data som skrivs till röret när ett NamedPipeClientStream objekt har anslutits, men innan servern har anropat WaitForConnection, kommer att vara tillgängliga för servern efter anropet till WaitForConnection.

Gäller för

Connect(Int32)

Ansluter till en väntande server inom den angivna tidsgränsen.

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

Parametrar

timeout
Int32

Antalet millisekunder som ska vänta tills servern svarar innan anslutningen överskrider tidsgränsen.

Attribut

Undantag

Det gick inte att ansluta till servern inom den angivna timeout perioden.

timeout är mindre än 0 och inte inställt på Infinite.

Klienten är redan ansluten.

Servern är ansluten till en annan klient och tidsgränsen har upphört att gälla.

Kommentarer

Den här metoden väntar på att en pipe-instans ska bli tillgänglig. Connect kan returneras innan WaitForConnection anropas från NamedPipeServerStream, men WaitForConnection returneras inte förrän Connect den har returnerats. Du anger parametern timeout till Infinite för att ange ett oändligt timeout-värde.

Alla data som skrivs till röret när ett NamedPipeClientStream objekt har anslutits, men innan servern har anropat WaitForConnection, kommer att vara tillgängliga för servern efter anropet till WaitForConnection.

Gäller för