Post

๐Ÿ’ป[C#]IPC ํ†ต์‹ ํƒ๊ตฌ (Named Pipe)-3

๐Ÿ“š ๊ฐœ๋ฐœ ํ™˜๊ฒฝ

  • View VisualStudio 2022
  • View .NET 6
  • View Console APP

๐Ÿ“‘ NamedPipeServerStream

NamedPipeServerStream ๋จผ์ € NamePipe์˜ Server ๋ถ€๋ถ„์˜ ์ƒ์„ฑ์ž๋ฅผ ๋ณด๋ฉด


1
public NamedPipeServerStream (string pipeName, System.IO.Pipes.PipeDirection direction);
  • string pipeName : ํŒŒ์ดํ”„์˜ ์ด๋ฆ„
  • System.IO.Pipes.PipeDirection direction : ํŒŒ์ดํ”„์˜ ๋ฐฉํ–ฅ


๋ฐฉํ–ฅ์˜ ์—ด๊ฑฐ ๊ฐ’์—๋Š” ์ด 3๊ฐ€์ง€๊ฐ€ ์žˆ์œผ๋ฉฐ

FieldDescription
InํŒŒ์ดํ”„ ๋ฐฉํ–ฅ์„ ์•ˆ์ชฝ์œผ๋กœ ์ง€์ •ํ•จ
InOutํŒŒ์ดํ”„ ๋ฐฉํ–ฅ์„ ์–‘์ชฝ์œผ๋กœ ์ง€์ •ํ•จ
OutํŒŒ์ดํ”„ ๋ฐฉํ–ฅ์„ ๋ฐ”๊นฅ์ชฝ์œผ๋กœ ์ง€์ •ํ•จ


์—ฌ๊ธฐ์„œ Server ์ชฝ์—๋Š” Out์„ ์“ธ๊ฒƒ์ด๋‹ค. ํ•ด๋‹น ํŒŒ์ดํ”„๊ฐ€ ์„œ๋ฒ„์—์„œ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด Out์œผ๋กœ ์ง€์ •ํ•˜๋ฉฐ ์“ฐ๊ธฐ์ „์šฉ์ด๊ธฐ๋„ ํ•˜๋‹ค.


๐Ÿ“‘ NamedPipeClientStream

NamedPipeClientStream

NamePipe ์˜ Cline ๋ถ€๋ถ„์˜ ์ƒ์„ฑ์ž๋ฅผ ์‚ดํŽด๋ณด๋ฉด

1
public NamedPipeClientStream (string serverName, string pipeName, System.IO.Pipes.PipeDirection direction);
  • string serverName : ์„œ๋ฒ„ ์ด๋ฆ„
  • string pipeName : ํŒŒ์ดํ”„์˜ ์ด๋ฆ„
  • System.IO.Pipes.PipeDirection direction : ํŒŒ์ดํ”„์˜ ๋ฐฉํ–ฅ

NamedPipeServerStream์˜ ์ƒ์„ฑ์ž์™€ ๋‹ค๋ฅด๊ฒŒ string serverName ๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ ์„œ๋ฒ„๊ฐ€ ๋กœ์ปฌ ์ปดํ“จํ„ฐ์— ์žˆ๋Š”๊ฒฝ์šฐ์—๋Š” โ€.โ€ ์œผ๋กœ ๋„ฃ์–ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค! string pipeName ์—๋Š” ์„œ๋ฒ„์™€ ๊ฐ™์€ ํŒŒ์ดํ”„์˜ ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•˜๋ฉฐ System.IO.Pipes.PipeDirection direction์€ In์œผ๋กœ ํŒŒ์ดํ”„๊ฐ€ ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ์„ค์ •์ด ๋˜๋ฉฐ ์ฝ๊ธฐ ์ „์šฉ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ–ฅ๏ธ Named Pipe ๊ตฌํ˜„

Naed Pipe Write Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class NamedPipeWriter
{
    public void WriteToPipe(string data)
    {
        using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("Cat", PipeDirection.Out))
        {
            Console.WriteLine("Named Pipe ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ๋ฅผ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค...");
            pipeServer.WaitForConnection();

            using (StreamWriter writer = new StreamWriter(pipeServer))
            {
                writer.AutoFlush = true;
                writer.WriteLine(data);
                Console.WriteLine("Named Pipe ์„œ๋ฒ„๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ–ˆ์Šต๋‹ˆ๋‹ค.");
            }
        }
    }
}

Named Pipe Read Client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class NamedPipeReader
{
    public string ReadFromPipe()
    {
        using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "Cat", PipeDirection.In))
        {
            Console.WriteLine("Named Pipe ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ์—ฐ๊ฒฐ ์ค‘...");
            pipeClient.Connect();

            using (StreamReader reader = new StreamReader(pipeClient))
            {
                string data = reader.ReadLine();
                Console.WriteLine("Named Pipe ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์‹ ํ–ˆ์Šต๋‹ˆ๋‹ค.");
                return data;
            }
        }
    }
}

์“ฐ๊ณ / ์ฝ๋Š” ํด๋ž˜์Šค๊ฐ€ ์žˆ์œผ๋‹ˆ ์ด์ œ ์‚ฌ์šฉํ•  ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ž๋ฉด

Program class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Program
{
    static void Main()
    {
        NamedPipeWriter writer = new NamedPipeWriter();
        NamedPipeReader reader = new NamedPipeReader();

        // ์ƒˆ๋กœ์šด ์Šค๋ ˆ๋“œ์—์„œ Named Pipe ์„œ๋ฒ„๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
        Task.Run(() =>
        {
            writer.WriteToPipe("Night Mo ๋ฐ์ดํ„ฐ ์ €์žฅ!");
        });

        // Named Pipe ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์Šต๋‹ˆ๋‹ค.
        string data = reader.ReadFromPipe();
        Console.WriteLine($"Received from pipe: {data}");
    }
}

๊ฒฐ๊ณผ

image

This post is licensed under CC BY 4.0 by the author.

ยฉ Gubeommo. Some rights reserved.

Using the Chirpy theme for Jekyll.