在C#中,ProcessStartInfo
類主要用于啟動和管理外部進程
System.IO.Pipes
命名空間中的NamedPipeServerStream
和NamedPipeClientStream
類來創建和管理命名管道。示例:創建一個簡單的命名管道服務器和客戶端
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();
}
}
}
System.IO.MemoryMappedFiles
命名空間中的MemoryMappedFile
和MemoryMappedViewAccessor
類來創建和管理共享內存。示例:創建一個簡單的共享內存服務器和客戶端
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);
}
}
}
}
System.Net.Sockets
命名空間中的TcpClient
和TcpListener
類來創建和管理套接字。示例:創建一個簡單的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方法。