AvaloniaTCP-v1.0.0:学习使用Avalonia/C#进行TCP通讯的一个简单Demo

TCP通讯简介

TCP(传输控制协议,Transmission Control Protocol)是一种面向连接的、可靠的、基于字节流的传输层通信协议。它确保数据包按顺序传输,并在必要时进行重传,以保证数据的完整性和准确性。TCP通过三次握手建立连接,通过四次挥手释放连接,确保通信双方在传输数据前已准备好,并在传输结束后正确关闭连接。TCP广泛应用于需要高可靠性的网络应用,如网页浏览、文件传输和电子邮件等。

Demo效果

启动两个应用,一个当服务端,一个当客户端。

开启服务端:

开启客户端:

客户端向服务端发送消息:

服务端向客户端发送消息:

Demo代码

启动服务端:

ini 复制代码
[RelayCommand]
private async Task StartServer()
{
    System.Net.IPAddress Ip = System.Net.IPAddress.Parse(IpAddress);
    _tcpServer = new TcpListener(Ip, Port);
    _tcpServer.Start();
    Message += "Server started. Waiting for a connection...\r\n";
​
    // 接受客户端连接
    _tcpServer_Client = await _tcpServer.AcceptTcpClientAsync();
    Message += "客户端已连接\r\n";
​
    // Handle client communication
    _ = HandleClientAsync(_tcpServer_Client);
}
csharp 复制代码
private async Task HandleClientAsync(TcpClient client)
{
    var stream = client.GetStream();
    var buffer = new byte[1024];
    int bytesRead;
​
    while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) != 0)
    {
        var message = Encoding.UTF8.GetString(buffer, 0, bytesRead);
        Message+=$"Received from client: {message}\r\n";
​
        // Echo the message back to the client
        //var response = Encoding.UTF8.GetBytes($"Echo: {message}");
        //await stream.WriteAsync(response, 0, response.Length);
    }
​
    Message += "Client disconnected...\r\n";
    stream.Close();
}

启动客户端:

ini 复制代码
 [RelayCommand]
 private async Task StartClient()
 {
     System.Net.IPAddress Ip = System.Net.IPAddress.Parse(IpAddress);
     _tcpClient = new TcpClient();
     await _tcpClient.ConnectAsync(Ip, Port);
     Message += "Connected to server...\r\n";
     
     _ = HandleServerCommunicationAsync(_tcpClient);
 }
ini 复制代码
private async Task HandleServerCommunicationAsync(TcpClient client)
{
    var stream = client.GetStream();
    var buffer = new byte[1024];
    int bytesRead;
​
    while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) != 0)
    {
        var message = Encoding.UTF8.GetString(buffer, 0, bytesRead);
        Message += $"Received from server: {message}\r\n";
       
    }
​
    Message += "Disconnected from server...\r\n";
    stream.Close();
}

向服务端发消息:

csharp 复制代码
 [RelayCommand]
 private async Task SendMessageToServer()
 {
     if (_tcpClient == null || !_tcpClient.Connected)
     {
         Message += "Not connected to server.\r\n";
         return;
     }
​
     var stream = _tcpClient.GetStream();
     var data = Encoding.UTF8.GetBytes(Text);
     await stream.WriteAsync(data, 0, data.Length);
     Message += $"Sent: {Text}\r\n";
 }

向客户端发消息:

csharp 复制代码
[RelayCommand]
private async Task SendMessageToClient()
{
    if (_tcpServer_Client == null || !_tcpServer_Client.Connected)
    {
        Message += "Not connected to client.\r\n";
        return;
    }
​
    var stream = _tcpServer_Client.GetStream();
    var data = Encoding.UTF8.GetBytes(Text);
    await stream.WriteAsync(data, 0, data.Length);
    Message += $"Sent: {Text}\r\n";
}

全部代码已上传至github.com/Ming-jiayou...

希望通过我的点滴分享,能够让对Avalonia感兴趣的朋友,更快入门。

相关推荐
我好喜欢你~4 小时前
C#---StopWatch类
开发语言·c#
一阵没来由的风7 小时前
拒绝造轮子(C#篇)ZLG CAN卡驱动封装应用
c#·can·封装·zlg·基础封装·轮子
一枚小小程序员哈13 小时前
基于微信小程序的家教服务平台的设计与实现/基于asp.net/c#的家教服务平台/基于asp.net/c#的家教管理系统
后端·c#·asp.net
Eternity_GQM15 小时前
【Word VBA Zotero 引用宏错误分析与改正指南】【解决[21–23]参考文献格式插入超链接问题】
开发语言·c#·word
cimeo20 小时前
【C 学习】06-算法&程序设计举例
c#
百锦再20 小时前
.NET 的 WebApi 项目必要可配置项都有哪些?
java·开发语言·c#·.net·core·net
WYH2871 天前
C#控制台输入(Read()、ReadKey()和ReadLine())
开发语言·c#
hqwest1 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
做一位快乐的码农1 天前
基于.net、C#、asp.net、vs的保护大自然网站的设计与实现
c#·asp.net·.net
DavieLau1 天前
C#项目WCF接口暴露调用及SOAP接口请求测试(Python版)
xml·服务器·开发语言·python·c#