首页 > 极客资料 博客日记
AvaloniaTCP-v1.0.0:学习使用Avalonia/C#进行TCP通讯的一个简单Demo
2024-10-14 13:00:04极客资料围观15次
本篇文章分享AvaloniaTCP-v1.0.0:学习使用Avalonia/C#进行TCP通讯的一个简单Demo,对你有帮助的话记得收藏一下,看极客之家收获更多编程知识
TCP通讯简介
TCP(传输控制协议,Transmission Control Protocol)是一种面向连接的、可靠的、基于字节流的传输层通信协议。它确保数据包按顺序传输,并在必要时进行重传,以保证数据的完整性和准确性。TCP通过三次握手建立连接,通过四次挥手释放连接,确保通信双方在传输数据前已准备好,并在传输结束后正确关闭连接。TCP广泛应用于需要高可靠性的网络应用,如网页浏览、文件传输和电子邮件等。
Demo效果
启动两个应用,一个当服务端,一个当客户端。
开启服务端:
开启客户端:
客户端向服务端发送消息:
服务端向客户端发送消息:
Demo代码
启动服务端:
[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);
}
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();
}
启动客户端:
[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);
}
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();
}
向服务端发消息:
[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";
}
向客户端发消息:
[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";
}
全部代码已上传至https://github.com/Ming-jiayou/AvaloniaTCP。
希望通过我的点滴分享,能够让对Avalonia感兴趣的朋友,更快入门。
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
相关文章
最新发布
- Nuxt.js 应用中的 prerender:routes 事件钩子详解
- 【问题解决】Tomcat由低于8版本升级到高版本使用Tomcat自带连接池报错无法找到表空间的问题
- 【FAQ】HarmonyOS SDK 闭源开放能力 —Vision Kit
- 六、Spring Boot集成Spring Security之前后分离认证流程最佳方案
- 《JVM第7课》堆区
- .NET 8 高性能跨平台图像处理库 ImageSharp
- 还在为慢速数据传输苦恼?Linux 零拷贝技术来帮你!
- 刚毕业,去做边缘业务,还有救吗?
- 如何避免 HttpClient 丢失请求头:通过 HttpRequestMessage 解决并优化
- 让性能提升56%的Vue3.5响应式重构之“版本计数”