溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#多進(jìn)程與網(wǎng)絡(luò)通信

發(fā)布時(shí)間:2024-09-13 16:32:54 來源:億速云 閱讀:78 作者:小樊 欄目:編程語言

在C#中,可以使用多進(jìn)程和網(wǎng)絡(luò)通信來實(shí)現(xiàn)并行處理和數(shù)據(jù)傳輸。這里是一個(gè)簡單的示例,展示了如何在C#中使用多進(jìn)程和網(wǎng)絡(luò)通信。

首先,我們需要?jiǎng)?chuàng)建一個(gè)TCP服務(wù)器,用于監(jiān)聽客戶端連接和接收數(shù)據(jù)。以下是一個(gè)簡單的TCP服務(wù)器示例:

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

class TcpServer
{
    private TcpListener _listener;

    public TcpServer(int port)
    {
        _listener = new TcpListener(IPAddress.Any, port);
    }

    public void Start()
    {
        _listener.Start();
        Console.WriteLine("Server started.");

        while (true)
        {
            var client = _listener.AcceptTcpClient();
            Console.WriteLine("Client connected.");

            var thread = new Thread(HandleClient);
            thread.Start(client);
        }
    }

    private void HandleClient(object obj)
    {
        var client = (TcpClient)obj;
        var stream = client.GetStream();
        var buffer = new byte[1024];

        while (true)
        {
            var bytesRead = stream.Read(buffer, 0, buffer.Length);
            if (bytesRead == 0) break;

            var data = Encoding.UTF8.GetString(buffer, 0, bytesRead);
            Console.WriteLine($"Received: {data}");
        }

        client.Close();
        Console.WriteLine("Client disconnected.");
    }
}

接下來,我們需要?jiǎng)?chuàng)建一個(gè)TCP客戶端,用于連接到服務(wù)器并發(fā)送數(shù)據(jù)。以下是一個(gè)簡單的TCP客戶端示例:

using System;
using System.Net.Sockets;
using System.Text;

class TcpClient
{
    private TcpClient _client;

    public TcpClient(string host, int port)
    {
        _client = new TcpClient(host, port);
    }

    public void SendData(string data)
    {
        var stream = _client.GetStream();
        var buffer = Encoding.UTF8.GetBytes(data);
        stream.Write(buffer, 0, buffer.Length);
    }
}

現(xiàn)在,我們可以創(chuàng)建一個(gè)多進(jìn)程應(yīng)用程序,其中一個(gè)進(jìn)程作為服務(wù)器,另一個(gè)進(jìn)程作為客戶端。以下是一個(gè)簡單的多進(jìn)程示例:

using System;
using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            // 啟動服務(wù)器進(jìn)程
            var serverProcess = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "dotnet",
                    Arguments = $"{typeof(Program).Assembly.Location} server",
                    UseShellExecute = false,
                    CreateNoWindow = true
                }
            };
            serverProcess.Start();

            // 啟動客戶端進(jìn)程
            var clientProcess = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "dotnet",
                    Arguments = $"{typeof(Program).Assembly.Location} client",
                    UseShellExecute = false,
                    CreateNoWindow = true
                }
            };
            clientProcess.Start();

            serverProcess.WaitForExit();
            clientProcess.WaitForExit();
        }
        else if (args[0] == "server")
        {
            var server = new TcpServer(12345);
            server.Start();
        }
        else if (args[0] == "client")
        {
            var client = new TcpClient("localhost", 12345);
            client.SendData("Hello from client!");
        }
    }
}

在這個(gè)示例中,我們首先檢查命令行參數(shù)的數(shù)量。如果沒有參數(shù),我們啟動服務(wù)器和客戶端進(jìn)程。如果有參數(shù),我們根據(jù)參數(shù)值(“server"或"client”)來運(yùn)行相應(yīng)的代碼。

這個(gè)示例展示了如何在C#中使用多進(jìn)程和網(wǎng)絡(luò)通信。你可以根據(jù)自己的需求修改這些代碼,以實(shí)現(xiàn)更復(fù)雜的功能。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI