PipeStream.Read Yöntem

Tanım

Aşırı Yüklemeler

Read(Span<Byte>)

Geçerli akıştan bir bayt dizisi okur, bunları bir bayt dizisine yazar ve okunan bayt sayısı kadar akış içindeki konumu ilerletir.

Read(Byte[], Int32, Int32)

Bir akıştan bir bayt bloğu okur ve verileri belirtilen bir uzunlukta belirtilen konumdan başlayarak belirtilen arabelleğe yazar.

Read(Span<Byte>)

Kaynak:
PipeStream.Unix.cs
Kaynak:
PipeStream.Unix.cs
Kaynak:
PipeStream.Unix.cs

Geçerli akıştan bir bayt dizisi okur, bunları bir bayt dizisine yazar ve okunan bayt sayısı kadar akış içindeki konumu ilerletir.

public:
 override int Read(Span<System::Byte> buffer);
public override int Read (Span<byte> buffer);
override this.Read : Span<byte> -> int
Public Overrides Function Read (buffer As Span(Of Byte)) As Integer

Parametreler

buffer
Span<Byte>

Bellek bölgesi. Bu yöntem döndürdüğünde, bu bölgenin içeriği geçerli kaynaktan okunan bayt ile değiştirilir.

Döndürülenler

içine okunan buffertoplam bayt sayısı. Bu, şu anda kullanılabilir sayıda bayt yoksa içinde buffer ayrılan bayt sayısından az veya akışın sonuna ulaşıldıysa sıfır (0) olabilir.

Özel durumlar

Okunan bayt sayısı arabellek uzunluğundan uzundu.

Akış okumayı desteklemiyor.

Kapalı kanala erişilemiyor.

Boru henüz bağlanmadı.

-veya-

Borunun bağlantısı kesilmiş durumda.

-veya-

Boru tutamacı ayarlanmadı. (Uygulamanız PipeStream çağrı InitializeHandle(SafePipeHandle, Boolean, Boolean)yaptı mı?

Açıklamalar

Geçerli PipeStream nesnenin CanRead okuma işlemlerini destekleyip desteklemediğini belirlemek için özelliğini kullanın.

ReadAsync Geçerli akıştan zaman uyumsuz olarak okumak için yöntemini kullanın.

Bu yöntem geçerli akıştan en fazla buffer.Length bayt sayısını okur ve içinde bufferdepolar. Akış içindeki geçerli konum, okunan bayt sayısıyla gelişmiştir; ancak bir özel durum oluşursa akış içindeki geçerli konum değişmeden kalır.

Bu yöntem, kullanılabilir veri olmaması durumunda en az bir bayt veri okunana kadar engeller.

Bu yöntem yalnızca akışta daha fazla veri olmadığında ve artık beklenmezse (kapalı yuva veya dosya sonu gibi) 0 döndürür.

Bu yöntem, akışın sonuna ulaşılmasa bile istenenden daha az bayt döndürmek için ücretsizdir.

Temel veri türlerini okumak için kullanın BinaryReader .

Şunlara uygulanır

Read(Byte[], Int32, Int32)

Kaynak:
PipeStream.Unix.cs
Kaynak:
PipeStream.Unix.cs
Kaynak:
PipeStream.Unix.cs

Bir akıştan bir bayt bloğu okur ve verileri belirtilen bir uzunlukta belirtilen konumdan başlayarak belirtilen arabelleğe yazar.

public:
 override int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public override int Read (byte[] buffer, int offset, int count);
[System.Security.SecurityCritical]
public override int Read (byte[] buffer, int offset, int count);
override this.Read : byte[] * int * int -> int
[<System.Security.SecurityCritical>]
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer

Parametreler

buffer
Byte[]

Bu yöntem döndürdüğünde, ve (offset + count - 1) arasındaki offset değerlerin geçerli kaynaktan okunan baytlarla değiştirdiği belirtilen bayt dizisini içerir.

offset
Int32

Okunan baytların yerleştirileceği dizideki buffer bayt uzaklığı.

count
Int32

Okunacak bayt sayısı üst sınırı.

Döndürülenler

olarak okunan buffertoplam bayt sayısı. Bu, şu anda kullanılabilir durumda değilse istenen bayt sayısından az veya akışın sonuna ulaşılırsa 0 olabilir.

Öznitelikler

Özel durumlar

buffer, null değeridir.

offset 0'dan küçüktür.

-veya-

count 0'dan küçüktür.

count içindeki kullanılabilir bufferbayt sayısından büyük.

Boru kapalı.

Kanal okuma işlemlerini desteklemiyor.

Borunun bağlantısı kesildi, bağlanmayı bekliyor veya tutamaç ayarlanmadı.

G/Ç hatası oluştu.

Örnekler

Aşağıdaki örnek anonim bir kanal istemcisi ve kanal sunucusu oluşturur. Kanal sunucusu, doğrulama kodu olarak kanal istemcisinden bir dizi bayt okumak için yöntemini kullanır Read . Hem kanal istemcisi hem de kanal sunucusu aynı örneğin bir parçasıdır. Örneğin sunucu bölümü bir istemci işlemi oluşturur ve bağımsız değişken olarak anonim kanal tutamacını geçirir.

#using <System.dll>
#using <System.Core.dll>

using namespace System;
using namespace System::IO;
using namespace System::IO::Pipes;
using namespace System::Diagnostics;

ref class PipeStreamExample
{
private:
    static array<Byte>^ matchSign = {9, 0, 9, 0};

public:
    static void Main()
    {
        array<String^>^ args = Environment::GetCommandLineArgs();
        if (args->Length < 2)
        {
            Process^ clientProcess = gcnew Process();

            clientProcess->StartInfo->FileName = Environment::CommandLine;

            AnonymousPipeServerStream^ pipeServer =
                gcnew AnonymousPipeServerStream(PipeDirection::In,
                HandleInheritability::Inheritable);
            // Pass the client process a handle to the server.
            clientProcess->StartInfo->Arguments = pipeServer->GetClientHandleAsString();
            clientProcess->StartInfo->UseShellExecute = false;
            Console::WriteLine("[SERVER] Starting client process...");
            clientProcess->Start();

            pipeServer->DisposeLocalCopyOfClientHandle();

            try
            {
                if (WaitForClientSign(pipeServer))
                {
                    Console::WriteLine("[SERVER] Valid sign code received!");
                }
                else
                {
                    Console::WriteLine("[SERVER] Invalid sign code received!");
                }
            }
            catch (IOException^ e)
            {
                 Console::WriteLine("[SERVER] Error: {0}", e->Message);
            }
            clientProcess->WaitForExit();
            clientProcess->Close();
            Console::WriteLine("[SERVER] Client quit. Server terminating.");
        }
        else
        {
            PipeStream^ pipeClient =
                gcnew AnonymousPipeClientStream(PipeDirection::Out, args[1]);
            try
            {
                Console::WriteLine("[CLIENT] Sending sign code...");
                SendClientSign(pipeClient);
            }
            catch (IOException^ e)
            {
                Console::WriteLine("[CLIENT] Error: {0}", e->Message);
            }
            Console::WriteLine("[CLIENT] Terminating.");
        }
    }

private:
    static bool WaitForClientSign(PipeStream^ inStream)
    {
        array<Byte>^ inSign = gcnew array<Byte>(matchSign->Length);
        int len = inStream->Read(inSign, 0, matchSign->Length);
        bool valid = len == matchSign->Length;

        while (valid && len-- > 0)
        {
            valid = valid && (matchSign[len] == inSign[len]);
        }
        return valid;
    }

    static void SendClientSign(PipeStream^ outStream)
    {
        outStream->Write(matchSign, 0, matchSign->Length);
    }
};

int main()
{
    PipeStreamExample::Main();
}
using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;

class PipeStreamExample
{
    private static byte[] matchSign = {9, 0, 9, 0};

    public static void Main()
    {
        string[] args = Environment.GetCommandLineArgs();
        if (args.Length < 2)
        {
            Process clientProcess = new Process();

            clientProcess.StartInfo.FileName = Environment.CommandLine;

            using (AnonymousPipeServerStream pipeServer =
                new AnonymousPipeServerStream(PipeDirection.In,
                HandleInheritability.Inheritable))
            {
                // Pass the client process a handle to the server.
                clientProcess.StartInfo.Arguments = pipeServer.GetClientHandleAsString();
                clientProcess.StartInfo.UseShellExecute = false;
                Console.WriteLine("[SERVER] Starting client process...");
                clientProcess.Start();

                pipeServer.DisposeLocalCopyOfClientHandle();

                try
                {
                    if (WaitForClientSign(pipeServer))
                    {
                        Console.WriteLine("[SERVER] Valid sign code received!");
                    }
                    else
                    {
                        Console.WriteLine("[SERVER] Invalid sign code received!");
                    }
                }
                catch (IOException e)
                {
                    Console.WriteLine("[SERVER] Error: {0}", e.Message);
                }
            }
            clientProcess.WaitForExit();
            clientProcess.Close();
            Console.WriteLine("[SERVER] Client quit. Server terminating.");
        }
        else
        {
            using (PipeStream pipeClient = new AnonymousPipeClientStream(PipeDirection.Out, args[1]))
            {
                try
                {
                    Console.WriteLine("[CLIENT] Sending sign code...");
                    SendClientSign(pipeClient);
                }
                catch (IOException e)
                {
                     Console.WriteLine("[CLIENT] Error: {0}", e.Message);
                }
            }
            Console.WriteLine("[CLIENT] Terminating.");
        }
    }

    private static bool WaitForClientSign(PipeStream inStream)
    {
        byte[] inSign = new byte[matchSign.Length];
        int len = inStream.Read(inSign, 0, matchSign.Length);
        bool valid = len == matchSign.Length;

        while (valid && len-- > 0)
        {
            valid = valid && (matchSign[len] == inSign[len]);
        }
        return valid;
    }

    private static void SendClientSign(PipeStream outStream)
    {
        outStream.Write(matchSign, 0, matchSign.Length);
    }
}
Imports System.IO
Imports System.IO.Pipes
Imports System.Diagnostics

Class PipeStreamExample
    Private Shared matchSign() As Byte = {9, 0, 9, 0}

    Public Shared Sub Main()
        Dim args() As String = Environment.GetCommandLineArgs()
        If args.Length < 2 Then
            Dim clientProcess As New Process()

            clientProcess.StartInfo.FileName = Environment.CommandLine

            Using pipeServer As New AnonymousPipeServerStream( _
                PipeDirection.In, HandleInheritability.Inheritable)

                ' Pass the client process a handle to the server.
                clientProcess.StartInfo.Arguments = pipeServer.GetClientHandleAsString()
                clientProcess.StartInfo.UseShellExecute = false
                Console.WriteLine("[SERVER] Starting client process...")
                clientProcess.Start()

                pipeServer.DisposeLocalCopyOfClientHandle()

                Try
                    If WaitForClientSign(pipeServer) Then
                        Console.WriteLine("[SERVER] Valid sign code received!")
                    Else
                        Console.WriteLine("[SERVER] Invalid sign code received!")
                    End If
                Catch e As IOException
                    Console.WriteLine("[SERVER] Error: {0}", e.Message)
                End Try
            End Using
            clientProcess.WaitForExit()
            clientProcess.Close()
            Console.WriteLine("[SERVER] Client quit. Server terminating.")
        Else
            Using pipeClient As PipeStream = New AnonymousPipeClientStream(PipeDirection.Out, args(1))
                Try
                    Console.WriteLine("[CLIENT] Sending sign code...")
                    SendClientSign(pipeClient)
                Catch e As IOException
                    Console.WriteLine("[CLIENT] Error: {0}", e.Message)
                End Try
            End Using
            Console.WriteLine("[CLIENT] Terminating.")
        End If
    End Sub

    Private Shared Function WaitForClientSign(inStream As PipeStream) As Boolean
        Dim inSign() As Byte = Array.CreateInstance(GetType(Byte), matchSign.Length)
        Dim len As Integer = inStream.Read(inSign, 0, matchSign.Length)
        Dim valid = len.Equals(matchSign.Length)

        While len > 0
            len -= 1
            valid = valid And (matchSign(len).Equals(inSign(len)))
        End While
        Return valid
    End Function

    Private Shared Sub SendClientSign(outStream As PipeStream)
        outStream.Write(matchSign, 0, matchSign.Length)
    End Sub
End Class

Açıklamalar

Geçerli PipeStream nesnenin CanRead okuma işlemlerini destekleyip desteklemediğini belirlemek için özelliğini kullanın.

Bayt okunana Read veya akışın sonuna ulaşılana kadar count yöntem bloklarını çağırma. Zaman uyumsuz okuma işlemleri için bkz BeginRead . ve EndRead.

Şunlara uygulanır