Edit

Share via


NamedPipeClientStream.Connect Method

Definition

Connects to a waiting server.

Overloads

Connect(TimeSpan)

Connects to a waiting server within the specified timeout period.

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(TimeSpan)

Source:
NamedPipeClientStream.cs
Source:
NamedPipeClientStream.cs
Source:
NamedPipeClientStream.cs
Source:
NamedPipeClientStream.cs

Connects to a waiting server within the specified timeout period.

public void Connect(TimeSpan timeout);

Parameters

timeout
TimeSpan

The amount of time to wait for the server to respond before the connection times out.

Remarks

See Connect(Int32) remarks.

Applies to

.NET 10 and other versions
Product Versions
.NET 7, 8, 9, 10

Connect()

Source:
NamedPipeClientStream.cs
Source:
NamedPipeClientStream.cs
Source:
NamedPipeClientStream.cs
Source:
NamedPipeClientStream.cs

Connects to a waiting server with an infinite time-out value.

public void Connect();

Exceptions

The client is already connected.

Examples

The following example demonstrates a method to send a string from a parent process to a child process using named pipes. This example creates a NamedPipeClientStream object in a child process, which then connects to a pipe on the local computer. The server example can be seen in the NamedPipeServerStream class. 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();
    }
}

Remarks

This method calls the Connect(Int32) method with an infinite time-out value.

This method waits for a pipe instance to become available. Connect might return before WaitForConnection is called from the NamedPipeServerStream object, but WaitForConnection will not return until Connect has returned.

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.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Connect(Int32)

Source:
NamedPipeClientStream.cs
Source:
NamedPipeClientStream.cs
Source:
NamedPipeClientStream.cs
Source:
NamedPipeClientStream.cs

Connects to a waiting server within the specified time-out period.

public void Connect(int timeout);
[System.Security.SecurityCritical]
public void Connect(int timeout);

Parameters

timeout
Int32

The number of milliseconds to wait for the server to respond before the connection times out.

Attributes

Exceptions

Could not connect to the server within the specified timeout period.

timeout 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.

Remarks

This method waits for a pipe instance to become available. Connect might return before WaitForConnection is called from the NamedPipeServerStream, but WaitForConnection will not return until Connect has returned. You set the timeout parameter to Infinite to specify an infinite time-out value.

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.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1