91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
平江县| 临洮县| 乌拉特中旗| 神木县| 沁水县| 紫阳县| 东乌珠穆沁旗| 永定县| 石阡县| 漠河县| 眉山市| 牙克石市| 万安县| 遂川县| 山东省| 博白县| 叙永县| 海丰县| 德格县| 连州市| 南康市| 上杭县| 大兴区| 游戏| 潼关县| 金昌市| 时尚| 永胜县| 启东市| 神池县| 仁化县| 汉沽区| 来凤县| 新乐市| 农安县| 惠州市| 赤城县| 杭锦后旗| 孟津县| 德昌县| 和静县|