简体   繁体   中英

How to create named pipe (mkfifo) with .net6 in linux?

I want to create a named pipe ("mkfifo") with .net6 in Linux.

Using the class NamedPipeServerStream doesn't help me as it creates a socket file instead of a pipe.

This creates a socket:

var notAPipeButsocket = new NamedPipeServerStream("/tmp/my.notpipe", PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
        notAPipeButsocket.WaitForConnection();

It seems Microsoft chose to implement "NamedPipeServerStream" explicitly with sockets in linux: Why not use Unix Domain Sockets for Named Pipes?

How to create a real named pipe file with .net6 in Linux and write in it?

Context: I want to open the pipe with WireShark.

The only thing I found how to create a named pipe (mkfifo) from .net6 so far is with Mono:

https://github.com/dotnet/runtime/issues/24390#issuecomment-384650120

You can use the Mono.Posix.NetStandard library on .NET Core to get access to the mkfifo POSIX command. This will allow your program to read/write to a FIFO/Unix named pipe.

https://github.com/mono/mono/blob/47187bbc9b552f6ca5b2d80a2be6c7395b40da9e/mcs/class/Mono.Posix/Mono.Unix.Native/Syscall.cs#L4013-L4017

To write in a named pipe, you can simply use FileStream like this:

using FileStream fs = File.OpenWrite(namedPipePath);
fs.WriteByte(134);

Note, that this call will block until someone else will start reading it (another process, or whoever).

In my case it was enough to write into the pipe without creating it in .net6 because I could create it terminal via mkfifo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM