您的位置:首页 > 房产 > 家装 > 东营百姓网免费发布信息网_河北项目网是真实的吗_seo策略分析_免费网站建设

东营百姓网免费发布信息网_河北项目网是真实的吗_seo策略分析_免费网站建设

2025/1/7 4:38:28 来源:https://blog.csdn.net/mingupup/article/details/142915771  浏览:    关键词:东营百姓网免费发布信息网_河北项目网是真实的吗_seo策略分析_免费网站建设
东营百姓网免费发布信息网_河北项目网是真实的吗_seo策略分析_免费网站建设

TCP通讯简介

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

Demo效果

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

开启服务端:

image-20241014112528767

开启客户端:

image-20241014112558142

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

image-20241014112646168

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

image-20241014112730780

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感兴趣的朋友,更快入门。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com