NamedPipeServerStream在单(NamedPipeServerStream in M

2019-10-17 20:36发布

我想C#.NET项目转换为单声道。 NamedPipeServerStream&NamedPipeClientStream类没有在单声道支持和投掷未经处理过的异常。 你能帮我的人?

码:

class Program
{
    static string _PipeName = "testPipe";
    static void Main(string[] args)
    {

        ListenForPipeServer();
    }

    static void ListenForPipeServer()
    {
        using (NamedPipeServerStream pipeServer = new NamedPipeServerStream(
                          _PipeName,
                          PipeDirection.InOut,
                          2,
                          PipeTransmissionMode.Message,
                          PipeOptions.None

                          ))
        {
            StreamReader sr = new StreamReader(pipeServer);
            StreamWriter sw = new StreamWriter(pipeServer);


            do
            {
                try
                {
                    pipeServer.WaitForConnection();
                    sw.WriteLine("Testing");
                    sw.Flush();
                    pipeServer.WaitForPipeDrain();
                    Console.WriteLine(sr.ReadLine());

                }
                catch (Exception)
                {

                    throw;
                }
                finally
                {

                    pipeServer.WaitForPipeDrain();
                    if (pipeServer.IsConnected) pipeServer.Disconnect();
                }

            } while (true);

        }
    }



}

未处理的异常:

System.IO.IOException:创建命名管道错误:错误代码-1
在System.IO.Pipes.UnixNamedPipe.EnsureTargetFile(System.String名)[0x0001f]在/用户/助洗剂/数据/通道/单MAC-UI-刷新-2-10 / 2baeee2f /源极/ bockbuild /简档/单-2-10 /积聚根/单2.10.11 / _build /单2.10.11.git / MCS /类/ System.Core程序/ System.IO.Pipes / PipeUnix.cs:131 System.IO.Pipes .UnixNamedPipeServer..ctor(System.IO.Pipes.NamedPipeServerStream所有者,System.String pipeName,的Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode,PipeAccessRights权利PipeOptions选项的Int32 inBufferSize,的Int32 outBufferSize,HandleInheritability可继承)[0x00012]在/用户/助洗剂/数据/通道/单MAC-UI-刷新-2-10 / 2baeee2f /源极/ bockbuild /简档/单2-10 /建立根/单2.10.11 / _build /单2.10.11.git / MCS /类/ System.Core程序/ System.IO.Pipes / PipeUnix.cs:265在System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName,PipeDirection方向的Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode,PipeOptions选项,INT3 2 inBufferSize,的Int32 outBufferSize,System.IO.Pipes.PipeSecurity pipeSecurity,HandleInheritability可继承,PipeAccessRights additionalAccessRights)[0x0004b]在/用户/助洗剂/数据/通道/单MAC-UI-刷新-2-10 / 2baeee2f /源极/ bockbuild /简档/单2-10 /积聚根/单2.10.11 / _build /单2.10.11.git / MCS /类/ System.Core程序/ System.IO.Pipes / NamedPipeServerStream.cs:100在System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName,PipeDirection方向的Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode,PipeOptions选项的Int32 inBufferSize,的Int32 outBufferSize,System.IO.Pipes.PipeSecurity pipeSecurity,HandleInheritability可继承)[0x00000]在:0在System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName,PipeDirection方向的Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode,PipeOptions选项的Int32 inBufferSize,的Int32 outBufferSize,System.IO.Pipes.PipeSecurity pipeSecurity)[0x00000]在:0 在System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName,PipeDirection方向的Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode,PipeOptions选项的Int32 inBufferSize,的Int32 outBufferSize)[0x00000]中:0在System.IO.Pipes.NamedPipeServerStream。 .ctor(System.String pipeName,PipeDirection方向的Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode,PipeOptions选项)[0x00000]中:在0(包装远程-调用与 - 检查)System.IO.Pipes.NamedPipeServerStream:.ctor(字符串, System.IO.Pipes.PipeDirection,INT,System.IO.Pipes.PipeTransmissionMode,System.IO.Pipes.PipeOptions)在ConsoleTestPipeApp1.Program.ListenForPipeServer()[0x00000]在/用户/瓦代夫卢/项目/ Service_Test / ConsoleTestPipeApp1 / ConsoleTestPipeApp1 /Program.cs:21在ConsoleTestPipeApp1.Program.Main(System.String []参数)[0x00000]在/Users/Vadivelu/Project/Service_Test/ConsoleTestPipeApp1/ConsoleTestPipeApp1/Program.cs:16 [ERROR]致命未处理EXCEPTI ON信息:System.IO.IOException:创建命名管道错误:错误代码-1在System.IO.Pipes.UnixNamedPipe.EnsureTargetFile(System.String名)[0x0001f]在/用户/助洗剂/数据/通道/单MAC -ui刷新-2-10 / 2baeee2f /源极/ bockbuild /简档/单2-10 /积聚根/单2.10.11 / _build /单2.10.11.git / MCS /类/ System.Core程序/System.IO.Pipes/PipeUnix.cs:131在System.IO.Pipes.UnixNamedPipeServer..ctor(System.IO.Pipes.NamedPipeServerStream所有者,System.String pipeName,的Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode,PipeAccessRights权利,PipeOptions选项, INT32 inBufferSize,INT32 outBufferSize,HandleInheritability可继承)[0x00012]在/用户/助洗剂/数据/通道/单MAC-UI-刷新-2-10 / 2baeee2f /源极/ bockbuild /简档/单2-10 /建设 - 根/单2.10.11 / _build /单2.10.11.git / MCS /类/ System.Core程序/ System.IO.Pipes / PipeUnix.cs:265在System.IO.Pipes.NamedPipeServerStream..ctor(系统.String pipeName,PipeDirection方向,的Int32 maxNumberOfServerInst ances,PipeTransmissionMode transmissionMode,PipeOptions选项的Int32 inBufferSize,的Int32 outBufferSize,System.IO.Pipes.PipeSecurity pipeSecurity,HandleInheritability可继承,PipeAccessRights additionalAccessRights)[0x0004b]在/用户/助洗剂/数据/通道/单MAC-UI-茶点2-10 / 2baeee2f /源极/ bockbuild /简档/单2-10 /积聚根/单2.10.11 / _build /单2.10.11.git / MCS /类/ System.Core程序/ System.IO。管/ NamedPipeServerStream.cs:100在System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName,PipeDirection方向的Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode,PipeOptions选项的Int32 inBufferSize,的Int32 outBufferSize,System.IO.Pipes.PipeSecurity pipeSecurity ,HandleInheritability可继承)[0x00000]中:0在System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName,PipeDirection方向的Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode,PipeOptions选项的Int32 inBufferSize,OU的Int32 tBufferSize,System.IO.Pipes.PipeSecurity pipeSecurity)[0x00000]中:0在System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName,PipeDirection方向的Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode,PipeOptions选项的Int32 inBufferSize,的Int32 outBufferSize)[0x00000]中:0在System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName,PipeDirection方向的Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode,PipeOptions选项)[0x00000]中:在0(包装远程-invoke-与检查)System.IO.Pipes.NamedPipeServerStream:.ctor(字符串,System.IO.Pipes.PipeDirection,INT,System.IO.Pipes.PipeTransmissionMode,System.IO.Pipes.PipeOptions)在ConsoleTestPipeApp1.Program.ListenForPipeServer( )[0x00000]在/Users/Vadivelu/Project/Service_Test/ConsoleTestPipeApp1/ConsoleTestPipeApp1/Program.cs:21在ConsoleTestPipeApp1.Program.Main(System.String []参数)[0x00000]在/用户/瓦代夫卢/项目/ Service_Test / ConsoleTestPip eApp1 / ConsoleTestPipeApp1 / Program.cs中:16

Answer 1:

唯一的问题是,一个名为“testPipe”您正在使用不存在时,C#代码访问管道管。 如果你第一次运行该命令mkfifo testPipe (在UNIX上),然后运行下面的C#代码,它应该工作。



文章来源: NamedPipeServerStream in Mono