Location>code7788 >text

AvaloniaTCP-v1.0.0: A Simple Demo for Learning TCP Communication with Avalonia/C#

Popularity:875 ℃/2024-10-14 12:57:55

Introduction to TCP Communications

TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte-stream-based transport layer communication protocol. It ensures that packets are transmitted sequentially and retransmitted when necessary to guarantee data integrity and accuracy.TCP establishes a connection through three handshakes and releases it through four handshakes, ensuring that the two communicating parties are ready to transmit data before it is transmitted and that the connection is closed correctly at the end of the transmission.TCP is widely used in network applications that require a high degree of reliability, such as web browsing, file transfers, and e-mail.

Demo effect

Start two applications, one as a server and one as a client.

Turn on the server:

image-20241014112528767

Open the client:

image-20241014112558142

The client sends a message to the server:

image-20241014112646168

The server sends a message to the client:

image-20241014112730780

Demo Code

Start the server:

[RelayCommand]
private async Task StartServer()
{
     Ip = (IpAddress);
    _tcpServer = new TcpListener(Ip, Port);
    _tcpServer.Start();
    Message += "Server started. Waiting for a connection...\r\n";

    // Accepting client connections
    _tcpServer_Client = await _tcpServer.AcceptTcpClientAsync();
    Message += "Client Connected\r\n";

    // Handle client communication
    _ = HandleClientAsync(_tcpServer_Client);
}
private async Task HandleClientAsync(TcpClient client)
{
    var stream = ();
    var buffer = new byte[1024];
    int bytesRead;

    while ((bytesRead = await (buffer, 0, )) != 0)
    {
        var message = Encoding.(buffer, 0, bytesRead);
        Message+=$"Received from client: {message}\r\n";

        // Echo the message back to the client
        //var response = Encoding.($"Echo: {message}");
        //await (response, 0, );
    }

    Message += "Client disconnected...\r\n";
    ();
}

Launch the client:

 [RelayCommand]
 private async Task StartClient()
 {
      Ip = (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 = ();
    var buffer = new byte[1024];
    int bytesRead;

    while ((bytesRead = await (buffer, 0, )) != 0)
    {
        var message = Encoding.(buffer, 0, bytesRead);
        Message += $"Received from server: {message}\r\n";
       
    }

    Message += "Disconnected from server...\r\n";
    ();
}

Sends a message to the server:

 [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.(Text);
     await (data, 0, );
     Message += $"Sent: {Text}\r\n";
 }

Sends a message to the client:

[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.(Text);
    await (data, 0, );
    Message += $"Sent: {Text}\r\n";
}

The full code has been uploaded to /Ming-jiayou/AvaloniaTCP.

I hope that by sharing a little bit of what I've done, I can get those who are interested in Avalonia, started faster.