Ejemplo n.º 1
0
        /// <summary>
        /// Create a named pipe instance.
        /// </summary>
        /// <param name="pipeName">Local name (the part after \\.\pipe\)</param>
        public static NamedPipeStream Create(string pipeName, ServerMode mode)
        {
            IntPtr handle = IntPtr.Zero;
            string name   = @"\\.\pipe\" + pipeName;

            handle = CreateNamedPipe(
                name,
                (uint)mode,
                PIPE_TYPE_BYTE | PIPE_WAIT,
                PIPE_UNLIMITED_INSTANCES,
                0,    // outBuffer,
                1024, // inBuffer,             NMPWAIT_WAIT_FOREVER,
                IntPtr.Zero);
            if (handle.ToInt32() == INVALID_HANDLE_VALUE)
            {
                throw new Win32Exception("Error creating named pipe " + name + " . Internal error: " + Marshal.GetLastWin32Error().ToString());
            }
            // Set members persistently...
            NamedPipeStream self = new NamedPipeStream();

            self._handle = handle;
            switch (mode)
            {
            case ServerMode.InboundOnly:
                self._mode = FileAccess.Read;
                break;

            case ServerMode.OutboundOnly:
                self._mode = FileAccess.Write;
                break;

            case ServerMode.Bidirectional:
                self._mode = FileAccess.ReadWrite;
                break;
            }
            return(self);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Test / demo code:
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                /*
                 * Server-side sample code:
                 */
                NamedPipeStream stream = NamedPipeStream.Create("testpipe", NamedPipeStream.ServerMode.Bidirectional);
                StreamReader    sr     = new StreamReader(stream, Encoding.Unicode);
                Console.WriteLine("Listening...");
                do
                {
                    stream.Listen();
                    do
                    {
                        bool dataRead = false;
                        while (stream.DataAvailable)
                        {
                            try
                            {
                                string s = sr.ReadLine();
                                if (s != null && s.Length > 0)
                                {
                                    StreamWriter sw = new StreamWriter(stream, Encoding.Unicode);
                                    sw.WriteLine(s);
                                    sw.Flush();
                                    Console.WriteLine(s);
                                    dataRead = true;
                                }
                            }
                            catch { }
                        }
                        System.Threading.Thread.Sleep(dataRead ? 50 : 1000);
                    } while (stream.IsConnected);
                } while (true);
            }
            else
            {
                /*
                 * // Client side sample code:
                 */NamedPipeStream stream = new NamedPipeStream(@"\\.\pipe\testpipe", FileAccess.ReadWrite);

                StreamWriter sw = new StreamWriter(stream, Encoding.Unicode);

                sw.AutoFlush = true;

                for (int i = 0; i < 500; i++)
                {
                    sw.WriteLine("Hello world {0}", i);
                    System.Threading.Thread.Sleep(1000);
                    if (stream.DataAvailable)
                    {
                        StreamReader sr = new StreamReader(stream);
                        Console.WriteLine(sr.ReadLine());
                    }
                }

                sw.Write("Final line");

                sw.Close();
            }
        }
        /// <summary>
        /// Test / demo code:
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            if (args.Length == 0)
            {
            /*
            * Server-side sample code:
            */
            NamedPipeStream stream = NamedPipeStream.Create("testpipe", NamedPipeStream.ServerMode.Bidirectional);
            StreamReader sr = new StreamReader(stream,Encoding.Unicode);
            Console.WriteLine("Listening...");
            do
            {
            stream.Listen();
            do
            {
            bool dataRead = false;
            while (stream.DataAvailable)
            {
                try
                {
                string s = sr.ReadLine();
                if (s != null && s.Length > 0)
                {
                    StreamWriter sw = new StreamWriter(stream, Encoding.Unicode);
                    sw.WriteLine(s);
                    sw.Flush();
                    Console.WriteLine(s);
                    dataRead = true;
                }
                }
                catch { }
            }
            System.Threading.Thread.Sleep(dataRead ? 50 : 1000);

            } while (stream.IsConnected);

            } while (true);

            }
            else
            {
            /*
            // Client side sample code:
            */         NamedPipeStream stream = new NamedPipeStream(@"\\.\pipe\testpipe", FileAccess.ReadWrite);

            StreamWriter sw = new StreamWriter(stream, Encoding.Unicode);

            sw.AutoFlush = true;

            for (int i = 0; i < 500; i++)
            {
            sw.WriteLine("Hello world {0}", i);
            System.Threading.Thread.Sleep(1000);
            if (stream.DataAvailable)
            {
            StreamReader sr = new StreamReader(stream);
            Console.WriteLine(sr.ReadLine());
            }
            }

            sw.Write("Final line");

            sw.Close();
            }
        }
        /// <summary>
        /// Create a named pipe instance.
        /// </summary>
        /// <param name="pipeName">Local name (the part after \\.\pipe\)</param>
        public static NamedPipeStream Create(string pipeName, ServerMode mode)
        {
            IntPtr handle = IntPtr.Zero;
            string name = @"\\.\pipe\" + pipeName;

            handle = CreateNamedPipe(
            name,
            (uint)mode,
            PIPE_TYPE_BYTE | PIPE_WAIT,
            PIPE_UNLIMITED_INSTANCES,
            0, // outBuffer,
            1024, // inBuffer,             NMPWAIT_WAIT_FOREVER,
            IntPtr.Zero);
            if (handle.ToInt32() == INVALID_HANDLE_VALUE)
            {
            throw new Win32Exception("Error creating named pipe " + name + " . Internal error: " + Marshal.GetLastWin32Error().ToString());
            }
            // Set members persistently...
            NamedPipeStream self = new NamedPipeStream();
            self._handle = handle;
            switch (mode)
            {
            case ServerMode.InboundOnly:
            self._mode = FileAccess.Read;
            break;
            case ServerMode.OutboundOnly:
            self._mode = FileAccess.Write;
            break;
            case ServerMode.Bidirectional:
            self._mode = FileAccess.ReadWrite;
            break;
            }
            return self;
        }