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

溫馨提示×

如何實現C# Socket的多線程通信

c#
小樊
88
2024-09-04 06:42:34
欄目: 編程語言

在C#中,使用Socket實現多線程通信可以通過創建一個服務器端和客戶端并為每個連接創建單獨的線程來完成。這里是一個簡單的示例,展示了如何實現多線程Socket通信:

  1. 首先,創建一個服務器端:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

class Server
{
    private TcpListener _listener;

    public void Start(int port)
    {
        _listener = new TcpListener(IPAddress.Any, port);
        _listener.Start();

        Console.WriteLine("Server started on port: " + port);

        while (true)
        {
            // Accept incoming connections and create a new thread for each connection
            TcpClient client = _listener.AcceptTcpClient();
            Thread clientThread = new Thread(HandleClient);
            clientThread.Start(client);
        }
    }

    private void HandleClient(object obj)
    {
        TcpClient client = (TcpClient)obj;
        NetworkStream stream = client.GetStream();

        // Read and process data from the client
        byte[] buffer = new byte[256];
        int bytesRead = stream.Read(buffer, 0, buffer.Length);
        string receivedData = System.Text.Encoding.ASCII.GetString(buffer, 0, bytesRead);

        Console.WriteLine("Received data: " + receivedData);

        // Send response to the client
        string response = "Server received: " + receivedData;
        byte[] responseData = System.Text.Encoding.ASCII.GetBytes(response);
        stream.Write(responseData, 0, responseData.Length);

        // Close the connection
        stream.Close();
        client.Close();
    }
}
  1. 然后,創建一個客戶端:
using System;
using System.Net.Sockets;

class Client
{
    public void Connect(string serverIp, int port)
    {
        TcpClient client = new TcpClient(serverIp, port);
        NetworkStream stream = client.GetStream();

        // Send data to the server
        string message = "Hello, Server!";
        byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
        stream.Write(data, 0, data.Length);

        // Receive response from the server
        byte[] buffer = new byte[256];
        int bytesRead = stream.Read(buffer, 0, buffer.Length);
        string response = System.Text.Encoding.ASCII.GetString(buffer, 0, bytesRead);

        Console.WriteLine("Server response: " + response);

        // Close the connection
        stream.Close();
        client.Close();
    }
}
  1. 最后,在主程序中啟動服務器和客戶端:
using System;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        // Start the server
        Server server = new Server();
        Thread serverThread = new Thread(() => server.Start(8000));
        serverThread.Start();

        // Give the server some time to start
        Thread.Sleep(1000);

        // Connect the client
        Client client = new Client();
        client.Connect("127.0.0.1", 8000);

        Console.ReadLine();
    }
}

這個示例中,服務器監聽指定端口上的連接請求,并為每個連接創建一個新線程。客戶端連接到服務器并發送一條消息,然后接收服務器的響應。這種方法可以輕松地擴展到處理多個客戶端連接。

0
出国| 商洛市| 左贡县| 宣武区| 西宁市| 北海市| 洞口县| 五峰| 巴南区| 宣化县| 定结县| 涟水县| 英吉沙县| 云龙县| 定州市| 明光市| 福泉市| 土默特右旗| 巢湖市| 临高县| 石阡县| 攀枝花市| 临颍县| 六安市| 青岛市| 诏安县| 昌图县| 通山县| 奇台县| 苏尼特右旗| 鄢陵县| 漳浦县| 江城| 烟台市| 杭州市| 右玉县| 蒲城县| 汝城县| 昌黎县| 彩票| 正阳县|