亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

c# processstartinfo怎樣管理進程間通信

c#
小樊
81
2024-11-23 00:40:15
欄目: 編程語言

在C#中,ProcessStartInfo類主要用于啟動和管理外部進程

  1. 使用命名管道(Named Pipes):命名管道是一種進程間通信(IPC)機制,允許一個進程與另一個進程通過共享內存進行數據傳輸。在C#中,可以使用System.IO.Pipes命名空間中的NamedPipeServerStreamNamedPipeClientStream類來創建和管理命名管道。

示例:創建一個簡單的命名管道服務器和客戶端

Server(服務器端):

using System;
using System.IO.Pipes;

class NamedPipeServer
{
    static void Main()
    {
        string pipeName = "TestPipe";
        using (NamedPipeServerStream serverStream = new NamedPipeServerStream(pipeName, PipeDirection.InOut))
        {
            Console.WriteLine("Waiting for client connection...");
            serverStream.WaitForConnection();

            // 發送數據到客戶端
            string message = "Hello from server!";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
            serverStream.Write(buffer, 0, buffer.Length);

            serverStream.Close();
        }
    }
}

Client(客戶端):

using System;
using System.IO.Pipes;

class NamedPipeClient
{
    static void Main()
    {
        string pipeName = "TestPipe";
        using (NamedPipeClientStream clientStream = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut))
        {
            Console.WriteLine("Connecting to server...");
            clientStream.Connect();

            // 從服務器接收數據
            byte[] buffer = new byte[256];
            int bytesRead = clientStream.Read(buffer, 0, buffer.Length);
            string message = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead);
            Console.WriteLine("Received from server: " + message);

            clientStream.Close();
        }
    }
}
  1. 使用共享內存(Shared Memory):共享內存是一種高效的進程間通信方法,允許多個進程訪問同一塊物理內存。在C#中,可以使用System.IO.MemoryMappedFiles命名空間中的MemoryMappedFileMemoryMappedViewAccessor類來創建和管理共享內存。

示例:創建一個簡單的共享內存服務器和客戶端

Server(服務器端):

using System;
using System.IO.MemoryMappedFiles;

class SharedMemoryServer
{
    static void Main()
    {
        string sharedMemoryName = "TestSharedMemory";
        using (MemoryMappedFile sharedMemory = MemoryMappedFile.CreateOrOpen(sharedMemoryName, 1024))
        {
            using (MemoryMappedViewAccessor accessor = sharedMemory.CreateViewAccessor())
            {
                Console.WriteLine("Writing to shared memory...");
                string message = "Hello from server!";
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
                accessor.Write(0, buffer, 0, buffer.Length);
            }
        }
    }
}

Client(客戶端):

using System;
using System.IO.MemoryMappedFiles;

class SharedMemoryClient
{
    static void Main()
    {
        string sharedMemoryName = "TestSharedMemory";
        using (MemoryMappedFile sharedMemory = MemoryMappedFile.OpenExisting(sharedMemoryName))
        {
            using (MemoryMappedViewAccessor accessor = sharedMemory.CreateViewAccessor())
            {
                Console.WriteLine("Reading from shared memory...");
                byte[] buffer = new byte[1024];
                int bytesRead = accessor.Read(0, buffer, 0, buffer.Length);
                string message = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead);
                Console.WriteLine("Received from server: " + message);
            }
        }
    }
}
  1. 使用套接字(Sockets):套接字是一種通用的進程間通信方法,可以在本地或通過網絡進行通信。在C#中,可以使用System.Net.Sockets命名空間中的TcpClientTcpListener類來創建和管理套接字。

示例:創建一個簡單的TCP服務器和客戶端

Server(服務器端):

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

class TcpServer
{
    static async Task Main(string[] args)
    {
        int port = 8080;
        using (TcpListener listener = new TcpListener(IPAddress.Any, port))
        {
            listener.Start();
            Console.WriteLine("Waiting for client connection...");

            using (TcpClient client = await listener.AcceptTcpClientAsync())
            {
                NetworkStream stream = client.GetStream();
                byte[] buffer = new byte[1024];
                int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
                string message = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                Console.WriteLine("Received from client: " + message);

                string response = "Hello from server!";
                byte[] responseBytes = Encoding.UTF8.GetBytes(response);
                await stream.WriteAsync(responseBytes, 0, responseBytes.Length);
            }
        }
    }
}

Client(客戶端):

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

class TcpClient
{
    static async Task Main(string[] args)
    {
        string serverIp = "127.0.0.1";
        int port = 8080;

        using (TcpClient client = new TcpClient(serverIp, port))
        {
            NetworkStream stream = client.GetStream();
            string message = "Hello from client!";
            byte[] messageBytes = Encoding.UTF8.GetBytes(message);
            await stream.WriteAsync(messageBytes, 0, messageBytes.Length);

            byte[] buffer = new byte[1024];
            int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
            string response = Encoding.UTF8.GetString(buffer, 0, bytesRead);
            Console.WriteLine("Received from server: " + response);
        }
    }
}

這些示例展示了如何在C#中使用ProcessStartInfo類以及上述方法來管理進程間通信。你可以根據具體需求選擇合適的IPC方法。

0
安阳县| 黔西| 富川| 镇赉县| 井陉县| 乐亭县| 独山县| 长沙市| 顺昌县| 海晏县| 金坛市| 聂荣县| 阿城市| 镇远县| 津南区| 湖南省| 信宜市| 岑巩县| 吴忠市| 邢台县| 江阴市| 都昌县| 保康县| 中山市| 兴义市| 元阳县| 鹤山市| 垦利县| 南京市| 车致| 武定县| 资阳市| 梅州市| 罗山县| 鹰潭市| 五家渠市| 故城县| 华阴市| 尉氏县| 永清县| 开平市|