TcpEcho 项目使用教程

TcpEcho 项目使用教程

TcpEchoBasic TCP server that uses System.IO.Pipelines to parse line based messages项目地址:https://gitcode.com/gh_mirrors/tc/TcpEcho

1. 项目的目录结构及介绍

TcpEcho 项目的目录结构如下:

TcpEcho/
├── Program.cs
├── TcpEcho.csproj
└── README.md
  • Program.cs: 项目的入口文件,包含主要的逻辑代码。
  • TcpEcho.csproj: 项目的配置文件,定义了项目的依赖和构建信息。
  • README.md: 项目的说明文档,提供了项目的基本信息和使用指南。

2. 项目的启动文件介绍

Program.cs 是 TcpEcho 项目的启动文件,主要负责启动 TCP 服务器并处理客户端连接。以下是 Program.cs 的主要内容:

using System;
using System.IO.Pipelines;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace TcpEcho
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var listenSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
            listenSocket.Bind(new IPEndPoint(IPAddress.Loopback, 8080));
            listenSocket.Listen(10);

            Console.WriteLine("Listening on port 8080");

            while (true)
            {
                var socket = await listenSocket.AcceptAsync();
                _ = ProcessLinesAsync(socket);
            }
        }

        private static async Task ProcessLinesAsync(Socket socket)
        {
            var pipe = new Pipe();
            Task writing = FillPipeAsync(socket, pipe.Writer);
            Task reading = ReadPipeAsync(socket, pipe.Reader);

            await Task.WhenAll(reading, writing);
        }

        private static async Task FillPipeAsync(Socket socket, PipeWriter writer)
        {
            const int minimumBufferSize = 512;

            while (true)
            {
                Memory<byte> memory = writer.GetMemory(minimumBufferSize);
                try
                {
                    int bytesRead = await socket.ReceiveAsync(memory, SocketFlags.None);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    writer.Advance(bytesRead);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.Message);
                    break;
                }

                FlushResult result = await writer.FlushAsync();

                if (result.IsCompleted)
                {
                    break;
                }
            }

            writer.Complete();
        }

        private static async Task ReadPipeAsync(Socket socket, PipeReader reader)
        {
            while (true)
            {
                ReadResult result = await reader.ReadAsync();
                ReadOnlySequence<byte> buffer = result.Buffer;

                while (TryReadLine(ref buffer, out ReadOnlySequence<byte> line))
                {
                    ProcessLine(socket, line);
                }

                reader.AdvanceTo(buffer.Start, buffer.End);

                if (result.IsCompleted)
                {
                    break;
                }
            }

            reader.Complete();
        }

        private static bool TryReadLine(ref ReadOnlySequence<byte> buffer, out ReadOnlySequence<byte> line)
        {
            var position = buffer.PositionOf((byte)'\n');

            if (position == null)
            {
                line = default;
                return false;
            }

            line = buffer.Slice(0, position.Value);
            buffer = buffer.Slice(buffer.GetPosition(1, position.Value));
            return true;
        }

        private static void ProcessLine(Socket socket, in ReadOnlySequence<byte> line)
        {
            foreach (var segment in line)
            {
                _ = socket.SendAsync(segment, SocketFlags.None);
            }
        }
    }
}

3. 项目的配置文件介绍

TcpEcho.csproj 是 TcpEcho 项目的配置文件,定义了项目的依赖和构建信息。以下是 TcpEcho.csproj 的主要内容:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

</Project>
  • OutputType: 指定输出类型为可执行文件(Exe)。
  • TargetFramework: 指定目标框架为 .NET 5.0。

以上是 TcpEcho 项目的使用教程,希望对您有所帮助。

TcpEchoBasic TCP server that uses System.IO.Pipelines to parse line based messages项目地址:https://gitcode.com/gh_mirrors/tc/TcpEcho

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温欣晶Eve

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值