■ 명명 파이프(Named Pipe) 사용하기

----------------------------------------------------------------------------------------------------


TestSolution.zip


[TestServer 프로젝트]

 

Program.cs

 

 

using System;

using System.IO;

using System.IO.Pipes;

 

namespace TestServer

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            WriteLog("PRESS ANY KEY TO START SERVER", true);

 

            Console.ReadKey(true);

 

            string message = null;

 

            WriteLog("WAIT FOR CONNECTION...", true);

 

            using(NamedPipeServerStream stream = new NamedPipeServerStream("458B7ED9-1BF9-4FAE-A91C-714678F124C4", PipeDirection.In))

            {

                stream.WaitForConnection();

 

                StreamReader reader = new StreamReader(stream);

 

                message = reader.ReadToEnd();

            }

 

            WriteLog("MESSAGE RECEIVED :", true);

            WriteLog("----------------------------------------", false);

            WriteLog(message, false);

            WriteLog("----------------------------------------", false);

        }

 

        #endregion

        #region 로그 작성하기 - WriteLog(format, printTimeStamp, parameterArray)

 

        /// <summary>

        /// 로그 작성하기

        /// </summary>

        /// <param name="format">포맷 문자열</param>

        /// <param name="printTimeStamp">타임 스탬프 출력 여부</param>

        /// <param name="parameterArray">매개 변수 배열</param>

        private static void WriteLog(string format, bool printTimeStamp, params object[] parameterArray)

        {

            string message;

 

            if(parameterArray.Length == 0)

            {

                message = format;

            }

            else

            {

                message = string.Format(format, parameterArray);

            }

 

            string log;

 

            if(printTimeStamp)

            {

                log = string.Format("[{0}] {1}", DateTime.Now.ToString("HH:mm:ss"), message);

            }

            else

            {

                log = string.Format("{0}", message);

            }

 

            Console.WriteLine(log);

        }

 

        #endregion

    }

}

 

 

[TestClient 프로젝트]

 

Program.cs

 

 

using System;

using System.IO;

using System.IO.Pipes;

 

namespace TestClient

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            WriteLog("PRESS ANY KEY TO CONNECT SERVER", true);

 

            Console.ReadKey(true);

 

            using(NamedPipeClientStream stream = new NamedPipeClientStream(".", "458B7ED9-1BF9-4FAE-A91C-714678F124C4", PipeDirection.Out,

                PipeOptions.None))

            {

                if(!stream.IsConnected)

                {

                    stream.Connect(1000);

 

                    WriteLog("SERVER CONNECTED...", true);

                }

 

                StreamWriter writer = new StreamWriter(stream);

 

                for(int i = 0; i < 1000; i++)

                {

                    writer.WriteLine("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

                }

 

                writer.Flush();

            }

        }

 

        #endregion

        #region 로그 작성하기 - WriteLog(format, printTimeStamp, parameterArray)

 

        /// <summary>

        /// 로그 작성하기

        /// </summary>

        /// <param name="format">포맷 문자열</param>

        /// <param name="printTimeStamp">타임 스탬프 출력 여부</param>

        /// <param name="parameterArray">매개 변수 배열</param>

        private static void WriteLog(string format, bool printTimeStamp, params object[] parameterArray)

        {

            string message;

 

            if(parameterArray.Length == 0)

            {

                message = format;

            }

            else

            {

                message = string.Format(format, parameterArray);

            }

 

            string log;

 

            if(printTimeStamp)

            {

                log = string.Format("[{0}] {1}", DateTime.Now.ToString("HH:mm:ss"), message);

            }

            else

            {

                log = string.Format("{0}", message);

            }

 

            Console.WriteLine(log);

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요